Difference for, while, do..while of three loop

Distinguish three loop statements

  • Control statements that variable conditions controlled, at the end of the for loop, you can not be accessed, while the end of the while loop 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.
  • If the conditional never been satisfied, then the for loop and while loop will be executed zero times, but do-while loop will execute at least once.

Code Example

public  class Demo13LoopDifference {
     public  static  void main (String [] args) {
         for ( int I =. 1; I <0; I ++ ) { 
            System.out.println ( "the Hello" ); 
        } 
        // System.out.println (I ); // this line is wrong wording! Because the variable i is defined within a for loop parentheses, only for circulation to their own use. 

        int I =. 1 ;
         do { 
            System.out.println ( "World" ); 
            I ++ ; 
        } the while (I <0 );
         //Now beyond the scope of do-while loop, we can still use the variable i 
        System.out.println (i); // 2 
    } 
}

Results of the

 

Guess you like

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