<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # C 編程的`switch-case`語句 > 原文: [https://beginnersbook.com/2014/01/switch-case-statements-in-c/](https://beginnersbook.com/2014/01/switch-case-statements-in-c/) 當我們有多個選項時,使用**`switch-case`語句**,我們需要為每個選項執行不同的任務。 ## C - `switch-case`語句 語法: ```c switch (variable or an integer expression) { case constant: //C Statements ; case constant: //C Statements ; default: //C Statements ; } ``` ### `switch-case`流程圖 ![C switch case](https://img.kancloud.cn/2b/64/2b647e7125b8646b3efe47fc14412d93_400x400.jpg) ### C 中的`switch-case`示例 ```c #include <stdio.h> int main() { int num=2; switch(num+2) { case 1: printf("Case1: Value is: %d", num); case 2: printf("Case1: Value is: %d", num); case 3: printf("Case1: Value is: %d", num); default: printf("Default: Value is: %d", num); } return 0; } ``` **輸出:** ```c Default: value is: 2 ``` **說明:**在`switch`中我給出了一個表達式,你也可以給變量。我給了`num + 2`,其中`num`值是 2,并且在相加之后表達式得到 4。因為沒有用值 4 定義的情況,所以執行默認情況。 ### 怪異故事 - 介紹`break`語句 在我們討論更多關于[`break`語句](https://beginnersbook.com/2014/01/c-break-statement/)之前,請猜測這個 C 程序的輸出。 ```c #include <stdio.h> int main() { int i=2; switch (i) { case 1: printf("Case1 "); case 2: printf("Case2 "); case 3: printf("Case3 "); case 4: printf("Case4 "); default: printf("Default "); } return 0; } ``` **輸出:** ```c Case2 Case3 Case4 Default ``` 我傳遞了一個變量給`switch`,變量的值是 2,所以控制跳轉到`case` 2,但是在上面的程序中沒有這樣的語句可以在`case` 2 執行后打破流程。這就是`case 2`之后,所有后續`case`和默認語句都已執行的原因。 **如何避免這種情況?** 我們可以使用`break`語句來打破每個`case`塊之后的控制流。 ### `switch-case`中的`break`語句 當您希望程序流從`switch`中出來時,`break`語句很有用。每當在`switch`體中遇到`break`語句時,控制流都會出現在`switch case`語句外。 **具有`break`的`switch-case`示例** 和在上面看到的相同,但這次我們正在使用`break`。 ```c #include <stdio.h> int main() { int i=2; switch (i) { case 1: printf("Case1 "); break; case 2: printf("Case2 "); break; case 3: printf("Case3 "); break; case 4: printf("Case4 "); break; default: printf("Default "); } return 0; } ``` **輸出:** ```c Case 2 ``` **為什么`default`后不使用`break`語句?** 控制流本身會在默認情況下從`switch`中出來,所以我沒有使用它,但是如果你想在默認情況下使用它,你可以使用它,這樣做沒有壞處。 ## 關于`switch-case`的幾個重點 1)`case`并不總是需要順序`1,2,3`等。它們可以在`case`關鍵字后面包含任何整數值。此外,`case`不需要始終按升序排列,您可以根據程序的需要以任何順序指定它們。 2)您也可以在`switch-case`中使用字符。例如: ```c #include <stdio.h> int main() { char ch='b'; switch (ch) { case 'd': printf("CaseD "); break; case 'b': printf("CaseB"); break; case 'c': printf("CaseC"); break; case 'z': printf("CaseZ "); break; default: printf("Default "); } return 0; } ``` 輸出: ```c CaseB ``` 3)`switch`中提供的表達式應該產生一個常量值,否則它將無效。 例如: **`switch`的有效表達式:** ```c switch(1+2+23) switch(1*2+3%4) ``` **無效的`switch`表達式:** ```c switch(ab+cd) switch(a+b+c) ``` 4)允許嵌套`switch`語句,這意味著你可以在另一個`switch`內部使用`switch`語句。但是應該避免使用嵌套的`switch`語句,因為它會使程序更復雜,更不易讀。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看