当前位置:网站首页>十分钟教你玩转分支语句!!!!!小白速进,新手福利!!
十分钟教你玩转分支语句!!!!!小白速进,新手福利!!
2022-07-31 05:17:00 【乐言..】
前言:内容较为精简,力求迅速解决,重点标红。
目录
课后小练习:如果你想要更上一层楼,这里有两个小练习,私信给答案!!!人在的话会进行讲解。期待三连
一:什么分支语句?
分支语句是一种做出选择的代码例如你口袋里有5块钱,你想吃梨子或者西瓜,这时一个奸商来了,梨子4块一个,西瓜100块一个,你将如何选择?当我们遇到这种类似情况是,我们会选择分支语句。
二:分支语句的使用以及注意点!
1)if语句
//语法结构:
if(表达式)
语句;
//第二种情况
if(表达式)
语句1;
else
语句2;
//多分支时:
if(表达式1)
语句1;
else if(表达式2)
语句2;
else
语句3;
//如果分支还要再多,可以大量使用else if,但是最后一定要放else
一个例子:好好学习可以走上人生巅峰
不好好学习回家种地
代码如下:
#include<stdio.h>
int main()
{
//a表示好好学习,b表示不好好学习。
int c=0;
int a=0;
int b=0;
scanf(“%d”,&c)
if(c=a)
{
printf("迎娶白富美");
}
else(c=b)
{
printf("回家种地”);
}
return 0;
注意):else只会与最近的if匹配,注意书写格式否则可能出现如下情况:
#include <stdio.h>
int main()
{
int a = 0;
int b = 2;
if(a == 1)
if(b == 2)//xxxxx
printf("hehe\n");
else//xxxxxx
printf("haha\n");
return 0;
}
此时编译器可能会发生错误,两个xxxx的代码会相互匹配,导致出错,改正如下:
#include <stdio.h>
int main()
{
int a = 0;
int b = 2;
if(a == 1)
{
if(b == 2)
{
printf("hehe\n");
}
}
else
{
printf("haha\n");
}
return 0;
}
注意代码的风格,对于一个程序员来说很重要,要从一开始培养!!!
下面列出几种可能的if语句书写形式:
//代码1
if (condition) {
return x;
}
return y;
//代码2
if(condition)
{
return x;
}
else
{
return y;
}
//代码3
int num = 1;
if(num == 5)
{
printf("hehe\n");
}
//代码4
int num = 1;
if(5 == num)
{
printf("hehe\n");
}
此时代码四比代码二好得多,不容易出错!!!!笔者建议各位模范代码四;
2)switch语句
适用范围:当分支过多时,如果使用if语句,那么将会出现一大堆的else if,导致代码过于冗长,可读性下降,这时switch语句就派上了用场!!
switch(整型表达式)
{
语句项;
}
此处语句项为case语句如下
case 整形常量表达式:
语句;
1:break语句
当我们选择了某一项之后,我们可能需要跳出语句块,不然语句可能一直执行下去,达不到我们想要的效果。
例如星期几:
#include <stdio.h>
int main()
{
int day = 0;
switch(day)
{
case 1:
printf("星期一\n");
break;
case 2:
printf("星期二\n");
break;
case 3:
printf("星期三\n");
break;
case 4:
printf("星期四\n");
break;
case 5:
printf("星期五\n");
break;
case 6:
printf("星期六\n");
break;
case 7:
printf("星期天\n");
break;
}
return 0;
}
当我们选择了情况之后,需要用break语句跳出switch语句,否则全部都会打印!!!!
为了养成良好的编程习惯,我们需要在每一个case后加上break语句,以防日后的bug
2.default语句:
当我们的想要表达的值与case中毫无匹配项,也就是case里面没有,那么就会用default来描述
编程好习惯 :在每个 switch 语句中都放一条default子句是个好习惯,甚至可以在后边再加一个 break 。
课后小练习:如果你想要更上一层楼,这里有两个小练习,私信练习,需要答案按私信答案,人在的话会进行讲解。期待三连
如果有什么不对的地方,欢迎各位大佬指正,本人感激不尽。
边栏推荐
- When solving background-size:cover, the picture is covered but not displayed completely?
- pyspark.ml feature transformation module
- Research reagents Cholesterol-PEG-Maleimide, CLS-PEG-MAL, Cholesterol-PEG-Maleimide
- CAS:474922-22-0 Maleimide-PEG-DSPE 磷脂-聚乙二醇-马来酰亚胺简述
- 我的训练函数模板(动态修改学习率、参数初始化、优化器选择)
- np.fliplr与np.flipud
- Fluorescein-PEG-DSPE Phospholipid-Polyethylene Glycol-Fluorescein Fluorescent Phospholipid PEG Derivatives
- Natural language processing related list
- Multi-Modal Face Anti-Spoofing Based on Central Difference Networks学习笔记
- Where can I find the private files set by myself?
猜你喜欢
wangeditor富文本编辑器上传图片以及跨域问题解决
Cholesterol-PEG-Acid CLS-PEG-COOH Cholesterol-Polyethylene Glycol-Carboxyl Modified Peptides
crontab timing operation
【Latex】TexLive+VScode+SumatraPDF 配置LaTex编辑环境
Multi-Modal Face Anti-Spoofing Based on Central Difference Networks学习笔记
box-shadow相关属性
2022年SQL大厂高频实战面试题(详细解析)
DC-CDN学习笔记
ImportError: cannot import name ‘Xxxx‘ from partially initialized module ‘xx.xx.xx‘
[Solved] ssh connection report: Bad owner or permissions on C:\\Users/XXX/.ssh/config
随机推荐
Talking about the understanding of CAP in distributed mode
crontab的定时操作
关于Iframe
Cholesterol-PEG-Acid CLS-PEG-COOH 胆固醇-聚乙二醇-羧基修饰肽类化合物
The browser looks for events bound or listened to by js
[已解决]ssh连接报:Bad owner or permissions on C:\\Users/XXX/.ssh/config
活体检测CDCN学习笔记
会话和饼干,令牌
The content of the wangeditor editor is transferred to the background server for storage
Learning and understanding of ROS service programming
Log jar package conflict, and its solution
变分自编码器VAE实现MNIST数据集生成by Pytorch
Three methods of accessing image pixels in opencv
2022 SQL big factory high-frequency practical interview questions (detailed analysis)
【Latex】TexLive+VScode+SumatraPDF 配置LaTex编辑环境
cocos2d-x implements cross-platform directory traversal
MySQL 入门:Case 语句很好用
四种常见的POST提交数据方式
Tensorflow steps on the pit while using it
Sourcery插件(自动提升代码质量)