Intialization
Condition loop condition (repeated execution if the condition is met)
Increment increment of cyclic Variables
4. Statement loop body
5.
While (<# condition #> ){
<# Statements #>
}
Initialize the loop variable before the while;
Then, when the condition is followed by the condition in the small Guo: the loop body is executed when the condition is met,
Then statements: Statement (loop body );
At last, write increment: the increment of the loop variable (the increment of the loop variable );
Int I = 1; // the initial value of the cyclic variable.
Do {
Printf ("% d", I); // cyclic body
I ++; // increment of cyclic Variables
} While (I <= 100); // cyclic Condition
/For (INT I = 0; I <101; I ++ ){
// Sum + = I;
//
//} Internal loop. After the sum of all items is executed, the loop is displayed in the output.
// Printf ("sum = % d \ n", sum );
Difference between while and do... while loop:
For a while loop, you must first judge the loop condition and then execute the loop body. for do .... for a while loop, whether or not the loop condition is true, execute a loop body first, and then judge whether the loop condition is true.
Differences between while and for loops:
While loops are mostly used when the number of loops is unclear. For loops are mostly used to know the number of loops. For loops can be converted to while loops, but while loops cannot be converted to for loops.
When to use a loop?
When we do the same thing twice or more times, we need to consider the use of loop. The repeated operations as the loop body.
It is recommended that the loop should not exceed two layers, otherwise the readability of the program will deteriorate.
// The for loop can be converted to the while and do while loops, but the while and do while loops may not be converted to the for loop.
Break is used to end the cycle of the current layer,
Continue; // used to end this loop, and then jump to the next loop to continue execution;
This article from "a long time back to one" blog, please be sure to keep this source http://lulun426.blog.51cto.com/9197884/1538384