当前位置:网站首页>初阶C语言 - 分支语句(if、switch)
初阶C语言 - 分支语句(if、switch)
2022-07-25 23:57:00 【渡上舟(每天都要坚持学习鸭)】
目录
1. 分支语句
1.1 if语句
1.1.1 if 语句的语法结构:
- 多条语句要使用 { }
- 分支语句只会走其中一个语句
- else if 的数量是不限的
- 最后一个else是可以省略的
1.1.2 悬空else
#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;
} 
else的匹配:else是和它离的最近的if匹配的
if...else...合起来是一条语句
所以不会输出hehe
代码这样改才会输出hehe
(放一条空语句在if语句后面,这样就是两个地位相等的if语句啦)
#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;
} 
可见,这样的代码极易让人产生误会,
我们需要有好的代码风格:该对齐就要对齐,该带{ }就要带{ }。
#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;
} 
1.2 switch 语句(常用于多分支)
1.2.1 switch 语句的语法结构:
当然,整型常量表达式是字符常量表达式也是可以的,字符在内存中存的是ASCII码值,ASCII码值是整型。
#include <stdio.h>
int main()
{
char day = 0;
scanf("%c", &day);
switch(day)
{
case 'a':
case 'b':
case 'c':
printf("小写字母\n");
break;
case 'A':
case 'B':
case 'C':
printf("大写字母\n");
break;
default:
break;
}
return 0;
} 
1.2.2 switch 是如何工作的:
首先,switch根据switch后面表达式的值决定它匹配哪个case语句,就从哪个case语句进去
进去之后,按顺序执行,直到遇到break,跳出switch语句。
case决定入口,break决定出口。没有break,就会一直往下执行,直到遇到break;
#include <stdio.h>
int main()
{
int day = 0;
scanf("%d", &day);
switch (day)
{
case 1:
case 2:
case 3:
case 4:
case 5:
printf("weekday\n");
break;
case 6:
case 7:
printf("weekend\n");
break;
default:
printf("选择错误\n");
break;
}
return 0;
}
1.2.3 switch 语句可以嵌套使用
- switch语句可以嵌套switch语句,也可以嵌套if语句
- break只能跳出自己所在的switch语句
#include <stdio.h>
int main()
{
int n = 1;
int m = 2;
switch (n)
{
case 1:
m++;
case 2:
n++;
case 3:
switch (n)
{//switch允许嵌套使用
case 1:
n++;
case 2:
m++;
n++;
break;
}
case 4:
m++;
break;
default:
break;
}
printf("m = %d, n = %d\n", m, n);
return 0;
}
1.2.4 default
- 当 switch 表达式的值并不匹配所有 case 标签的值时,这个 default 子句后面的语句就会执行
- 每个switch语句中只能出现一条default子句
- default可以出现在语句列表的任何位置,按顺序执行,直到遇到break 跳出switch语句。
- default语句后注意有没有加break
- default位置一般推荐最后
综上,其实,default 和 case语句 的执行大差不差。都是入口,然后遇到break才会跳出switch语句,否则就一直往下执行。

边栏推荐
- SQLZOO——Nobel Quiz
- 二叉树——226. 翻转二叉树
- Android solves the risk of database injection vulnerability
- [learning notes] unreal 4 engine introduction (III)
- [learning notes] unreal 4 engine introduction (IV)
- Responsibility chain model of behavioral model
- Part 74: overview of machine learning optimization methods and superparameter settings
- The items of listview will be displayed completely after expansion
- The expression of flag=false if (flag) {return} timer=null if (timer) {return} in the throttle valve has been unclear
- 2022-07-18 study notes of group 5 self-cultivation class (every day)
猜你喜欢
随机推荐
Imitating the magnifying glass effect of JD products -- JS Foundation
How does JS judge whether the current date is within a certain range
Reduce method of array
What is parity? How to use C language?
Optimize the browsing experience of yandere/konachan site with user scripts
C语言实战之猜拳游戏
Get the data of Mafeng Hotel
行为型模式之观察者模式
Exercise (2) create a set to store the elements "1", "$", "2", "$", "3", "$", "4"“
Lua script Wireshark plug-in to resolve third-party private protocols
The expression of flag=false if (flag) {return} timer=null if (timer) {return} in the throttle valve has been unclear
Leetcode 0919. complete binary tree inserter: array representation of complete binary tree
Fixed and alternate sequential execution of modes
多御安全浏览器手机版将增加新功能,使用户浏览更个性化
二叉树——222. 完全二叉树的节点个数
十大排序之快速排序
抽丝剥茧C语言(高阶)程序环境和预处理
[learning notes] unreal 4 engine introduction (IV)
栈与队列——239. 滑动窗口最大值
Part 74: overview of machine learning optimization methods and superparameter settings









