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
广告
© . All rights reserved.