switch 语句:
switch语句用于运行一组特定的语句,取决于一个表达式的值。它往往取代了一组 if-elsif语句,给你更多的控制和程序的可读性。
语法:
简单的 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
|
case 的 <val> 必须是原子,文字字符串,常数或枚举。多值一个 case 可以指定用逗号分隔的值。缺省情况下,控制switch块结束时直到遇到下一个case 。
例子:
#!/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 语句:
case语句表达式的值匹配时,执行一个switch ,默认情况下它出来。缺省情况下,控制流开关块结束时,遇到下一个case。
默认为一个特定的switch 模块是可以改变的,所以控制传递到下一条可执行语句,每当有新的 case,在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
|
例子:
#!/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语句可以有一个可选的标签开关命名块。这个名字可以用在嵌套switch break语句打破封闭开关,而不是仅仅拥有switch .
一个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
|
例子:
#!/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! |
注:如果你不使用fallthru 语句,那么你就不需要使用标签,因为switch语句会自动出来。
