[c primer] branch statement and loop statement (on)

Article directory

  • 1. Introductory statement
    • 1.1 Classification of sentences
      • 1.1.3 Control Statements
        • 1.1.3.1 Classification of control statements
          • 1.1.3.1.1 Branch statement_if statement
          • 1.1.3.1.2 Branch statement_switch statement
          • 1.1.3.1.2 Loop statement _while statement

1. Introductory statement

This article only introduces the branch statement, and the rest of the statements are expected to be updated

1.1 Classification of sentences

C statements can be divided into the following five categories
1. Expression statement
2. Function call statement
3. Control Statements
4. Compound statement
5. Empty statement

1.1.3 Control Statement

Control statements are used to control the execution flow of the program to realize various structures and methods of the program. They are composed of specific statement definers. There are nine control statements in C language.

1.1.3.1 Classification of control statements

1. Branch statement: if statement, switch statement;
2. Loop statement: do while statement, while statement, for statement;
3. Turning statement: break statement, goto statement, continue statement, return statement.

1.1.3.1.1 Branch statement_if statement

branch statement

There is no right choice in this world, we just have to work hard to make the original choice right. — Haruki Murakami

The secret to being happy is to stop thinking about everything!






































yes







no










begin









chat with friends







whither think about other things?









unhappy









happy







Grammar structure:
if(expression)
sentence;
\t
if(expression)
Statement 1;
else
Statement 2;

//Multiple branches
if(expression1)
Statement 1;
else if(expression2)
Statement 2;
else
Statement 3;
//code one
#include <stdio.h>
int main()
{<!-- -->
int age = 0;//initialization
scanf("%d", &age);
if(age < 18)
printf("Underage");
return 0;
}
//code two
#include <stdio.h>
int main()
{<!-- -->
int age = 0;
scanf("%d", & age)
if(age < 18)
printf("Underage");
else
printf("adult");
return 0;
}
//code three
#include <stdio.h>
int main()
{<!-- -->
int age = 0;
scanf("%d", &age)
if(age < 18 )
printf("Youth");
else if(age >= 18 & amp; & amp; age < 30)
printf("youth");
else if(age >= 30 & amp; & amp; age < 50)
printf("middle-aged");
else if(age >= 50 & amp; & amp; age < 80)
printf("old age");
else
printf("Shouxing");
return 0;
}

pay attention to:
if you did this please change this hobby
1. Dangling else
#include <stdio.h>
int main()
{<!-- -->
int a = 0;
int b = 2;
if(a == 1)
if(b == 2)
printf("hehe\
");
else
printf("haha\
");
return 0;
If you think this code will print, you are wrong
This is because
1. Else matching: else will match with the nearest if.
2. In C language, 0 is true and non-zero is false. If the judgment statement is false, the if statement will not be executed.
Although the above code is artificially aligned, it actually looks like this:
#include <stdio.h>
int main()
{<!-- -->
int a = 0;
int b = 2;
if(a == 1)
{<!-- -->
if(b == 2)
printf("hehe\
");
else
printf("haha\
");
}
return 0;
}
Isn't it much clearer if you look at it this way!
Therefore, in the process of typing code, we must pay attention to the use of indentation and {} to make the code more logical. Here I recommend "High Quality C/C++ Programming" for reading.
1.1.3.1.2 Branch statement_switch statement
If there are many branches, it will be particularly troublesome to use the if...else... statement. At this time, we will use the switch statement suitable for multi-branch situations.
Grammar structure:
switch (micro-shaping expression)
{
statement item;
}
Statement item:
case integer constant expression:
statement;
#include <stdio.h>
int main()
{<!-- -->
int day = 0;
scanf("%d", & amp; day);
switch(day)
{<!-- -->
case 1:
printf("Monday\
");
break;
case 2:
printf("Tuesday\
");
break;
case 3:
printf("Wednesday\
");
break;
case 4:
printf("Thursday\
");
break;
case 5:
printf("Friday\
");
break;
case 6:
printf("Saturday\
");
break;
case 7:
printf("Sunday\
");
break;
}
return 0;
}
The switch statement can be nested, and the break will only jump out of a switch.
If the value of the expression is different from the values of all case labels, the structure is that all case statements are skipped and the program continues to execute without reporting an error. If you do not want to ignore all values that do not match, you can add a default clause:
default:
1.1.3.1.2 loop statement_while statement
In life, we not only encounter choices, we often need to perform the same thing many times, and we need to repeat and repeat at this time.
The while statement was introduced in the C language to implement loops.
































unlucky













lunky










begin







search work









study hard









go to work







If you can't find a job, you should study hard!
Print the numbers from 1 to 10 on the screen
#include <stdio.h>
int main()
{<!-- -->
int num = 0;
while(num <= 9)
{<!-- -->
num + =1;
printf("%d", &num);
}
return 0;
}
Then what are the functions of break and continue in the while loop
//break code
#include <stdio.h>
int main()
{<!-- -->
int num = 0;
while(num <= 9)
{<!-- -->
num = num + 1;
if(num == 5)
break;
printf("%d",num)
}
}
If the program runs, you will find that only 1 2 3 4 is printed on the screen
this means:
As long as a break is encountered in the loop, all subsequent loops will be stopped (terminate the loop)
//continue code
#include <stdio.h>
int main()
{<!-- -->
int num = 0;
while(num <= 9)
{<!-- -->
num = num + 1;
if(num == 5)
continue;
printf("%d",num)
}
}
If the program runs, you will find that the screen prints 1 2 3 4 6 7 8 9 10
this means:
As long as continue is encountered in the loop, the following loop will not be executed (only this loop will be terminated)