C language loop structure learning and sharing!

"C language" loop control structure learning sharing
C language learning I learned from the little turtle lesson on Bilibili, very good, if necessary, highly recommended!
The characteristic of the cycle structure: execute a certain block repeatedly until the condition is not established. The given condition is called the loop condition, and the repeatedly executed program segment is called the loop body.
There are mainly 4 loop structures:
(1) Use goto statements and if statements to form loops; (not commonly used)
(2) Use while statements;
(3) ) Use do-while statement;
(4) Use for statement;

these 4 forms can be transformed into each other! ! !
Let me analyze the difference between these four structures in detail:
(1) Goto statement The
goto statement is an unconditional transfer statement, and its format is: goto statement label;
usually goto statement is used in conjunction with if conditional statement, but note: this statement is not commonly used , Mainly because it makes the program level unclear and difficult to read, but it is more reasonable to use the goto statement when the multi-level nesting exits.
Here is an example for everyone: if the conditional procedure in the if on line 6 is established, the loop body is executed, and when the goto loop instruction is encountered, the program does not continue to execute, but returns to line 6 to continue the loop until if Until the condition is not established. Insert picture description here
(2) While statement
The general form of the while statement:
while (loop condition)
{ loop body } When the loop condition is not 0, execute the loop body, if the loop condition is 0, exit the loop. Emphasize: The programs here solve the same problem, but the loop structure used is different, so that everyone can see the difference in the loop body! ! !




I think this program is easy to understand. I won’t explain too much here. If you don’t understand, please leave a message!
Insert picture description here
(3)
The general form of do-while statement do-while statement is:
do
{ (loop body) } while (loop condition expression) is the same sentence, if you don’t understand, please leave a message! ! ! ! ! ! The difference between while statement and do-while statement The while statement first judges the loop condition and then executes the loop body, so when the condition is not established, the loop body will not be executed; the do-while statement loops once and then judges the condition, so this statement Cycle at least once! Also, in the do-while statement, the semicolon in English must not be missing after while(i<=100)! ! ! Look at line 10 of the above program! (4) For statement General form: for(expression 1; expression 2; expression 3) { loop body } The execution process of this loop is briefly described: 1. First find expression 1 [expression 1 is generally a loop variable Assign initial value] 2. Go to expression 2 [Expression 2 is generally a loop condition] 3. If expression 2 is true, execute the loop body, if it is false, then end the loop 4. If not exit the loop, go to the expression 3 [Expression 3 is generally the increment of the loop variable] 5. Return to step 2 above and continue execution until the loop condition is not established. It is the same sentence. If you don't understand, please leave a message! note:




Insert picture description here
















1. All three expressions in the for loop can be omitted, but the two semicolons cannot be omitted.
2. Omit expression 1, which means no initial value is assigned to the loop variable.
3. If expression 2 is omitted, no other processing will be done and it will become an endless loop!
Insert picture description here
Just say so many 4 kinds of cycles! It will be incomplete, if there is an error, thank you for your guidance!

Guess you like

Origin blog.csdn.net/qq_46216951/article/details/108816981