Cyclic structure
- Features: when a given condition is true, execute a program segment repeatedly until the condition is not true.
while statement
- General form:
while(expression){ sentence; }
flow chart
- be careful:
- There should be statements in the loop body that make the loop tend to end, otherwise the program will fall into an endless loop and execute the statements indefinitely.
- The program falls into an endless loop and ends with atrl+break
- If the loop body contains more than one statement, it should be enclosed with {}!
example
Output 1-30 at console
#include <stdio.h> void main() { int i = 1; while (i <= 30) { printf("%d,", i); i++; } }
Find sum = 1 + 2 + 3 ++ one hundred
Problem solving ideas
- Let sum represent the addend (the initial value is 0)
- Let i denote the addend (the initial value is 1)
- It ends after 100 additions, or when the addend is greater than 100
- Store calculation results in sum
#include <stdio.h> void main() { int sum = 0; int i = 1; while (i <= 100) { sum = sum + i; i++; } printf("%d\n", sum); }
do while statement
Execute the loop first, then judge, do The while loop ensures that the loop is executed at least once
- General form:
do{ sentence; } while (expression);
example
Find sum = 1 + 2 + 3 ++ 100, use do While implementation
#include <stdio.h> void main() { int sum = 0; int i = 1; do{ sum = sum + i; i++; } while (i <= 100); printf("%d\n", sum); }
for loop
- General form
for ( Expression 1; Expression 2; Expression 3) { sentence; }
flow chart
- The expression 1 statement is executed first and only once. This step allows you to declare and initialize any loop control variables. You can also not write any statements here, as long as a semicolon appears.
- Next, it will be judged that if the expression 2 statement is true, the loop body will be executed. If false, the loop body is not executed and the next statement after the for loop is executed.
- After executing the body of the for loop, the control flow will jump back to the expression 3 statement above. This statement allows updating loop control variables. This statement can be left blank as long as a semicolon appears after the condition.
- The condition is judged again. If true, the loop is executed and the process is repeated. When the condition becomes false, the for loop terminates.
- For and while can complete the same loop function, but for is more concise and efficient.
example
Find sum = 1 + 2 + 3 ++ 100, implemented with for
#include <stdio.h> void main() { int sum = 0; for (int i = 1; i <= 100;i++) { sum = sum + i; } printf("%d\n", sum); }
- One or two or three expressions can be omitted, but the ";" after the expression Cannot default.
- For the normal operation of the program, expression 1 (initial value of cyclic variable) and expression 3 (increment of cyclic variable) are omitted.
- Expression 2 is generally a relational expression or a logical expression, but it can also be a numerical expression or a character expression. As long as its value is non-zero, the loop body will be executed.
Expression 1 is omitted.
#include <stdio.h> void main() { int sum = 0; int i = 1; for (; i <= 100;i++) { sum = sum + i; } printf("%d\n", sum); }
If expression 2 (loop condition) is omitted, it will become an endless loop without other processing.
#include <stdio.h> void main() { int sum = 0; for (int i = 1;;i++) { sum = sum + i; } printf("%d\n", sum); }
Expression 3 is omitted.
#include <stdio.h> void main() { int sum = 0; for (int i = 1;i <= 100;) { sum = sum + i; i++; } printf("%d\n", sum); }
goto Statement
General form:
goto Statement label; //Statement labels are composed of letters, numbers and underscores. Integers cannot be used ... Statement label: sentence;
flow chart
example
Find sum = 1 + 2 + 3 ++ 100, implemented with goto and if
#include <stdio.h> void main() { int sum = 0; int i = 1; loop: if (i <= 100) { sum = sum + i; i++; goto loop; } printf("%d\n", sum); }
The difference of four cycle structures
- All four loops can be used to deal with the same problem and can generally replace each other. goto type circulation is generally not advocated.
- For while and do while loops, the loop body should contain statements that make the loop close. The function of the for statement is stronger. Anything that can be completed with the while loop can be realized with the for loop
- When using while and do while loops, the initialization of loop variables should be completed before the while and do while statements, while the for statement can initialize loop variables in expression 1.
- While loop, do while loop and for loop can jump out of the loop with break statement and end the loop with continue statement.
example
- Idea:
- Set the variable height h and the number of folds n
- Cycle termination condition H > 8848000
- Folding algorithm h = pow(2,n); n=n+1; (pow is a function of power)
Assuming that a piece of paper is large enough and can be folded indefinitely, and the thickness of this paper is 1mm, how many times can this paper be folded to reach the height of Mount Everest (8848m)?
// while Loop #include <stdio.h> #include <math.h> void main() { int h = 0, n = 0; while (h <= 8848000) { h = pow(2, n); n++; } printf("%d\n", n - 1); } // for loop #include <stdio.h> #include <math.h> void main() { int n, h=0; for (n=0;h <= 8848000;n++) { h = pow(2, n); } printf("%d\n", n - 1); }