The C language provides a variety of looping statements that can form a variety of different forms of loop structure:
- The loop is formed by the Goto statement and the IF statement;
- Use the while statement;
- Use the Do-while statement;
- with a for statement;
One, goto statement
A goto statement is an unconditional transfer statement similar to a goto statement in BASIC. The goto statement is used in the following format:
Goto statement label;
Where the label is a valid identifier, this identifier, together with a ":" appears somewhere inside the function, after the goto statement is executed, the program jumps to the label and executes the subsequent statement.
The goto statement is usually not used, mainly because it will make the program level unclear, and difficult to read, but in the multi-layered nesting exit, with a goto statement is more reasonable.
Second, while
The general form of a while statement is:
while (expression) statement
Where the expression is a loop condition and the statement is a loop body.
The semantics of the while statement is to execute the Loop body statement when the value of the expression is evaluated, and the values are true (not 0).
Exercise: Count the number of characters entered in a line from the keyboard.
- #include <stdio.h>
- int main(void){
- int n=0;
- printf("Input a string:\ n");
- while (getchar()! ='\ n') n+ +;
- printf("%d", n);
- return 0;
- }
Third, Do...while
The general form of the Do-while statement is:
Do
Statement
while (expression);
This loop differs from the while loop in that it executes the statements in the loop first, then determines whether the expression is true, resumes the loop if true, and terminates the loop if it is false. Therefore, the Do-while loop executes at least one loop statement.
Iv. for
In the C language, the For statement uses the most flexible, and it can completely replace the while statement. The general form of it is:
for (expression 1; expression 2; expression 3) statement
It performs the following procedures:
- The expression 1 is solved first.
- Solve Expression 2, if its value is true (not 0), execute the inline statement specified in the FOR statement, and then perform step 3rd below), and if its value is False (0), end the loop and go to step 5th).
- Solve an expression 3.
- Back to the 2nd above).
- The loop ends with the execution of a statement below the for statement.
Attention:
1) "Expression 1 (loop variable assignment initial value)" In the For Loop, "Expression 2 (Loop condition)" and "Expression 3 (loop variable increment)" Are all selections, that is, you can default, but the semicolon (;) cannot be defaulted.
2) omitted "Expression 1 (the cyclic variable assigned initial value)", indicating that the loop control variable is not assigned an initial value.
3) "Expression 2 (cyclic condition)" is omitted, then it becomes a dead loop when no other processing is done.
4) Omit "Expression 3 (increment of loop variable)", then do not operate the loop control variable, you can add the statement that modifies the loop control variable in the statement body.
5) "Expression 1 (cyclic variable assignment initial value)" and "Expression 3 (loop variable increment)" are omitted.
6) 3 expressions can be omitted. For example:
for (;;) Statement
Equivalent:
while (1) statement
7) Expression 1 can be an assignment expression that sets the initial value of a loop variable, or it can be another expression.
8) expression 1 and expression 3 can be a simple expression or a comma-expression.
9) Expression 2 is generally a relational expression or a logical expression, but also a numeric expression or a character expression, as long as its value is nonzero, the loop body is executed.
V. Break out of the loop
Break statements are typically used in loop statements and switch statements. When break is used in switch statements, the program can jump out of a switch and execute a switch statement;
When the break statement is used in a do-while, for, and while Loop statement, the program terminates the loop and executes the statement following the loop, usually the break statement is always associated with the IF statement, which jumps out of the loop when the condition is met.
Vi. continue end of the cycle
The Ontinue statement acts by skipping the remaining statements in the loop body and forcing the next loop to be executed. The continue statement is used only in the loop body of for, while, Do-while, and often with an if condition statement to speed up the loop.
Exercise: Find all primes between 100 and 200.
- #include <stdio.h>
- #include <math.h>
- int main(void){
- int M, I, K, n=0;
- For(M=101; M<=; M=m+2){
- K=sqrt(M); //M- Radical
- For(i=2; I<=k; I+ +)
- If(M%i= =0) break ;
- If(i>=k+1){
- printf("%d\ n", M);
- n=n+1;
- }
- }
- printf("\ n");
- return 0;
- }
Exercise 2: Use the following formula to find π:
Analysis: First, the denominator of an item with a positive coefficient is 4n-3 (n is the number of items for a positive item), the denominator of the item with negative numbers is 4n-1 (n is the number of items of a negative item), that is, the denominator is 1, 3, 5, 7 ... the odd series, the denominator of the nth item is 2n-1 10000-1.
- #include <stdio.h>
- #include <math.h>
- int main(void)
- {
- Double P=0, J=1;
- int i;
- for (i=1; I<10000; I+ + ) //Here I is the number of items
- {
- J=Pow(-1.0, I+1)/(2*i-1); //pow (x, y ) to the Y power of x
- P+=j;
- printf("%lf\ n",4*p); //Output value of each item
- }
- printf("%lf\ n",4*p); //Output final pi value
- return 0;
- }
"C-Cycle structure"