当前位置:网站首页>悬空else问题
悬空else问题
2022-04-22 07:50:00 【圣喵】
悬空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;
}
如果你的答案是:haha,那恭喜你答错了!别气馁嘛,再思考一下…;现在,若你已经深思熟虑后得出的答案是:hehe,那我敢保证你绝对是在瞎猜了。那究竟这段程序的输出结果会是什么呢?

答案是令人意想不到的,这段代码并不会输出任何东西。那这时为什么呢?这就是我今天想要分享的一个你一不小心就会犯错的小知识点:“悬空else问题”。首先,学过C语言的老铁们应该知道,if表达式后只能跟一条子句,若该字句中含有多条语句,则必须用大括号括起来变成一个语句块(因为语句块可以被当成一条语句)。
if (a == 1)
if (b == 2)
printf("hehe\n");
那么我想问一下:最上面的if(a==1)表达式后的字句是谁呢?在这里千万注意,if(a==1)表达式的子句可不是if(b==2)这个表达式啊,而是:
if (b == 2)
printf("hehe\n");
因为if语句其实是一个整体,所以if表达式和后面的那条字句加起来才是一条完整的语句,若仅仅只有if表达式,那是不被语法所认可的。既然已经想到这了,我再追问一下,if-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是和与之对齐的那个if所匹配,从而导致悲剧的发生。在这我还要提一点,是关于else的匹配:不管你if—else是如何嵌套使用,对不对齐,你看的顺不顺眼,我else只会和离得最近的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;
}
这样不就一目了然了嘛!!!

这份博客如果对你有帮助,给博主一个免费的点赞以示鼓励欢迎各位点赞评论收藏️,谢谢!!!
如果有什么疑问或不同的见解,欢迎评论区留言欧。
版权声明
本文为[圣喵]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_66769266/article/details/124145982
边栏推荐
- PCIe学习-PCIe总线的基本概念(六)
- Learning summary summary
- HyperLedger Explorer 0.3.9环境搭建
- Matlab tip: to use 'xxx function', you must authorize, install and enable the following products: XXX toolbox
- navicat连接oracle数据库失败:cannot load OCI DLL,87:Instant Client package is ...
- "Unexpected" operation in SQL statement
- ROM, Ram, SRAM, DRAM, flash, SDRAM Difference
- 111. Minimum depth of binary tree
- 简述变长数组
- Reorganize notes: [Ultimate method] create user-defined code templates in vscade
猜你喜欢

Elastic job installation deployment access

Rhel7 - process management
![Reorganize notes: [Ultimate method] create user-defined code templates in vscade](/img/82/745bdc43c0a1191db1b2a13e5091d0.png)
Reorganize notes: [Ultimate method] create user-defined code templates in vscade

PCIe学习-PCIe总线体系结构入门:事务层-数据链路层-物理层(八)

Redhat7 configuration yum

CSDN怎么转载别人的博客

Action inattendue dans une instruction SQL

素数求解的N种境界

Scanf / sprintf, fscanf / fprintf, sscanf / sprintf, snprintf of C language

Explain escape character
随机推荐
初识C语言~分支语句
简述变长数组
Rhel7 configuring local Yum source
Dynamic memory management of C
617. Merge binary tree (easy)
Establishment of golang environment
mysql插入自由列
RedHat7配置yum
require-dev.mikey179/vfsStream is invalid, it should not contain uppercase characters. Please use m
内存地址的概念与理解
Linux(CentOS)下安装 PostgreSQL
Sum method for varchar type in SQL (type error)
mysql导出表的具体字段信息(Navicat客户端详细操作)
Shell notes
Cesium loads terrain data (cesiumlab makes terrain data), from source data to terrain service
微信小程序手机号码如何进行解密
服务器设置自动开机及定时开机
CSDN如何转载文章
数组传参的本质
POI operation excel three swordsman