<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..else`,嵌套`if..else` 和 `else..if`語句 > 原文: [https://beginnersbook.com/2014/01/c-if-else-statement-example/](https://beginnersbook.com/2014/01/c-if-else-statement-example/) 在上一個教程中,我們學習了如何在 C 中使用[`if`語句](https://beginnersbook.com/2014/01/c-if-statement/)。在本指南中,我們將學習如何使用 C 語句中的`if else`,嵌套`if else`和`else`語句。 ## C `if-else`語句 **`if else`語句的語法:** 如果條件返回`true`,則執行`if`正文內的語句,并跳過`else`正文內的語句。 如果條件返回`false`,則跳過`if`正文中的語句,并執行`else`中的語句。 ```c if(condition) { // Statements inside body of if } else { //Statements inside body of else } ``` ### `if-else`語句的流程圖 ![C If else flow diagram](https://img.kancloud.cn/03/13/0313a59031293c1e271bf059213e53a0_400x400.jpg) ### `if-else`語句的示例 在此程序中,要求用戶輸入年齡,并根據輸入,`if..else`語句檢查輸入的年齡是否大于或等于 18。如果滿足此條件,則顯示消息“您有資格投票”,但是,如果條件不符合,則顯示不同的消息“您沒有資格投票”。 ```c #include <stdio.h> int main() { int age; printf("Enter your age:"); scanf("%d",&age); if(age >=18) { /* This statement will only execute if the * above condition (age>=18) returns true */ printf("You are eligible for voting"); } else { /* This statement will only execute if the * condition specified in the "if" returns false. */ printf("You are not eligible for voting"); } return 0; } ``` 輸出: ```c Enter your age:14 You are not eligible for voting ``` **注意:**如果**只有一個語句**出現在`if`或`else`正文中,那么你不需要使用大括號(括號)。例如,上面的程序可以像這樣重寫: ```c #include <stdio.h> int main() { int age; printf("Enter your age:"); scanf("%d",&age); if(age >=18) printf("You are eligible for voting"); else printf("You are not eligible for voting"); return 0; } ``` ## C 嵌套`if-else`語句 當`if else`語句出現在另一個`if`或`else`的正文內時,則稱為嵌套`if-else`。 **嵌套`if`語句的語法:** ```c if(condition) { //Nested if else inside the body of "if" if(condition2) { //Statements inside the body of nested "if" } else { //Statements inside the body of nested "else" } } else { //Statements inside the body of "else" } ``` ### 嵌套`if-else`的示例 ```c #include <stdio.h> int main() { int var1, var2; printf("Input the value of var1:"); scanf("%d", &var1); printf("Input the value of var2:"); scanf("%d",&var2); if (var1 != var2) { printf("var1 is not equal to var2\n"); //Nested if else if (var1 > var2) { printf("var1 is greater than var2\n"); } else { printf("var2 is greater than var1\n"); } } else { printf("var1 is equal to var2\n"); } return 0; } ``` 輸出: ```c Input the value of var1:12 Input the value of var2:21 var1 is not equal to var2 var2 is greater than var1 ``` ## C - `else..if`語句 當需要檢查程序中的多個條件時,`else..if`語句很有用,可以使用`else..if `語句避免嵌套`if-else`塊。 **`else..if`語法的語法:** ```c if (condition1) { //These statements would execute if the condition1 is true } else if(condition2) { //These statements would execute if the condition2 is true } else if (condition3) { //These statements would execute if the condition3 is true } . . else { //These statements would execute if all the conditions return false. } ``` ### `else..if`語句的示例 讓我們在討論嵌套的`if..else`時采用我們在上面看到的相同示例。我們將使用`else..if`語句重寫相同的程序。 ```c #include <stdio.h> int main() { int var1, var2; printf("Input the value of var1:"); scanf("%d", &var1); printf("Input the value of var2:"); scanf("%d",&var2); if (var1 !=var2) { printf("var1 is not equal to var2\n"); } else if (var1 > var2) { printf("var1 is greater than var2\n"); } else if (var2 > var1) { printf("var2 is greater than var1\n"); } else { printf("var1 is equal to var2\n"); } return 0; } ``` 輸出: ```c Input the value of var1:12 Input the value of var2:21 var1 is not equal to var2 ``` > 正如您所看到的那樣,只執行`if`正文中的語句。這是因為在該語句中,只要滿足條件,就會執行該塊內的語句,并忽略其余的塊。 重要事項: 1. `else`和`else..if`是可選語句,只有`if`語句的程序運行正常。 2. 否則,如果沒有`if`,則無法使用。 3. `if else..if`塊中可以有任意數量的`else..if`語句。 4. 如果沒有滿足任何條件,則執行`else`塊中的語句。 5. 就像關系運算符一樣,我們也可以使用邏輯運算符,如 AND(`&&`),OR(`||`)和 NOT(`!`)。
                  <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>

                              哎呀哎呀视频在线观看