The difference between self-day03_Java three loop in Java

1 difference:

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. Recommended for use when the known number of cycles, when an unknown number of cycles is recommended while.

2 difference

Variables for loop in parentheses among definition, only the inner loop can be used. while loops and do-while loop initialization statement already outside, so after the cycle can continue to use it.

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. 
        System.out.println ( "================" ); 

        int I =. 1 ;
         do { 
            System.out.println ( "World" ); 
            I ++ ; 
        } the while (i <0);
         // now is beyond the scope of the 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/11521071.html