Euphoria Switch語句
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語句會自動出來。