<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++ 中的`if`語句 > 原文: [https://beginnersbook.com/2017/08/cpp-if-else-statement/](https://beginnersbook.com/2017/08/cpp-if-else-statement/) 有時我們只有在滿足或不滿足特定條件時才需要執行一個語句塊。這被稱為**決策**,因為我們在程序邏輯中做出決定后執行某個代碼。對于 C++ 中的決策,我們有四種類型的控制語句(或控制結構),如下所示: a)`if`語句 b)嵌套`if`語句 c)`if-else`語句 d)`if-else-if`語句 ## C++ 中的`if`語句 `if`語句包含條件,后跟語句或一組語句,如下所示: ```cpp if(condition){ Statement(s); } ``` `if`括號(通常稱為正文)中的語句僅在給定條件為真時才執行。如果條件為假,則完全忽略正文中的語句。** **`if`語句**的流程圖 ![if statement flow diagram](https://img.kancloud.cn/0d/cd/0dcd5b5d2d2409266b2edc7ebaa3b461_400x400.jpg) ### `if`語句的示例 ```cpp #include <iostream> using namespace std; int main(){ int num=70; if( num < 100 ){ /* This cout statement will only execute, * if the above condition is true */ cout<<"number is less than 100"; } if(num > 100){ /* This cout statement will only execute, * if the above condition is true */ cout<<"number is greater than 100"; } return 0; } ``` **輸出:** ```cpp number is less than 100 ``` ## C++ 中的嵌套`if`語句 當在另一個`if`語句中有`if`語句時,它被稱為**嵌套`if`語句**。嵌套的結構如下所示: ```cpp if(condition_1) { Statement1(s); if(condition_2) { Statement2(s); } } ``` > 如果`condition_1`為`true`,則執行`Statement1`。只有條件(`condition_1`和`condition_2`)都為真時,`Statement2`才會執行。 ### 嵌套`if`語句的示例 ```cpp #include <iostream> using namespace std; int main(){ int num=90; /* Nested if statement. An if statement * inside another if body */ if( num < 100 ){ cout<<"number is less than 100"<<endl; if(num > 50){ cout<<"number is greater than 50"; } } return 0; } ``` **輸出:** ```cpp number is less than 100 number is greater than 50 ``` ## 在 C++ 中使用`if-else`語句 有時你有一個條件,如果條件為真,你想要執行一段代碼,如果相同的條件為假,則執行另一段代碼。這可以使用`if-else`語句在 C++ 中實現。 這是`if-else`語句的外觀: ```cpp if(condition) { Statement(s); } else { Statement(s); } ``` 如果條件為真,則`if`內的語句將執行,如果條件為假,則`else`內的語句將執行。 **if-else** ![If else flow diagram](https://img.kancloud.cn/03/13/0313a59031293c1e271bf059213e53a0_400x400.jpg)的流程圖 ### `if-else`語句的示例 ```cpp #include <iostream> using namespace std; int main(){ int num=66; if( num < 50 ){ //This would run if above condition is true cout<<"num is less than 50"; } else { //This would run if above condition is false cout<<"num is greater than or equal 50"; } return 0; } ``` **輸出:** ```cpp num is greater than or equal 50 ``` ## C++ 中的`if-else-if`語句 當我們需要檢查多個條件時使用`if-else-if`語句。在這個控制結構中,我們只有一個`if`和一個`else`,但是我們可以有多個`else if`塊。這是它的樣子: ```cpp if(condition_1) { /*if condition_1 is true execute this*/ statement(s); } else if(condition_2) { /* execute this if condition_1 is not met and * condition_2 is met */ statement(s); } else if(condition_3) { /* execute this if condition_1 & condition_2 are * not met and condition_3 is met */ statement(s); } . . . else { /* if none of the condition is true * then these statements gets executed */ statement(s); } ``` **注意:**這里要注意的最重要的一點是,在`if-else-if`中,只要滿足條件,就會執行相應的語句集,忽略其余。如果沒有滿足條件,則執行`else`內的語句。 ### `if-else-if`的示例 ```cpp #include <iostream> using namespace std; int main(){ int num; cout<<"Enter an integer number between 1 & 99999: "; cin>>num; if(num <100 && num>=1) { cout<<"Its a two digit number"; } else if(num <1000 && num>=100) { cout<<"Its a three digit number"; } else if(num <10000 && num>=1000) { cout<<"Its a four digit number"; } else if(num <100000 && num>=10000) { cout<<"Its a five digit number"; } else { cout<<"number is not between 1 & 99999"; } return 0; } ``` **輸出:** ```cpp Enter an integer number between 1 & 99999: 8976 Its a four digit number ```
                  <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>

                              哎呀哎呀视频在线观看