总结
豆包 AI 助手文章总结
首页 > 后端开发 > C++ > 正文

在C语言中解释else-if梯形语句

WBOY
发布: 2023-09-05 18:09:07
转载
1554人浏览过

这是编写多路决策的最通用方法。

语法

请参阅下面给出的语法 -

if (condition1)
stmt1;
else if (condition2)
stmt2;
- - - - -
- - - - -
else if (condition n)
stmtn;
else
stmt x;
登录后复制

在C语言中解释else-if梯形语句

算法

参考下面给出的算法 −

START
Step 1: Declare int variables.
Step 2: Read a,b,c,d values at runtime
Step 3: i. if(a>b && a>c && a>d)
Print a is largest
ii.else if(b>c && b>a && b>d)
Print b is largest
iii. else if(c>d && c>a && c>b)
Print c is largest
iv. else
print d is largest
STOP
登录后复制

示例

以下是执行Else If Ladder条件运算符的C程序 −

立即学习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 && 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 8
8 is the largest
Run 2: Enter the values of a,b,c,d: 23 12 56 23
56 is the largest
登录后复制

考虑另一个 C 程序,它使用 else ifladder 显示学生的成绩 -

 实时演示

#include<stdio.h>
int main(){
   int marks;
   printf("Enter the marks of a student:</p><p>");
   scanf("%d",&marks);
   if(marks <=100 && marks >= 90)
      printf("Grade=A");
   else if(marks < 90 && marks>= 80)
      printf("Grade=B");
   else if(marks < 80 && marks >= 70)
      printf("Grade=C");
   else if(marks < 70 && marks >= 60)
      printf("Grade=D");
   else if(marks < 60 && marks > 50)
      printf("Grade=E");
   else if(marks == 50)
      printf("Grade=F");
   else if(marks < 50 && marks >= 0)
      printf("Fail");
   else
      printf("Enter a valid score between 0 and 100");
   return 0;
}
登录后复制

输出

您将看到以下输出 −

Run 1:
Enter the marks of a student:78
Grade=C
Run 2:
Enter the marks of a student:98
Grade=A
登录后复制

以上就是在C语言中解释else-if梯形语句的详细内容,更多请关注php中文网其它相关文章!

C语言速学教程(入门到精通)
C语言速学教程(入门到精通)

C语言怎么学习?C语言怎么入门?C语言在哪学?C语言怎么学才快?不用担心,这里为大家提供了C语言速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
豆包 AI 助手文章总结
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号