if else和switch case
来源:互联网 发布:浙江省数据开放平台 编辑:程序博客网 时间:2024/06/09 14:22
if else和switch case
if else和switch为选择结构语句,switch又称为多分支选择语句。
if else和switch哪个执行效率高?
if(判断语句)逐个进行判断,将满足条件的值输出,switch可以直接跳转到相应的case中,相当于存在一个case表,可以实现jump table。用花费空间资源来赢取时间,因此switch语句效率高,当选择分析较多时,效果更明显。但case后面只能跟常量(常量表达式),并不是任何时候 都适用,因此if else灵活性更高。
switch中的break?
break是switch中的循环结束语句,当跳转到相应的case中,直到遇到break或default程序将会结束,case代表的值都是不相同的,为了让一部分不同的case执行相同的结果,后面不写break就可以实现。
相关代码:
#include<stdio.h>
void main()
{
int c;
float grade;
printf("请输入一个百分制分数:\n");
scanf("%f",&grade);
printf("你的等级为:\n");
if(grade>90)
{
c=9;
}
else
{
c=grade/10;
}
switch(c)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5: printf("'E'\n");break;
case 6: printf("'D'\n");break;
case 7: printf("'C'\n");break;
case 8: printf("'B'\n");break;
case 9: printf("'A'\n");break;
}
}
#include<stdio.h>
void main()
{
int t,f,a,b,c,d,e;
printf("请输入一个不多于五位的正整数:\n");
scanf("%d",&t);
printf("这个数为几位数:\n");
if(t>=10000)
{
f=5,a=t/10000,b=(t%10000)/1000,c=(t%1000)/100,d=(t%100)/10,e=t%10;
printf("这个数为%d位数\n",f);
printf("这个数的每一位分别为:\n%d\n%d\n%d\n%d\n%d\n",a,b,c,d,e);
printf("这个数的逆序为:\n%d\n%d\n%d\n%d\n%d\n",e,d,c,b,a);
}
else if(t<10000&&t>1000)
{
f=4,b=(t%10000)/1000,c=(t%1000)/100,d=(t%100)/10,e=t%10;
printf("这个数为%d位数\n",f);
printf("这个数的每一位分别为:\n%d\n%d\n%d\n%d\n",b,c,d,e);
printf("这个数的逆序为:\n%d\n%d\n%d\n%d\n",e,d,c,b);
}
else if(t<1000&&t>=100)
{
f=3,c=(t%1000)/100,d=(t%100)/10,e=t%10;
printf("这个数为%d位数\n",f);
printf("这个数的每一位分别为:\n%d\n%d\n%d\n",c,d,e);
printf("这个数的逆序为:\n%d\n%d\n%d\n",e,d,c);
}
else if(t<100&&t>=10)
{
f=2,d=(t%100)/10,e=t%10;
printf("这个数为%d位数\n",f);
printf("这个数的每一位分别为:\n%d\n%d\n",d,e);
printf("这个数的逆序为:\n%d\n%d\n",e,d);
}
else
{
f=1,e=t%10;
printf("这个数为%d位数\n",f);
printf("这个数的每一位分别为:\n%d\n",e);
printf("这个数的逆序为:\n%d\n",e);
}
}
- if...else和switch..case
- if else和switch case
- 优化if else 和 switch case 结构
- switch...case和if...else效率比较
- 分支结构 if...else和switch...case
- switch...case和if...else效率比较
- 判断语句switch - case 和if - else
- switch...case和if...else效率比较
- switch...case...和if...else...区别
- if.....else if .....else 与switch....case.....
- if...else if...和switch...case...执行效率
- switch case 与 if else
- if else 与 switch case
- C#中的分支结构:if..else if 、if... else 和 switch ...case语句的区别
- switch case 和 if else效率的比较
- 使用面向对象技术替代switch-case和if-else
- PHP下switch-case 和 if-else效率的比较
- 用数组代替if-else和switch-case语句
- 数据库的创建
- ASCII中LF与CR区别(换行符)
- 2017.10.21 C组比赛总结
- JavaEE常用API【Date、DateFormat、Calendar、Math、System、包装类、正则表达式】总结
- python-requests库的基本使用教程
- if else和switch case
- 最近一次给民营企业的咨询的感想
- 【状压DP】UOJ#265 【NOIP2016】愤怒的小鸟
- Android兼容性设计
- Ubuntu16.04中array ssl vpn安装及使用
- 图像分割学习笔记_1(opencv自带meanshift分割例子)
- angular标签的显示和隐藏
- POJ 2533 Longest Ordered Subsequence 动态规划
- ToolBar的使用