Control Flow : Switch

Control Flow : Switch

Glance at the Past! 回顧過去

Let's begin by first refreshing what we have done in the previous course. 首先複習一下我們在上堂課所學到的東西.

Instructions 操作指引

Write an if/elseif/else statement inside the editor and make it output anything you wish. 在編輯器裡面,寫一個 if / else if / else 判斷式,並在大括號裡面隨意輸出你想要的內容。

Dipping Your Toe into Switch statements 接著試試 Switch 條件式

In the editor you see a simple switch statement. A switch statement comes in handy when you have a series of if/elseif/else statements with multiple expressions that depend on the same value. The switch statement also provides a bit of efficiency and readability. Switches work like if statements, if a condition is true, it executes a block of code.
在編輯器你看到一個簡單的 switch 條件式。一個 switch 條件式適合用來替代一組較為龐大的 if / elseif / else 判斷式,而其狀態比較都是比較相同變數的值。該 switch 條件式也帶來更好的效率以及可讀性。 Switches 在運作上就像是 if 條件式,當狀態為 true , 它將會執行該對應區域裡的程式碼。

Instructions 操作指引

Try and figure out which of these caseblocks gets executed and smash that Submit button!
試著指出那一個 case 後方的區域程式會被執行,然後按下 Submit 按鈕!

Switch Syntax (Switch語法)

A switch statement is similar to an if / elif / else statement in that you can check multiple conditions. Here's what it looks like:
Switch條件式很類似 if / elseif / else 判斷式 ,都可用來檢查多重的狀態,看起來就像如下:

$myNum = 2;

switch ($myNum) {
    case 1:
        echo "1";
    case 2:
        echo "2";
    case 3:
        echo "3";
        echo "None of the above";

1. A switch statement is made up of the switch keyword, a variable to check, and a pair of curly braces { }. Here we check the value of $myNum.
1.一個 switch 條件式是由 switch 保留字 , 用來被檢查的變數 ,還有一組大括號( { } )。這個例子我們要檢查的變數是 $myNum。

2. Then we have a case block for each comparison. For example case 1: echo"1"; break; checks whether $myNum is equal to 1. If yes, it echos "1", and uses break to exit the switch statement. 2.接著我們用 case 區塊來進行逐一的比較。例如 case 1: echo “1”; break; ,是用來檢查 $myNum是否等於 1 。如果是的話就輸出 “1” ,並使用 break來跳離此 switch 條件式。

3. Otherwise, the next case block runs.
3.要不然的話,下一個 case 區塊的內容就會繼續被執行,直到碰到 break 為止。

4. If all cases return false, the defaultcase gets executed.
4.如果所有的 case 檢查都傳回 false ,那預設的 default block 就會被執行。

Instructions 操作指引

On line 10, there's a switch statement
在第10行,有一個 switch 條件式

01.Fill the __ spots with the correct code. Check out the example above
01.在 _ _ 的位置填入正確的程式碼,可對比上方的例子。

02.Add the default case.
加入一個 default 預設的 case。

Multiple Cases. Falling Through! 多重 Cases.直接穿越!

You sometimes want to make multiple expressions, all of which have the same result. Consider the following ifstatement:

PS: ( || ) 為 or ,也就是或者的意思,下方狀態比較語意為假如 $i 為 1 或者 2 或者 3 的話..

if ($i == 1 ||
    $i == 2 ||
    $i == 3) {
echo '$i is somewhere between 1 and 3.';

With a switch statement, you can do this by adding cases right after another without a break. This is called falling through. The following code works exactly like the above if statement:
透過 switch 條件式,你只需要在關聯 case 下方加入另一個 case 而不用 break 隔開。 這種技巧被稱為 falling through (譯者自行翻作直接穿越)。 下方的程式碼運行結果與上方的例子並無二致。

case 1:
case 2:
case 3:
    echo '$i is somewhere between 1 and 3.';

Instructions 操作指引

Replace the two underscores to make the switch also check for 3 and 4.
替換兩個 _ _ ,來讓switch同時確認 3 和 4。

Using "Endswitch". Syntactic Sugar! 使用語法秘糖

You have two ways of creating a switch. First, there's the way we have made all the past exercises:
你有兩種方式來生成 switch 。第一種,也就是我們之前練習所用的寫法

switch ($i) {


But we can also make it this way:

switch ($i):

This is called alternative syntax. It exists to provide syntactic sugar

There's no difference when using either the curly brace syntax (first example) or alternative syntax (second example), it only provides readability, thus it's usually used when mixing HTML and PHP code in the same file.
使用一般的大括號寫法(第一種)或使用替代與法(第二種)在功能上並無差別,只是用來提供較好的閱讀性, 所以一般用於混合HTML和PHP的檔案當中

Instructions 操作指引

In the editor you have the code of the previous exercise. Make the code use the alternative syntax rather than the curly-brace syntax.

All On Your Own! 自己動手做

It's time to show what you have learned about switches so far!
現在是讓你試試看學了多少有關 switch 技巧的時候了!

Instructions 操作指引

Create a switch statement with 3 different cases and a default case if you wish. Feel free to use the alternative syntax as well!
建立一個switch條件式包含了三種不同的case以及一個default。 如果你想要用替代語法來寫也是沒問題的唷!


發佈自 林品爵

一個堅信程式教育能夠改變下一代未來的狂熱者,青少年揚帆計畫的推動者。十年以上全端程式開發,上百場程式教學經驗,累積學生數達百人以上。 成大學士畢業,使用過J2EE.PHP.MySQL.CoronaSDK.Android.Objective-C.Swift.Python等技術知識,歷任友訊科技總部Java工程師首席.黑快馬股份有限公司研發經理.諾亞數碼娛樂執行長等職位,現任哥布林程式教育學苑創辦人,所開發過專案涵括產品管理系統.POS系統.拍賣平台.擴增實境.問券.社區App等等。 程式啟蒙班講師 . Laravel網頁程式班講師 . App程式班講師