如何在 C 语言中使用"else if ladder"条件语句?

cserver side programmingprogramming

else if ladder 是编写多路决策最常用的方式。

else if ladder 的语法如下。 −

if (condition1)
   stmt1;
else if (condition2)
   stmt2;
   - - - - -
   - - - - -
   else if (condition n)
      stmtn;
   else
      stmt x;

流程图

请参阅下面给出的流程图 −

示例

以下是执行 Else If 梯形条件语句的 C 程序 −

#include<stdio.h>
void main (){
   int a,b,c,d;
   printf("Enter the values of a,b,c,d: ");
   scanf("%d%d%d%d",&a,&b,&c,&d);
   if(a>b){
      printf("%d is the largest",a);
   }
   else if(b>c){
      printf("%d is the largest",b);
   }
   else if(c>d){
      printf("%d is the largest",c);
   }
   else{
      printf("%d is the largest",d);
   }
}

输出

当执行上述程序时,它会产生以下结果 −

Enter the values of a,b,c,d: 2 3 4 5
5 is the largest

相关文章