当前位置:网站首页>分支 if语句
分支 if语句
2022-04-21 11:45:00 【wx6235761444879】
什么是语句呢?
int
a
=
0;
//用英文分号隔开的一段代码就是一个语句
;
//空语句
1
+
2;
- 1.
- 2.
- 3.
那么if 语句的结构是什么呢?
1.
if(
表达式)
语句;
2.
if(
表达式)
语句;
else
语句;
3.
if(
表达式1)
语句;
else
if(
表达式2)
语句;
else
语句;
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
看基础的结构,我们来写一段代码吧!
int
main()
{
if(
age
<
18)
{
printf(
"未成年\n");
}
else
if(
age
>=
18
&&
age
<=
30)
//&&表示并且
printf(
"成年\n");
else
if(
age
>
30
&&
age
<=
60)
printf(
"中年\n");
else
printf(
"老年\n");
return
0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
上面这段代码是结构三的形式,那么这里也可以用上结构二来编写也是可以的
int
main()
{
if(
age
<
18)
{
printf(
"未成年\n");
}
else
{
if(
age
>=
18
&&
age
<=
30)
//&&表示并且
printf(
"成年\n");
else
if(
age
>
30
&&
age
<=
60)
printf(
"中年\n");
else
printf(
"老年\n");
}
return
0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
重点来了哦,else这个关键字到底是怎么运行的呢?下面我们就讲一讲悬空else来理解else 的运行
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.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
猜猜代码输出结果为什么呢?
输出结果什么都没有
那这是为什么呢?
原因很简单了,不要想那么复杂呐。原因就是else和最近的if 匹配
来,我们来一起看一下代码怎么运行

我们 理解了悬空else ,那么这段代码怎么去修改呢?
int
main()
{
int
a
=
0;
int
b
=
2;
if(
a
==
1)
{
if(
b
==
2)
printf(
"hehe\n");
else
printf(
"haha\n");
}
return
0;
}
//当然这个结果也是什么都没有 我们到这里就明白了悬空else
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
总结:
悬空else是和最近的if 进行匹配
这里我们来几道题目
int
main()
{
int
sum
=
4;
if(
sum
=
5)
printf(
"hehe\n");
return
0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
下面先思考一下结果是多少
结果是hehe
你真的清楚这段代码吗?(作为初学者,我一眼上去就错了)
我来讲讲这段代码怎么运行的

注意:符号= 表示赋值 ,不是判断
现在我们再来了解一道题目
打印出1-100的所有奇数
int
main()
{
int
i
=
1;
while(
i
<=
100)
//while 表示循环
{
if(
i
%
2
==
1)
//%表示模
{
printf(
"%d ",
i);
}
i
++;
}
return
0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
这里while表示循环
这段代码不难,简单。来,走

也可以这样来写
int
main()
{
int
i
=
1;
while(
i
<=
100)
{
printf(
"%d ",
i);
i
+=
2;
//i+=2 就是i=i+2;
}
return
0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
这就是if分支,简单理解,深刻吸收
重点就是悬空else和一些关键符号的理解
感谢观看!
版权声明
本文为[wx6235761444879]所创,转载请带上原文链接,感谢
https://blog.51cto.com/u_15555633/5235095
边栏推荐
- redis面试问题
- 星汉未来云原生基础治理平台SchedulX V1.1.0 重磅发布,助力企业降本增效
- ES6新特性(6)之箭头函数/Class类
- A small game of guessing numbers
- 5.4万Star全部归零,项目作者:十分后悔
- Kubernetes详解(二)——Kubernetes结构与资源对象
- L3-028 Sensen tourism (30 points) (Dijkstra + reverse drawing + details)
- C language: pointer 2 (linear table knowledge + detailed explanation of examples)
- 剖析GPU未来发展方向
- Realize browser multi tab communication
猜你喜欢

L2-005 set similarity (25 points) (Set + tolerance and exclusion)

File transfer (upload and download)

Introduction to Alibaba's super large-scale Flink cluster operation and maintenance system

Reading material: Information Technology Yearbook

循环队列的长度「In DataStructure」

L2-005 集合相似度 (25 分)(set+容斥)

A small game of guessing numbers

分享 Map 对象和普通对象的 7 个区别

ASP. Net core to realize JWT authorization and authentication (1. Theory)

leaflet军事标绘-突击方向修改(leaflet篇.90)
随机推荐
Sentinelsat package introduction
InfoQ 入驻快讯
NCURSES 安装包,以及pkg-config信息
Write array dictionary to CSV format
【招聘测评题】中的(行测)图形推理题基本逻辑总结(附例题)
Xinghan will become the co construction unit of finops industry promotion matrix in the future
File transfer (upload and download)
org.apache.flink.client.deployment.ClusterDeploymentException: Could not deploy Yarn job cluster.
org. apache. flink. client. deployment. ClusterDeploymentException: Could not deploy Yarn job cluster.
IDEA的LeetCode力扣插件设置
Xinghan future cloud native basic governance platform schedulx v1 1.0 heavy release to help enterprises reduce costs and increase efficiency
sentinelsat包介绍
MQ相关流程及各项内容
ASP.NET Core实现JWT授权与认证(1.理论篇)
Get and post requests
stm32i2c的解答
Solution to the display of the name of xftp file
【Flutter 专题】89 图解基本 Overlay 悬浮新手引导 #yyds干货盘点#
如何求源码,反码,补码
LC刷题第四天