Self Java loop while the day03_Java

While loop

while loop 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 ④ cycle, changes in the loop variable.

Execution Flow

demand

Illustrate the basic usage while

Code

/ * 
The while loop there is a standard format, there is an extended format. 

Standard format: 
the while (conditional) { 
    loop body 
} 

Extended format: 

initialization statement; 
the while (determination condition) { 
    loop body; 
    stepping statement; 
} 
* / 
public  class Demo10While {
     public  static  void main (String [] args) {
         int . 1 = I; // 1. initializer 
        the while (I <=. 3) { // 2. conditional 
            System.out.println ( "refueling" + I); // 3. loop 
            I ++; // 4. step into statement 
        } 
    } 
}

Results of the

Endless loop

Infinite loop: that is, the cycle conditions is always to true , the cycle of death is never-ending cycle. For example: the while (to true) {} In the later development, there will be an infinite loop use scenarios, such as: we need to read the input entered by the user, but the user input and how much data we do not know, can only use the death cycle, when the user does not want to enter your data, you can end the cycle, and how to end an infinite loop it, you need to use to jump out of the statement. 

Illustrate the cycle of death and precautions

Code Example
/ * 
Never not stop the cycle, called an infinite loop. 

Infinite loop standard format: 
the while (to true) { 
    loop body 
} 
* / 
public  class Demo16DeadLoop {
     public  static  void main (String [] args) {
         the while ( to true ) { 
            System.out.println ( "the I Love the Java!" ); 
        } 

        // System.out.println ( "the Hello"); never to execute the statement can not be written 
    } 
}

 

 
 
 
 

 

Guess you like

Origin www.cnblogs.com/wurengen/p/11521056.html