当前位置:网站首页>C language selection type supplement
C language selection type supplement
2022-06-21 14:41:00 【Yangasang 815】
Last time we talked about c In language if,else if ,else,switch Use of statements ,
Although more comprehensive , But there are still deficiencies , So I will do a little this time Add .
Let's start with grammar ,
Grammatical structure :
if( expression ) sentence ;
if( expression ) sentence 1;else sentence 2;
if( expression ) sentence 1;else if sentence 2;else sentence 3;
expression coca && || !( or And Not )
If the expression holds And execute multiple statements : Use -{} Generate code blocks -> { sentence };
Then let's talk about Dangling code The problem of :
For example, the following two codes :
int main() {
int a = 0;
int b = 2;
if (a == 1)
if (b == 2)
printf("hehe\n");
else printf("haha\n");
return 0;// Nothing there?
}
int main() {
int a = 0;
int b = 2;
if (a == 1)
if (b == 2)
printf("hehe\n");
else
printf("haha\n");
return 0;// Still not
}
// The second code puts else With the first if alignment , Try with the first if Form a selection statement , however , It's not right ,
// The real situation of the code is that of the first code , It's the second one. if And else Form statement ,
// In this code , Structure in order , From the top down , In judging a Is it equal to 1 when , because a=0, So the statement doesn't hold ,
// So the statement will not run any further , So nothing is printed .
// change a Value ; Make it established , as follows ;
int main() {
int a = 0;
int b = 2;
if (a == 0)
if (b == 2)
printf("hehe\n");// Print hehe
else printf("haha\n");
return 0;
}
// Conclusion :else There is no match with the nearest one if Statement matching
The third point , about Optimization of writing method .
//if Contrast of writing forms :
// Code 1:
if (condition) {
return x;
}
return y; The code is established : Conditions established return x, Don't set up , return y.
// But the code is not readable ,
// Can be optimized downwards :
// Code 2:
if (condition) {
return x;
}
else {
return y;
}
And finally , About switch sentence .
switch sentence : It has to be integer ,case Must be followed by a constant . Start where the integer constant expression satisfies .
/*switch( Integer expression ){
Statement item
}*/
// About statement items :
//case Integral constant expression : sentence ;break;
// default : sentence ;( except case Other conditions )
Special form :
/*int main() {
int s;
scanf("%d", &s);
switch (s) {
case 1:
case 2:
printf("nihao");
break;// A kind of
case 3:
case 4:
printf("buhao");
break;// A kind of
}
return 0;
}*/
This kind of writing is OK .
//case And default There is no order . And there can be if sentence , Allow nesting
边栏推荐
- Reptile Foundation_ Requests Library
- Pyqt environment in pycharm
- What fun things can a desk service do
- Use OpenCV to decompose the video into single frame pictures and synthesize the video with pictures
- Qt-8- use SQL database
- Pyqt5 learning notes of orange_ Connect to SQL Server database
- Understand the use of protobuf serialization protocol
- Chapter 3 - data link layer
- Write a code hot deployment
- UBI error: ubi_ read_ volume_ table: the layout volume was not found
猜你喜欢

What fun things can a desk service do

Teach you how to design interface automation test cases: extract interface information and analyze it

Chapter 2 - physical layer (I)

Taobao secsha plug-in

2022 Fujian latest fire protection facility operator simulation test question bank and answers

Make word2vec for Bert model

Detailed explanation of dynamic planning

2022 Hunan latest fire facility operator simulation test question bank and answers

P24 de noise

Write a code hot deployment
随机推荐
Summary of common libraries in machine learning
Chapter 5 - application layer
Never change
Chapter 3 - data link layer
Use Matplotlib to draw the first figure
Application GDB debugging
Make word2vec for Bert model
Use OpenCV to decompose the video into single frame pictures and synthesize the video with pictures
Pyqt5 learning notes of orange_ Connect to SQL Server database
UBI error: ubi_ read_ volume_ table: the layout volume was not found
JS written test question: asynchronous
. bash_ profile
So many statistical charts? This visualizer is great~~
Exit() function, macro exit_ Success, macro exit_ Difference between failure, exit() and return
Why is epoll efficient?
Two of my essays
Redis cluster deployment manual that Xiaobai can understand
Subshell
C language function fgets
Calculate subtotal of advertising business