By default, the C program runs from top to bottom, starting from the starting position of main (), and executes statements one by one until the end of the main () function.
However, this is rare in actual situations. The C language provides various program control statements to control the execution order of programs.
What are the process control statements?
If statement.
For statement.
While statement.
Do... while statement.
1. for statement
A for statement is a C programming structure that executes a specific number of times a code block consisting of one or more statements. It is also called
Loop, because the program usually executes this statement multiple times.
For statement structure:
For (initial; condition; increment)
{
Statement;
}
Initial, condition, and increment are expressions, while statement is a single or compound statement.
When the program runs the for statement, the following events occur:
A. Execute the expression initial. Initial is usually a value assignment statement that sets a variable to a specific value.
B. determine the condition. Condition is usually a relational expression.
If the condition is false (equal to 0), the for statement ends and
The first statement.
If the condition is true (not equal to 0), execute the statement.
C. Execute the expression increment and return to step 2.
Simple for Loop
# Include
/* For Loop example */int I; // print the number void main (void) {for (int I = 1; I <= 20; I ++) // count up {printf ("% d \ n", I) ;}system ("pause ");}
Result After running
A for statement is often used to "count up", that is, to increase a calculator from one value to another.
You can use "Inverted count", that is, to decrease the number of calculator variables.
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + ICA8c3Ryb25nPrW5vMbK/Twvc3Ryb25nPjxicj4KPC9wPgo8cD48cHJlIGNsYXNzPQ = "brush: java;"> # include Void main () {for (int I = 20; I> = 1; I --) // reverse count {printf ("% d \ n", I );} system ("pause ");}
Running result
Increment is 2
# Include
Void main () {int I = 0;/* for statement is very flexible. If the counter has been initialized before the program, the initialization expression can be omitted, but the separator cannot be omitted. */For (; I <= 20; I + = 2) // count up, but the increment is 2 {printf ("% d \ n", I );} system ("pause ");}
Running result
Another method
# Include
Void main () {int I = 0; for (; I <= 20;) // count up, but the increment is 2 {// I ++. You can also write printf ("% d \ n", I ++) here ); // I ++ is also possible here} system ("pause ");}
Running result
Deep understanding of
# Include
Void main () {/* can be any expression used to terminate the for loop. As long as the expression is true (non-zero), The for statement continues to be executed. */Int I = 0; int j = 15; for (; I <= 20 & j> = 0;) // count up, but the increment is 2 {j --; // printf ("% d \ n", I ++) can also be written here ); // I ++ is also possible here} system ("pause ");}
Running result
Initial can make any legal expression. It is a specific value assignment statement that sets the variable.
Condition can be any legal expression, usually a relational expression. When condition is false (0),
The statement is terminated, and the first statement after statement is executed; otherwise, the statement in statement is executed.
A for statement can contain another for Statement, which is nested.
Print any row or column
# Include
Void draw_box (int, int); void main () {// print out eight rows and 25 columns of Xdraw_box (8, 25); system ("pause ");} void draw_box (int row, int column) {int col; // defines the intermediate variable for (; row> 0; row --) {for (col = column; col> 0; col --) {printf ("x");} printf ("\ n"); // after each line is printed, add a line break to wrap it }}
Running result
2. while statement
A while statement is also called a while loop. It executes a block until the condition is false.
While (condition)
Statement
Condition allows any expression. statement is a statement or a composite statement.
After the program executes the while statement, the following event occurs:
A. Calculate the expression condition;
B. If condition is false (0), the while statement ends.
C. If condition is true (not 0), the statement in statement is executed.
D. Return to step 1.
While Loop
# Include
Int count;/* print the numbers from 1 to 20 */void main () {count = 1; // initialize while (count <= 20) {printf ("% d \ n", count); count ++;} system ("pause ");}
Running result
Conclusion:
For (; condition;) is equivalent to while (condition );
Due to this equivalence, any work that can be done using the for statement can be completed using the while statement.
When using the while statement, you must first use a separate statement to complete all necessary initialization work.
You must use a statement in the while loop to perform incremental operations.
Like the for statement, the while statement is nested.
Combination of for and while
# Define _ CRT_SECURE_NO_WARNINGS # include
Int array [5]; // declare an array with a capacity of 5 void main (void) {// declare and initialize two variables int ctr = 0, nbr = 0; printf ("This program prompts you to enter 5 numbers \ n"); printf ("Each number shocould be from 1 to 10 \ n"); while (ctr <5) {nbr = 0; while (nbr <1 | nbr> 10) {printf ("\ n Enter number % d of 5:", ctr + 1 ); scanf ("% d", & nbr);} array [ctr] = nbr; // assign ctr ++ to the array; // increment 1} // output array for (ctr = 0; ctr <5; ctr ++) {printf ("Value % d is % d \ n ", ctr + 1, array [ctr]);} system ("pause ");}
Running result