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! |
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語句可以有一個可選的標籤開關命名塊。這個名字可以用在巢狀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語句會自動出來。