C language programming loop structure programming

1 Need to cycle structure in the program

The cyclic structure is used to deal with the problems that need to be processed repeatedly, so the cyclic structure is also called the repetitive structure.
An effective loop should specify two conditions: (1) the loop body; (2) the condition for the end of the loop, that is, under what circumstances stop repeated operations.
C language provides three loop structure statements, while statement, do...while statement and for statement.

2 Use the while statement and do...while statement to realize the loop

2.1 Use the while statement to realize the loop

Find the sum of integers from 1 to 100.
Insert picture description here

The general form of the while statement: while (expression) statement
When the expression is non-zero, the embedded statement in the while statement is executed.
Insert picture description here
The characteristics of the while loop: first judge the expression, and then execute the loop body (inline statement).

2.2 Use do...while statement to achieve loop

Find the sum of integers from 1 to 100.
Insert picture description here

The general form of do...while statement:
do
loop body statement
while (expression);
Insert picture description here
The characteristic of do...while statement is to execute the loop body first, and then judge whether the loop condition is established.

3 Use the for statement to implement a loop

Find the sum of integers from 1 to 100.
Insert picture description here

The for statement is used when the number of loops is uncertain and only the end condition of the loop is given. It can completely replace the while statement.

3.1 The general form and execution process of the for statement

General form: for (expression 1; expression 2; expression 3) statement
for (assign initial value to loop variable; loop condition; loop variable increment) statement
Execution process:
Insert picture description here

4 Loop nesting

A loop body contains another complete loop structure, called loop nesting. The embedded loop can also nest loops, which is a multi-layer loop.
The three loops can be nested within each other. Such as:
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

5 End the loop early

5.1 Use the break statement to exit the loop early

The general form of the break statement: break; The
break statement is only used in loop statements and switch statements.
The break statement is to end the entire loop process
Insert picture description here

5.2 Use the continue statement to end this loop early

The general form of the continue statement: continue; the
function is to end the loop, that is, skip the unexecuted statements in the loop body, and then judge whether to execute the loop next time.
The continue statement only ends this loop.
Insert picture description here
Example: Input the grades of students in a class, output the grades of the students who failed, and find the average grade of the students who passed.
Idea: Output the failed grades, and then skip the sentence of summing up and averaging the total grades. Use the continue sentence to deal with this problem.
Insert picture description here

6 Comparison of several cycles

1 All three cycles can be replaced.
2 The for loop is an operation that makes the loop close to the end in "expression 3".
3 The initialization of loop variables is completed before the while and do...while statements. The for statement realizes the initialization of the loop variable in expression 1.
4 Break statement and continue statement can be used for all three loop statements.

7 Comprehensive example of cycle program

Rabbit breeding problems

There is a pair of rabbits, and they give birth to a pair of rabbits in the third month after giving birth. After the little rabbits grow up to the third month, they give birth to a pair of rabbits every month. Assuming that all rabbits are not dead, what is the total number of rabbits in 40 months?
Rabbit breeding rules
Insert picture description here
Note: Those less than 1 month old are small rabbits, those less than 2 months old are medium rabbits, and those more than 3 months old are old rabbits.
The total number of rabbits is 1,1,2,3,5,8,13..., this is the Fibonacci sequence. The characteristic of this sequence is that the first and second numbers are 1, 1. Starting from the third number, the number is the sum of the previous two numbers.
Find the top 40 numbers in the Fibonacci sequence.
Writing a program:
Insert picture description here
Analysis:
The function of the if statement is to make a line break after outputting 4 numbers.

8 Improved part

8.1 Comparison of while and do...while loop

Use the while statement and do...while statement to deal with the same problem. If the loop question part of the two is the same, their results are also the same.
However, if the expression after while is false (0 value) at the beginning, the results of the two loops are different.
Example: Find the sum of integers from 1 to 10.
Use the while statement
Insert picture description here

After running it once, enter 11.
Insert picture description here
Use the do...while statement to
Insert picture description here
Insert picture description here
analyze:
when the input i is less than or equal to 10, the two results are the same. But when ii is greater than 10, the two results are different. For the while loop, it is false when i<=10 and nothing is executed. For the do...while loop, it is false when i<=10, and the loop body is executed once.
Conclusion:
When the first value of the expression after while is "true", the results of the two loops are the same, otherwise they are not the same.

8.2 Various forms of the for statement

(1) Expression 1 can be omitted, but the semicolon after expression 1 cannot be omitted. Such as:
Insert picture description here
Note that at this time, the loop variable i should be assigned an initial value (such as i=1;) before the for statement, so that the loop can proceed normally.
(2) Expression 2 is omitted, that is, the loop condition is not judged, and the loop will continue indefinitely. That is to say, expression 2 is always "true".
Insert picture description here
For example:
Insert picture description here
equivalent to
Insert picture description here
(3) Expression 3 can be omitted, but additional design is needed to ensure that the loop ends normally. For example:
Insert picture description here
The operation of i++ is not placed in the position of expression 3, but as part of the loop body, it can make the loop end normally.
(4) Expression 1 and Expression 3 can be omitted, and only Expression 2, that is, only the loop condition is given. For example:
Insert picture description here
equivalent
Insert picture description here
In this case, it is completely equivalent to the while statement. The for statement can give loop conditions, assign initial values, and automatically increment loop variables.
(5) All three expressions can be omitted, for example:
for (;;) statement
does not set the initial value, does not judge whether the loop condition is satisfied, the loop variable does not increase in value, that is, the loop body is executed without termination.
Equivalent to the
while(1) statement. The
loop condition is always "true" (a value other than 0 represents "true"), that is, the loop body is executed without termination.

(6) Expression 1 can be other expressions that have nothing to do with loop variables. For example:
Insert picture description here
(7) Expression 3 can also be any expression that has nothing to do with loop control.
(8) Expression 1 and Expression 3 can be simple expressions or comma expressions, that is, they contain more than one simple expression, separated by commas. For example:
Insert picture description here
Set two initial values ​​at the same time to increase the value of the two variables.
All the expressions are solved in order from left to right, and the value represented by the entire comma is the value of the rightmost expression.
Insert picture description here
Equivalent to
Insert picture description here
(9) The expression is generally a relational expression or a logical expression, and can also be a numerical expression or a character expression. As long as its value is not 0, the loop body is executed.
Insert picture description here
Implementation process:
Insert picture description here

9 Summary

1 The cyclic structure is used to deal with the problems that need to be processed repeatedly, so the cyclic structure is also called the repetitive structure.
2 For a valid loop, two conditions should be specified: (1) the loop body; (2) the condition for the end of the loop, that is, under what circumstances stop repeated operations.
3 C language provides three loop structure statements, while statement, do...while statement and for statement. The for loop is the most widely used and most flexible.
4 The loop question has more than one sentence. Use braces {} to enclose multiple sentences in the loop body to form a compound sentence.
5 The continue statement only ends this loop, and the break statement ends the entire loop process.
6 Loops can be nested.

Guess you like

Origin blog.csdn.net/qq_45059457/article/details/113915227