当前位置:网站首页>Suspended else problem
Suspended else problem
2022-04-22 08:51:00 【Holy meow】
In the air else problem

Let's look at a very simple piece of code , Try to find out the running result of the program :
#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 your answer is :haha, Congratulations on your wrong answer ! Don't be discouraged , Think again …; Now? , If you have thought it over, the answer is :hehe, Then I'm sure you're definitely guessing . What will be the output of this program ?

The answer is unexpected , This code does not output anything . Then why ? This is a little bit of knowledge I want to share today that you will make mistakes if you are not careful :“ In the air else problem ”. First , Did you learn C The old fellow of the language should know ,if An expression can only be followed by a clause , If the sentence contains more than one sentence , Must be enclosed in curly braces to become a Sentence block ( Because a statement block can be treated as a statement ).
if (a == 1)
if (b == 2)
printf("hehe\n");
Then I want to ask : Uppermost if(a==1) Who is the sentence after the expression ? Be careful here ,if(a==1) The clause of the expression is not if(b==2) This expression , It is :
if (b == 2)
printf("hehe\n");
because if The statement is actually a whole , therefore if The expression and the following sentence add up to a complete statement , If only if expression , That is not recognized by grammar . Now that I've thought of it , Let me ask again ,if-else Can a statement be treated as a statement ? The answer, of course, is : Sure . Now if you look back at the previous code, you will suddenly see the light , I see how it feels !!!
#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;
}
Is that so? , So simple , But why are you still wrong . There is such a mistake , The root cause is the bad code style , You read this code according to your code style , So I habitually think this else It's the one aligned with it if Matched , Thus leading to tragedy . I would like to mention one more point here , It's about else The matching of : No matter what you do if—else How to nest , Alignment misalignment , Do you like it , I else Only with the nearest if To match . So how to avoid this problem ? Then we must develop a good code style . as follows :
#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;
}
It's clear at a glance !!!

If this blog is helpful to you , Give bloggers a free like to show encouragement. Welcome to like comment collection ️, thank you !!!
If you have any questions or different opinions , Welcome to the comment area .
版权声明
本文为[Holy meow]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220749408911.html
边栏推荐
- Matlab tip: to use 'xxx function', you must authorize, install and enable the following products: XXX toolbox
- 学习总结汇总
- winkawaks1.45如何联机?winkawaks1.45怎样联机对战(其他版本类似)
- Advanced usage skills of nmap
- 重整笔记记录:【终极方法】在Vscode中用户创建自定义代码模板
- Blue Bridge Cup: dance of sine [Jav language implemented recursively]
- sql 中对varchar类型进行求和(sum)方法 (类型报错)
- 二分查找【详解】
- 简述变长数组
- VMware virtual machine communication and networking
猜你喜欢

Matlab installation product selection, how to select the products to be installed

初识C语言~分支语句

Mapbox sets the official map language to Chinese

VMware虚拟机通信与联网

安卓开发——SQLite和SQLiteDatabase应用实验6笔记

Rhel7 configuring local Yum source

RedHat7配置yum

How does CSDN reprint other people's blogs

SQL窗口函数

INSTALL_FAILED_MISSING_SHARED_LIBRARY
随机推荐
鲜为人知的“ 三字母词 ”
Disk management - raid creation
机器学习之分类回归树
CSDN如何转载文章
const修饰变量
Hyperledger Fabric1. 4 environment construction and example test
Matlab installation product selection, how to select the products to be installed
变量的作用域和生命周期
pycharm 咋下载
Android Development - SQLite and sqlitedatabase Application Experiment 6 notes
How to view port occupied in window
Fabric test example, encountered order exited (x) x seconds
The pit encountered in the compilation and installation of pinpoint of win system will be shared with you
The server is set to start automatically and regularly
什么产品都还没有 马斯克的“无聊公司”估值已高达57亿美元
idea中@Data注解,get/set方法不起作用
Advanced usage skills of nmap
PCIe learning - basic concepts of PCIe bus (6)
安卓开发——SQLite和SQLiteDatabase应用实验6笔记
INSTALL_FAILED_MISSING_SHARED_LIBRARY