switch statement
优质
小牛编辑
127浏览
2023-12-01
switch语句用于运行一组特定的语句,具体取决于表达式的值。 它经常取代一组if…elsif语句,让您对程序有更多的控制和可读性
语法 (Syntax)
简单switch语句的语法如下 -
switch expression do
case <val> [, <val-1>....] then
-- Executes when the expression matches one of the values
case <val> [, <val-1>....] then
-- Executes when the expression matches one of the values
.....................
case else
-- Executes when the expression does not matches any case.
end if
案例中的必须是原子,文字字符串,常量或枚举。 可以通过用逗号分隔值来指定单个案例的多个值。 默认情况下,当遇到下一个案例时,控制将流向开关块的末尾。
例子 (Example)
#!/home/euphoria-4.0b2/bin/eui
atom marks = 'C'
switch marks do
case 'A' then
puts(1, "Excellent!\n" )
case 'B', 'C' then
puts(1, "Well done!\n" )
case 'D' then
puts(1, "You passed!\n" )
case 'F' then
puts(1, "Better try again!\n" )
case else
puts(1, "Invalid grade!\n" )
end switch
这会产生以下结果 -
Well done!
switch...with fallthru声明
当switch与给定的表达式值匹配时,将执行switch的case语句,默认情况下它会出现。 默认情况下,当遇到下一个案例时,控制将流向开关块的末尾。
可以更改特定开关块的默认值,以便只要在switch语句中使用with fallthru遇到新案例,控制就会传递给下一个可执行语句 -
语法 (Syntax)
简单switch...with fallthru的语法switch...with fallthru语句如下 -
switch expression with fallthru do
case <val> [, <val-1>....] then
-- Executes when the expression matches one of the values
break -- optional to come out of the switch from this point.
case <val> [, <val-1>....] then
-- Executes when the expression matches one of the values
break -- Optional to come out of the switch from this point.
.....................
case else
-- Executes when the expression does not matches any case.
break -- Optional to come out of the switch from this point.
end if
例子 (Example)
#!/home/euphoria-4.0b2/bin/eui
atom marks = 'C'
switch marks with fallthru do
case 'A' then
puts(1, "Excellent!\n" )
case 'B', 'C' then
puts(1, "Well done!\n" )
case 'D' then
puts(1, "You passed!\n" )
case 'F' then
puts(1, "Better try again!\n" )
case else
puts(1, "Invalid grade!\n" )
end switch
这会产生以下结果 -
Well done!
You passed!
Better try again!
Invalid grade!
你可以使用可选的break语句从switch语句中的一个点出来,如下所示 -
#!/home/euphoria-4.0b2/bin/eui
atom marks = 'C'
switch marks with fallthru do
case 'A' then
puts(1, "Excellent!\n" )
break
case 'B', 'C' then
puts(1, "Well done!\n" )
break
case 'D' then
puts(1, "You passed!\n" )
break
case 'F' then
puts(1, "Better try again!\n" )
break
case else
puts(1, "Invalid grade!\n" )
break
end switch
这会产生以下结果 -
Well done!
switch....label声明
switch语句可以有一个可选label来命名switch块。 此名称可用于嵌套的switch break语句,以打破封闭的开关,而不仅仅是拥有的开关。
开关标签仅用于命名块,标签名称必须是带有单个或多个单词的双引号常量字符串。 label关键字区分大小写,应写为label 。
语法 (Syntax)
简单switch...label的语法switch...label语句如下 -
switch expression label "Label Name" do
case <val> [, <val-1>....] then
-- Executes when the expression matches one of the values
break "LEBEL NAME"
case <val> [, <val-1>....] then
-- Executes when the expression matches one of the values
break "LEBEL NAME"
.....................
case else
-- Executes when the expression does not matches any case.
break "LEBEL NAME"
end if
例子 (Example)
#!/home/euphoria-4.0b2/bin/eui
atom marks = 'C'
atom scale = 'L'
switch marks label "MARKS" do
case 'A' then
puts(1, "Excellent!\n" )
case 'B', 'C' then
puts(1, "Well done!\n" )
switch scale label "SCALE" do
case 'U' then
puts(1, "Upper scale!\n" )
break "MARKS"
case 'L' then
puts(1, "Lower scale!\n" )
break "MARKS"
case else
puts(1, "Invalid scale!\n" )
break "MARKS"
end switch
case 'D' then
puts(1, "You passed!\n" )
case 'F' then
puts(1, "Better try again!\n" )
case else
puts(1, "Invalid grade!\n" )
end switch
这会产生以下结果 -
Well done!
Lower scale!
Note - 如果您没有使用with fallthru语句,那么您不需要使用标签,因为switch语句会自动出现。