Euphoria - 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> 必须是原子、文字字符串、常量或枚举。 可以通过用逗号分隔值来指定单个案例的多个值。 默认情况下,当遇到下一个情况时,控制流到 switch 块的末尾。
示例
#!/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...withfallthru 语句
switch 的 case 语句在与给定表达式值匹配时执行,并且默认情况下会出现。 默认情况下,当遇到下一个情况时,控制流到 switch 块的末尾。
可以更改特定 switch 块的默认值,以便每当遇到新情况时,通过在 switch 语句中使用 with fallthru −,控制权就会传递到下一个可执行语句 −
语法
简单的switch...withfallthru语句的语法如下 −
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 语句可以有一个可选的label来命名 switch 块。 该名称可以用在嵌套 switch 中断语句中,以打破封闭的开关而不仅仅是所属的开关。
开关标签仅用于命名块,标签名称必须是具有单个或多个单词的双引号常量字符串。 label 关键字区分大小写,应写为 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!
注意 − 如果您没有使用 with Fallthru 语句,则不需要使用标签,因为 switch 语句会自动出现。