- First, for
- Second, while
- Third, do While
- Iv. continue
- V. Break
First, for
Please output 1000 * numbers on the screen
printf ("************************* ...");
#include "stdio.h" void Main () { int i; for (i=1;i<=1000;i++) { printf ("*");} }
for (expression A; expression b; expression c)
{
The block of code to be executed repeatedly D;
}
1, expression a executes first, and only once
2, expression B executes, if true, code block D
3, then execute the expression C, and then select Expression B, back to the 2nd step
4, ABCD can be omitted for (;i>=100;);
5. Expression 1 is usually used to assign an initial value to a cyclic variable, which is generally an assignment expression I=1 i=-1,j=100
6. Expression 2 is usually a cyclic condition, typically a relational expression or a logical expression i>=5 i>5&&j<=8
7, expression 3 is usually used to modify the value of the loop variable, is generally a compound assignment statement i++,i--, i+=5,j++
8, these three expressions can be a comma expression, that is, each expression can be composed of more than one expression. All three expressions are optional and can be omitted.
#include "stdio.h" void Main () { int i,j; for (i=1;i<=10;i++) { printf ("%d \ n", i);} }
#include "stdio.h" void Main () { int i,j; for (i=-10;i<=10;i++) { printf ("%d \ n", i);} }
#include "stdio.h" void Main () { int i,j; for (i=1;i<=10;i=i+3) { printf ("%d \ n", i);} }
#include "stdio.h" void Main () { int i,j; for (i=10;i>0;i--) { printf ("%d \ n", i);} }
#include "stdio.h" void Main () { int i,j; For (I=10,j=1;i>0;i--, j + +) { printf ("%d +%d = one \ n", i,j);} }
#include "stdio.h" void Main () { int i=1; for (; i<=10;i++) { printf ("\ n%d", i);} }
#include "stdio.h" void Main () { int i=1; for (;i<=10;) { printf ("\ n%d", i); i++;} }
#include "stdio.h" void Main () { int i=1; for (;;) { printf ("\ n%d", i); i++; if (i>10) break; }}
Each expression in the For statement can be omitted, but the semicolon delimiter must not be less. Such as:
The for (; expression; expression) omits expression 1.
The for (expression;; expression) omits expression 2.
for (expression; expression;) omitted Expression 3.
for (;;) eliminates all expressions.
When the loop variable is assigned an initial value, you can omit expression 1, such as omitting expression 2 or expression 3 will cause an infinite loop, then you should try to end the loop inside the loop.
The Loop body (the statement to be repeated) can be an empty statement.
Exercise: Calculate the sum of the odd numbers between 1-100. 1+3+5+7....99=?
#include "stdio.h" void Main () { //exercise: Calculates the sum of the odd numbers between 1-100. 1+3+5+7....99=? int i,s=0; for (i=1;i<=100;i++) { if (i%2==1) { s=s+i; } } printf ("Result:%d", s);}
2500
#include "stdio.h" void Main () { //exercise: Calculates the sum of the odd numbers between 1-100. 1+3+5+7....99=? int i,s=0; for (i=1;i<=100;i=i+2) { s=s+i; } printf ("Result:%d", s);}
2500
Second, while
The general form of a while statement is:
Initial value
while (expression a)
{
The value of the expression is true when the statement block to repeat; B
Step
}
Where the expression is a loop condition
The statement block and step size to repeat are the 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).
1, the program first determine whether a is true, if true, the implementation of the loop body B
2, after the completion of the loop body and then back to the 1th step
3. End loop when expression A is false
#include "stdio.h" void Main () { int i=1; while (i<=10) { printf ("%d \ n", i); i=i+1;} }
The expression in a while statement is typically a relational expression or logical expression, as long as the expression's value is true (not 0) to continue looping
If the loop body includes more than one statement, it must be enclosed in {} to form a compound statement.
You should pay attention to the selection of loop conditions to avoid a dead loop.
Practice:
Exercise: Calculate the sum of the odd numbers between 1-100. 1+3+5+7....99=?
/* Note:your choice is C IDE */#include "stdio.h" void Main () { int i=1,s=0; while (i<=100) { s=s+i; i=i+2; } printf ("s=%d", s);}
2500
Exercise: List all the numbers between 1-1000 that can be divisible by 3 and 7 at the same time.
Third, do While
The general form of the Do-while statement is:
Do
{
The block of statements to repeat;
}while (expression);
Where the statement block to repeat is the body of the loop
Expression is a looping condition
The semantics are: first execute the Loop body statement once, then judge the value of the expression, if True (not 0) continue to loop, otherwise terminate the loop.
/* Note:your choice is C IDE */#include "stdio.h" void Main () { int i=1,s=0; Do { s=s+i; i++; } while (i<=5); printf ("%d", s);} I=1 2 3 4 5 6//s=0 1 3 6 10 15
15
Applies to applications that must be executed once, such as entering a qualified number.
#include "stdio.h" void Main () { int n; do { printf ("Please enter a number between 1-7:"); scanf ("%d", &n); } while (n<1| | N>7);}
Iv. continue
Encountering continue in For,while,do-while indicates the end of the secondary loop and continues the next cycle.
#include "stdio.h" void Main () { int i; for (i=1;i<=10;i++) { printf (""); printf ("-");} }
#include "stdio.h" void Main () { int i; for (i=1;i<=5;i++) { if (i%2==0) { continue; End at the time, continue next } printf ("-"); } I=1 2 3 4 5 6 ---}
V. Break
Break jumps out of the current loop in For,while,do-while,switch.
#include "stdio.h" void Main () { int i; for (i=1;i<=5;i++) { if (i%2==0) {break ; End Current Loop } printf ("-"); } }
-
Exiting in an unconditional loop
#include "stdio.h" void Main () { int i=1,s=0; while (1) { s=s+i; i++; if (i>100) break; } printf ("%d", s);}
C Language Fifth cycle structure