微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

Else - 如果梯子是编写多路决策的最通用方式。

else if 梯子的语法如下 -

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

流程图

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

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

示例

以下是执行 Else If Ladder 条件语句的 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

以上就是如何在C语言中使用“else if ladder”条件语句?的详细内容,更多请关注编程之家其它相关文章

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐