Introduction: Process Control statements
1. Branch Statement (Condition Statement)
If/else (key)
Switch/case
2. Loop statements
For (important)
While
Do while
Branch statement
1> if/else
You can use the branch statement to execute different codes according to certain conditions.
If (logical expression condition 1) {// if it must appear once
Statements executed when condition 1 is met;
}
Else if (logical expression condition 2) {// or 0-N times
Statements executed when condition 2 is met;
}
Else {// otherwise 0-1 times
Statements executed when none of the preceding conditions are met
}
Exercise: enter an integer to determine whether it is a positive number, negative number, or zero.
Int a; if (a> 0) {printf ("% d is positive \ n", a);} else if (a = 0) {printf ("% d is zero \ n", a);} else {printf ("% d is negative \ n", );}
Note:
A> if/else only executes one branch. Therefore, if the first branch meets the conditions, it will not execute the subsequent branch. -> If the n branch is executed, n-1-1 branch is not valid.
B> One Of The else branch statements is required for execution, and one of the else branch statements is optional for execution.
C> If the branch has only one statement, {} can be omitted. Generally, {} is omitted when the break/continue/return Statement is executed.
D> process control statements must handle critical points.
E> it is often used to handle business needs.
# Include <stdio. h>/* use the if statement to determine whether the input date is several days in a month */int main () {printf ("Enter the year/month \ n"); int y, m, maxDay = 31; scanf ("% d", & y, & m); if (m <1 | m> 12) {printf ("illegal input \ n"); return-1 ;} if (m = 4 | m = 6 | m = 9 | m = 11) {maxDay = 30;} else if (m = 2) {maxDay = 28 + (y % 4 = 0 & y % 100! = 0 | y % 400 = 0);/* if (y % 4 = 0 & y % 100! = 0) | y % 400 = 0) maxDay = 29; else maxDay = 28; */} printf ("% d \ n", maxDay); return 0 ;}
Note:
A> if/else only executes one branch. Therefore, if the first branch meets the conditions, it will not execute the subsequent branch. -> If the n branch is executed, n-1-1 branch is not valid.
B> One Of The else branch statements is required for execution, and one of the else branch statements is optional for execution.
C> If the branch has only one statement, {} can be omitted. Generally, {} is omitted when the break/continue/return Statement is executed.
D> process control statements must handle critical points.
E> it is often used to handle business needs.
# Include <stdio. h>/* use the if statement to determine whether the input date is several days in a month */int main () {printf ("Enter the year/month \ n"); int y, m, maxDay = 31; scanf ("% d", & y, & m); if (m <1 | m> 12) {printf ("illegal input \ n"); return-1 ;} if (m = 4 | m = 6 | m = 9 | m = 11) {maxDay = 30;} else if (m = 2) {maxDay = 28 + (y % 4 = 0 & y % 100! = 0 | y % 400 = 0);/* if (y % 4 = 0 & y % 100! = 0) | y % 400 = 0) maxDay = 29; else maxDay = 28; */} printf ("% d \ n", maxDay); return 0 ;}
2> switch/case
Format:
Switch (control expression ){
Case constant expression: Statement; break;
...
Default: statement;
}
The control expression is treated as an integer. It can be a character, not a floating point or string.
Constant expressions must be constants, for example, 3 'A' 2 + 5.
Statement can be zero to multiple
Repeated branches are not allowed (values cannot be the same)
Default is not necessarily at the end, but generally at the end)
Switch execution mechanism, the statement can exit only when the break or termination occurs, otherwise it will continue to run down.
switch(a){ case 1: printf("1\n"); break; case 2: printf("2\n"); break; case 3: printf("3\n"); break; default: printf("d\n"); }
2. Loop statements
1> for Loop
Loop statements are used to repeatedly execute a code segment and exit after certain conditions (exit conditions rather than loop conditions) are met.
Syntax structure of the for Loop:
For (initialize loop variable 1; cycle condition 2; Step Statement 3 ){
Code segment to be executed repeatedly // loop body 4
}
int i; for(i=0;i<10;i++){ printf("%d\n",i); }
If the exit condition is never met, it is called an infinite loop. ctrl + c can exit the endless loop.
The simplest way to write an infinite loop: (;;){}
Supported in C99: for (int I = 0; I <10; I ++) {}, but std = c99 must be used during compilation
# Include <stdio. h>/* exercise: calculate the total score, average score, and maximum score of a student in a class. Scores need to be input, and scores do not need to be saved. Number of people: 1. Enter the number of people first and enter the score. (Use this) 2 can use-1 as the exit condition. */Int main () {int sum = 0, max = 0x80000000; // represents the smallest int integer of 1000... 0000 double avg; int I, num = 0;/* printf ("Enter the number of students \ n"); scanf ("% d", & num ); if (num <1) {printf ("illegal count \ n"); return-1 ;}for (I = 0; I <num; I ++) {int temp; scanf ("% d", & temp); sum = sum + temp; if (max <temp) max = temp ;} * // The number of times is determined before the loop. for (;) {// The number of cycles cannot be determined. Input-1 indicates that the input ends int temp; scanf ("% d ", & temp); if (temp =-1) break; // exit the loop sum = sum + temp; if (max <temp) max = temp; num ++ ;} printf ("sum = % d, avg = % lf, max = % d \ n", sum, 1.0 * sum/num, max); return 0 ;}
Break can exit the loop/switch and use return to exit the function.
Loops can be divided into: frequency determination, number uncertainty, and infinite loop.
The number of times is determined: int I; for (I = 0; I <num; I ++ ){...}
Uncertain number of times: for (;) {... if (condition) break ;...}
Infinite Loop: (;;){...}
Another loop is often nested in a loop, called multiple loops. The most commonly used is a dual loop.
Dual LoopThe difficulty lies in the control of the number of inner loops. The specific formula is as follows:
If I increments by 1
J <m * I + n; j ++
M is the number of inner loop increments. If the decrease is used, a negative number is obtained.
N is the first cycle of the inner loop.
Premise: the initial values of I and j must be the same. It is best to set them to 0.
# Include <stdio. h>/* use double for loop printing **************************/int main () {int I, j; // I control row, j control column for (I = 0; I <5; I ++) {for (j = 0; j <2 * I + 1; j ++) {printf ("*");} printf ("\ n");} return 0 ;}
# Include <stdio. h>/* print 9*9 multiplication table j * I 1*1 = 1 1*2 = 2 2*2 = 4 ...... */int main () {int I, j; for (I = 1; I <10; I ++) {for (j = 1; j <I + 1; j ++) {printf ("% d * % d = % d", j, I, j * I);} printf ("\ n ");} return 0 ;}
2> while LOOP
The while loop and the for loop can communicate with each other except the syntax.
Syntax:
Declare and initialize cyclic variables;
While (condition ){
Loop body;
Step-by-Step statement;
}
# Include <stdio. h> int main () {int sum = 0; while (1) {int temp; printf ("enter a number (0 exit) \ n "); scanf ("% d", & temp); if (temp = 0) break; sum + = temp;} printf ("sum = % d \ n", sum ); return 0 ;}
int i=0; while(i<10){ printf("%d\n",i); i++; }
While (1) {}// Infinite Loop
3> do-while loop
Do while LOOP
Syntax:
Declare and initialize cyclic variables;
Do {
Loop body;
Step-by-Step statement;
} While (condition );
The do-while loop first does and then judges the condition, so it must be executed at least once.
Int I = 10, j = 10; do {printf ("% d \ n", I); I ++;} while (I <10 ); while (j <10) {printf ("% d \ n", j); j ++;} // when I, j initially does not meet the conditions, do Loop print I, while do not print j. // When I and j initially meet the conditions, there is no difference. // In many cases, do loop and while (1) {... if () break;} can interwork.
# Include <stdio. h>/* use do-while to calculate the number of digits in the input. Note: do-while (); end with a semicolon */int main () {int num, count = 0; printf ("enter an integer \ n"); scanf ("% d", & num); do {num = num/10; count ++; if (num = 0) break;} while (1/* num! = 0 */); printf ("% d \ n", count); return 0 ;}
Note:
Break and continue
Continue is used inside the loop to end this loop and continue the next loop. If the next loop condition is not true, it will also exit.
Break is used within a loop to interrupt all loops and end the entire loop.For nested loops, break only exits the current layer loop.).
This article from the "Snow Wolf" blog, please be sure to keep this source http://wolfzhxp.blog.51cto.com/2409990/1285967