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 ...with Fallthru声明

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

可以更改特定 switch 块的默认值,以便每当遇到新情况时,通过在 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语句可以有一个可选标签命名 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