<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # C++ 中的`switch-case`語句 > 原文: [https://beginnersbook.com/2017/08/cpp-switch-case/](https://beginnersbook.com/2017/08/cpp-switch-case/) 當我們有多個條件并且我們需要根據條件執行不同的操作時,使用`switch case`語句。當我們有多個條件時,我們需要在滿足特定條件時執行一個語句塊。在這種情況下,我們可以使用冗長的 [`if..else-if`語句](https://beginnersbook.com/2017/08/cpp-if-else-statement/)或`switch case`。冗長`if..else-if`的問題是當我們有幾個條件時它會變得復雜。`switch-case`是處理這種情況的干凈而有效的方法。 `switch case`語句的**語法:** ```cpp switch (variable or an integer expression) { case constant: //C++ code ; case constant: //C++ code ; default: //C++ code ; } ``` 即使`break`語句是可選的,`switch case`語句也大量使用`break`語句。我們將首先看到一個沒有`break`語句的例子,然后我們將討論`switch case`和`break`。 ## `switch-case`示例 ```cpp #include <iostream> using namespace std; int main(){ int num=5; switch(num+2) { case 1: cout<<"Case1: Value is: "<<num<<endl; case 2: cout<<"Case2: Value is: "<<num<<endl; case 3: cout<<"Case3: Value is: "<<num<<endl; default: cout<<"Default: Value is: "<<num<<endl; } return 0; } ``` **輸出:** ```cpp Default: Value is: 5 ``` **說明**:在`swicth`中我給出了一個表達式,你也可以給出變量。我給了表達式`num + 2`,其中`num`值是 5,并且在相加之后表達式得到 7。由于沒有用值 4 定義的情況,所以執行了默認情況。 ## `switch-case`流程圖 它求值表達式或變量的值(基于`switch`括號內給出的內容),然后根據結果執行相應的情況。 ![switch case flow diagram](https://img.kancloud.cn/2b/64/2b647e7125b8646b3efe47fc14412d93_400x400.jpg) ## `switch-case`中的`break`語句 在我們討論`break`語句之前,讓我們看看當我們在`switch case`中不使用`break`語句時會發生什么。請參閱以下示例: ```cpp #include <iostream> using namespace std; int main(){ int i=2; switch(i) { case 1: cout<<"Case1 "<<endl; case 2: cout<<"Case2 "<<endl; case 3: cout<<"Case3 "<<endl; case 4: cout<<"Case4 "<<endl; default: cout<<"Default "<<endl; } return 0; } ``` **輸出:** ```cpp Case2 Case3 Case4 Default ``` 在上面的程序中,我們在`switch`括號內部有變量`i`,這意味著無論變量`i`的值是什么,都會執行相應的`case`塊。我們已將整數值 2 傳遞給`switch`,因此控制流切換到`case 2`,但是在`case 2` 之后我們沒有使用`break`語句,導致控制流繼續到后續`case`直到結束。然而,這不是我們想要的,我們想要執行正確`case`塊并忽略其余的塊。這個問題的解決方案是在每個`case`塊之后使用`break`語句。 當您希望程序流從`switch`主體中出來時,使用`break`語句。每當在`switch`主體中遇到`break`語句時,執行流程將直接從`switch`中出來,忽略其余的情況。這就是您必須使用`break`語句結束每個`case`塊的原因。 讓我們采用相同的例子,但這次使用`break`語句。 ```cpp #include <iostream> using namespace std; int main(){ int i=2; switch(i) { case 1: cout<<"Case1 "<<endl; break; case 2: cout<<"Case2 "<<endl; break; case 3: cout<<"Case3 "<<endl; break; case 4: cout<<"Case4 "<<endl; break; default: cout<<"Default "<<endl; } return 0; } ``` **輸出:** ```cpp Case2 ``` 現在您可以看到只有`case 2`被執行,其余的后續`case`被忽略了。 **為什么我在`default`塊不使用`break`語句?** 控制流本身會在默認情況下從`switch`中出來,所以我之后沒有使用`break`語句,但是如果你想要它可以使用它,那么這樣做是沒有害處的。 ## 重要筆記 1)`case`并不總是需要順序`1,2,3`等。它可以在`case`關鍵字后面包含任何整數值。此外,`case`不需要始終按升序排列,您可以根據要求以任何順序指定它們。 2)您也可以在`switch-case`中使用字符。例如: ```cpp #include <iostream> using namespace std; int main(){ char ch='b'; switch(ch) { case 'd': cout<<"Case1 "; break; case 'b': cout<<"Case2 "; break; case 'x': cout<<"Case3 "; break; case 'y': cout<<"Case4 "; break; default: cout<<"Default "; } return 0; } ``` 3)允許嵌套`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>

                              哎呀哎呀视频在线观看