这是编写多路决策的最通用方法。
语法
请参阅下面给出的语法 –
if (condition1)stmt1;else if (condition2)stmt2;- - - - -- - - - -else if (condition n)stmtn;elsestmt x;
登录后复制
算法
参考下面给出的算法 −
STARTStep 1: Declare int variables.Step 2: Read a,b,c,d values at runtimeStep 3: i. if(a>b && a>c && a>d)Print a is largestii.else if(b>c && b>a && b>d)Print b is largestiii. else if(c>d && c>a && c>b)Print c is largestiv. elseprint d is largestSTOP
登录后复制
示例
以下是执行Else If Ladder条件运算符的C程序 −
立即学习“C语言免费学习笔记(深入)”;
实时演示
#includevoid 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 && a>c && a>d){ printf("%d is the largest",a); }else if(b>c && b>a && b>d){ printf("%d is the largest",b); }else if(c>d && c>a && c>b){ printf("%d is the largest",c); }else{ printf("%d is the largest",d); }}
登录后复制
输出
您将看到以下输出 −
Run 1:Enter the values of a,b,c,d: 2 4 6 88 is the largestRun 2: Enter the values of a,b,c,d: 23 12 56 2356 is the largest
登录后复制
考虑另一个 C 程序,它使用 else ifladder 显示学生的成绩 –
实时演示
#includeint main(){ int marks; printf("Enter the marks of a student:"); scanf("%d",&marks); if(marks = 90) printf("Grade=A"); else if(marks = 80) printf("Grade=B"); else if(marks = 70) printf("Grade=C"); else if(marks = 60) printf("Grade=D"); else if(marks 50) printf("Grade=E"); else if(marks == 50) printf("Grade=F"); else if(marks = 0) printf("Fail"); else printf("Enter a valid score between 0 and 100"); return 0;}
登录后复制
输出
您将看到以下输出 −
Run 1:Enter the marks of a student:78Grade=CRun 2:Enter the marks of a student:98Grade=A
登录后复制
以上就是在C语言中解释else-if梯形语句的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2583594.html