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>必须是原子、字面量字符串、常量或枚举。可以通过逗号分隔值来为单个 case 指定多个值。默认情况下,当遇到下一个 case 时,控制流将流向 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...with fallthru 语句

case 语句与给定表达式值匹配时,switchcase 语句将被执行,并且默认情况下它会退出。默认情况下,当遇到下一个 case 时,控制流将流向 switch 块的末尾。

可以通过在 switch 语句中使用with fallthru 来更改特定 switch 块的默认设置,以便在每次遇到新 case 时,控制流都传递到下一个可执行语句:

语法

简单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 语句可以有一个可选的label 来命名 switch 块。此名称可用于嵌套的 switch break 语句中,以跳出封闭的 switch 而不是仅跳出拥有它的 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 语句会自动退出。

euphoria_branching.htm
广告