Loops for loops, while loops, do while loop

When the loop may be a case where the loop condition repeatedly execute a section of code, the code is repeated is referred to as loop statement, when this loop is repeatedly performed, it is necessary at the right time cycle is determined to modify the conditions false, thus ending the cycle, otherwise the loop will always execute it, forming an endless loop.

for loop

 

Statement format:

 

Stepping expression: change the loop condition of expression

 

Implementation process:

The order of execution: ①②③④> ②③④> ②③④ ... ② so far satisfied.
① responsible for completing the loop variable initialization
② responsible for determining whether the loop condition is not satisfied out of the loop
statements execute specific ③
④ After cycling, cycling conditions change of variables involved

example:

Using a loop, calculates the even-numbered 1 to 100 and

public  static  void main (String [] args) {
 // 1. the definition of a variable initialization, recording cumulative sum, the initial value 0 
int SUM = 0 ;
 // 2. use for obtaining the digital loop between 1-100 
for ( int I =. 1; I <= 100; I ++ ) {
 // array 3. the judgment obtained is odd or even 
IF (I% 2 == 0 ) {
 // 4. If the sum is even increments, 
sum + = I; 
} 
} 
// after the 5 cycles, the accumulation result printed 
System.out.println ( "SUM:" + SUM); 
}

Note: for variable cycle can only be used inside the loop, the loop can not be used

 

while loop

 

Statement format:

Implementation process:

The order of execution: ①②③④> ②③④> ②③④ ... ② so far satisfied.
① responsible for completing the loop variable initialization.
② responsible for determining whether the loop condition is not satisfied out of the loop.
③ specific statements executed.
④ After the loop, the loop variable changes

 

example:

while loop and the calculated between 1-100

public  static  void main (String [] args) {
 // while loop implemented
 // define a variable record cumulative sum 
int SUM = 0 ;
 // defines the initialization expression 
int I =. 1 ;
 // use while loop initialization so value of the expression changes 
the while (I <= 100 ) {
 // accumulated sum 
SUM + = I;
 // step expression to change the value of the variable 
I ++ ; 
} // print summation variable 
System.out.println ( " and 1-100 are: "+ SUM); 
}

 

for  and  while  small differences

 

Control statements that variable conditions controlled, at the end of the for loop, you can not be accessed, and

 

And while the end of the cycle can continue to use, if you want to continue using it with a while, or recommended for.

 

The reason is that for the end of the cycle, the variable will disappear from memory, can improve memory usage efficiency.
Recommended for use when the known number of cycles, when an unknown number of cycles is recommended while.

 

 

do ...... while loop

Statement format:

Implementation process:

The order of execution: ①③④> ②③④> ②③④ ... ② so far satisfied.
① responsible for completing the loop variable initialization.
② responsible for determining whether the loop condition is not satisfied out of the loop.
Specific executed statement ③
④ After the cycle, the loop variable changes

note:

do ... while loop characteristics: the unconditional implementation of a loop, even if we write directly to the loop condition false, it still will cycle once. This cycle has certain risks, and therefore not recommended for beginners to do ... while loop.

Guess you like

Origin www.cnblogs.com/libinhong/p/10988682.html