C語言巢狀if語句



C語言程式設計中,巢狀if-else語句始終是合法的,這意味著你可以在另一個ifelse-if語句內部使用一個ifelse-if語句。

在程式設計環境中,“巢狀”是指將一個特定的程式設計元素包含在另一個類似的元素內部。例如,巢狀迴圈、巢狀結構、巢狀條件語句等。如果C語言中的if語句用在另一個if語句內部,那麼我們稱之為C語言中的巢狀if語句

語法

巢狀if語句的語法如下:

if (expr1){
   if (expr2){
      block to be executed when 
      expr1 and expr2 are true
   }
   else{
      block to be executed when 
      expr1 is true but expr2 is false
   }        
}

下圖表示if語句的巢狀:

Nested Condition

你可以用&&||組合布林表示式來達到同樣的效果。但是,對於更復雜的演算法,其中有多個布林表示式的不同組合,構建複合條件就變得困難了。相反,建議使用巢狀結構。

另一個if語句可以出現在頂級if塊中,或者它的else塊中,或者同時出現在兩者中。

示例1

讓我們舉個例子,程式需要確定給定的數字是否小於100,在100到200之間,或大於200。我們可以用下面的複合布林表示式來表達這種邏輯:

#include <stdio.h>
 
int main (){

   // local variable definition
   int a = 274;
   printf("Value of a is : %d\n", a);

   if (a < 100){
      printf("Value of a is less than 100\n");
   }
   
   if (a >= 100 && a < 200){
      printf("Value of a is between 100 and 200\n" );
   }
      
   if (a >= 200){
      printf("Value of a is more than 200\n" );
   }
}   

輸出

執行程式碼並檢查其輸出。在這裡,我們已將“a”的值初始化為274。更改此值並再次執行程式碼。如果提供的數值小於100,則會得到不同的輸出。同樣,如果提供的數字在100到200之間,輸出也會再次改變。

Value of a is : 274
Value of a is more than 200

示例2

現在讓我們對同一個問題使用巢狀條件。當我們使用巢狀條件時,它將使解決方案更容易理解。

首先,檢查“a >= 100”。在if語句的真值部分,檢查它是否<200以決定數字是否在100-200之間,或者它是否>200。如果第一個條件 (a >= 100) 為假,則表示該數字小於100。

#include <stdio.h>
 
int main (){

   // local variable definition
   // check with different values 120, 250 and 74
   int a = 120;
   printf("value of a is : %d\n", a );

   // check the boolean condition
   if(a >= 100){
   
      // this will check if a is between 100-200
      if(a < 200){
         // if the condition is true, then print the following
         printf("Value of a is between 100 and 200\n" );
      }
      else{
          printf("Value of a is more than 200\n");
      }
   }
   else{ 
       // executed if a < 100
       printf("Value of a is less than 100\n");
   }

   return 0;
}

輸出

執行程式碼並檢查其輸出。對於不同的“a”輸入值,你會得到不同的輸出:

Value of a is : 120
Value of a is between 100 and 200

示例3

下面的程式使用巢狀if語句來確定一個數字是否能被2和3整除,能被2整除但不能被3整除,能被3整除但不能被2整除,以及不能同時被2和3整除。

#include <stdio.h>

int main(){
   int a = 15;
   printf("a: %d\n", a);

   if (a % 2 == 0) {
      if (a % 3 == 0){
         printf("Divisible by 2 and 3");
      }
      else {
         printf("Divisible by 2 but not 3");
      }
   }
   else {
      if (a % 3 == 0){
         printf("Divisible by 3 but not 2");
      }
      else{
         printf("Not divisible by 2, not divisible by 3");
      }
   }

   return 0;
}

輸出

執行程式碼並檢查其輸出:

a: 15
Divisible by 3 but not 2

對於不同的“a”值,你會得到不同的輸出。

示例4

下面是一個C程式,用於檢查給定的年份是否是閏年。年份是否是閏年由以下規則決定:

  • 年份能被4整除嗎?
  • 如果是,它是世紀年(能被100整除)嗎?
  • 如果是,它能被400整除嗎?如果可以,它是閏年,否則不是。
  • 如果它能被4整除但不是世紀年,它是閏年。
  • 如果它不能被4整除,它不是閏年。

這是C程式碼:

#include <stdio.h>

int main(){

   // Test the program with the values 1900, 2023, 2000, 2012
   int year = 1900; 
   printf("year: %d\n", year);
   
   // is divisible by 4?
   if (year % 4 == 0){
      
      // is divisible by 100?
      if (year % 100 == 0){
         
         // is divisible by 400?
         if(year % 400 == 0){
            printf("%d is a Leap Year\n", year);
         }
         else{
            printf("%d is not a Leap Year\n", year);
         }
      }
      else{
         printf("%d is not a Leap Year\n", year);
      }
   }
   else{
      printf("%d is a Leap Year\n", year);
   }
}

輸出

執行程式碼並檢查其輸出:

year: 1900
1900 is not a Leap Year

使用變數“year”的不同值(如1900、2023、2000、2012)測試程式。

可以使用複合布林表示式代替巢狀if語句來達到相同的結果,如下所示:

If (year % 4 == 0 && (year % 400 == 0 || year % 100 != 0)){
   printf("%d is a leap year", year);
}
else{
   printf("%d is not a leap year", year);
}

使用C語言中的巢狀if語句,我們可以編寫結構化和多層決策演算法。它們簡化了複雜判別邏輯情況的編碼。巢狀也使程式更易於閱讀和理解。

c_decision_making.htm
廣告