1 loop statement
1.1 loop statements mainly include: while, do-while, and.
2while statement processing cycle
2.1while statement basic form:
While (expression)
{Loop body statement}
2.2 execution process: first, calculate the value of the while expression. When the value is not zero, execute the statements in the loop body. After the execution, continue to judge the value of the while expression. If the value is not zero, continue to execute the loop body, when the expression value is zero, the system jumps out of the loop and runs subsequent statements.
Sample Code:
<Span style = "font-size: 18px;"> # include "stdio. h"
Main (){
Int I, sum;
I = 1;
Sum = 0;
While (I <= 100)/* The number of accumulated I is less than or equal to the number of terminated n, and then cyclically accumulates; otherwise, the loop ends */
{Sum = sum + I;
I ++;
}
Printf ("sum = % d \ n", sum );
} </Span>
Note: The initial values must be assigned when sum and I are used.
Note the following when using the 2.3while statement:
The loop body must contain multiple statements that conform to the statement format. in the loop body expression, there must be a statement that controls the cycle to end, otherwise the loop will be infinitely executed; the loop body can be empty;
2.4while instance: a + aa + aaa + ....
Code:
<Span style = "font-size: 18px;"> # include "stdio. h"
Main ()
{
Int a, x;
Long sum = 0, n = 1, tn;
Scanf ("% d", & a, & x );
Tn =;
While (n <= x)/* Stop when n exceeds x */
{Sum + = tn;
Tn = tn * 10 + x;
N ++;
}
Printf ("result is % ld", sum );
} </Span>
Question: factorial.
3do while statement processing cycle
3.1do while statement basic form:
Do {loop body statement}
While (expression );
3.2 execution process: first execute the loop body after do, then calculate the expression value in while, continue to execute the do loop body when the value is not zero, when the value is zero, jump out of the loop.
Sample Code:
<Span style = "font-size: 18px;"> # include "stdio. h"
Main (){
Int I, sum;
I = 2;
Sum = 0;
Do/* execute the loop first */
{
Sum + = I;
I + = 2;
} While (I <= 100);/* judgment condition */
Printf ("sum = % d \ n", sum );
} </Span>
3.3while and do while are different: while must first judge whether the while expression is true and then execute the loop body, the do loop body is executed first.
4for statement processing cycle
4.1 basic syntax:
For (expression 1; expression 2; expression 3)
Loop body;
Note: for is a C-language keyword. The three expressions in brackets can be arbitrary expressions. When multiple statements exist in the loop, use compound statements.
4.2 calculate expression 1 first. Expression 1 is executed only once. Generally, it is a value assignment statement used to initialize the variable. Then, solve expression 2. If the value is zero, exit the loop. If the value is non-zero, then execute the loop body. Then execute expression 3, then judge expression 2, and then execute the loop body until expression 2 is invalid.
Sample Code:
<Span style = "font-size: 18px;"> # include "stdio. h"
Main ()
{
Int I, sum;
Sum = 0;
For (I = 1; I <= 100; I ++)
Sum + = I;
Printf ("sum = % d", sum );
} </Span>
4.3for statement features: Expressions 1, 2, and 3 can be omitted, but the semicolon (;) cannot be omitted; expressions can be any form of expressions; the loop body can be empty; you can use multiple variable control loops in the for brackets.
Sample Code for determining prime numbers:
<Span style = "font-size: 18px;"> # include "stdio. h"
# Include "math. h"
Main ()
{Int n, m, flag;
Flag = 1;/* the flag variable is the flag variable. If the value is 1, n is the prime number. Otherwise, n is not the prime number. Initial Value: 1 */
Printf ("\ n enter n:");/* enter n */
Scanf ("% d", & n );
For (m = 2; m <n & flag; m ++)/* determines whether n can be 2 ~ Division of numbers between n-1 */
If (n % m = 0)
Flag = 0;/* n is not a prime number */
If (flag = 1)/* if the flag value does not change, it is a prime number */
Printf ("\ n % d is a prime number.", n );
Else
Printf ("\ n % d is not a prime number.", n );
} </Span>
4.4 thinking:
1. Calculate the factorial.
2. output the Fibonacci series.
3. output the number of daffodils.
4. output format:
*****
****
***
**
*
5. Process Control statements
Sometimes we may need to interrupt the loop or skip this loop during the execution of the loop. In this case, we need to use the process control statement.
5.1 break: In the switch statement, we often use break to jump out of the switch branch. In fact, break can also be used in the loop statement to jump out of the loop. Use break in a loop to allow multiple outlets of the loop statement, making the program more flexible.
The break statement can jump out of the loop before the condition is reached.
Sample Code:
For (I = 0; I <10; I ++ ){
Printf ("% d", I );
If (I = 0) break;
}
Question: use the break statement to modify and judge the prime number.
5.2 continue: in the loop process, you can determine whether to execute this loop based on the conditions. After using continue, the subsequent code will not be executed and the next loop will be executed directly. However, unlike break, it can be used in both the Branch and the loop, and continue can only be used in the loop structure.
Sample Code:
For (I = 0; I <10; I ++ ){
If (I = 0) continue;/* do not execute a loop when I = 0 */
Printf ("% d", I );
}
Thinking Exercise: use continue to calculate the number of divisible values in 1000 by 3, 5, and 7.
5.3 goto: it is also called an unconditional turning statement.
Its general format is: goto label;
Sample Code:
<Span style = "font-size: 18px;" >#include <stdio. h>
Main (){
Int I, s = 0;
I = 1;
Loop: if (I <= 100) {/* indicates the position where the goto statement is switched */
S + = I;
I ++;
Goto loop;
}
Printf ("1 + 2 + 3 +... 100 = % d \ n ", s );
} </Span>
In structured program design, goto statements are not recommended, but sometimes goto statements are convenient. For example, in a multi-layer loop body, multiple breaks are required to jump out of the loop to the outermost layer, but you can directly implement them using goto.
From letthinking's column