Self Java loop for the day03_Java

Cycle concept

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

for 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
  • ③ execution of specific statements
  • ④ After cycling, the cycling conditions involving variables change

demand

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

Code

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

Results of the

Execution Flow

Nested loop concept

Nested loops : a loop means is another circulation loop. For example, there is also a for loop for loop is nested loops. The total number of cycles * = number of cycles within the outer loop
Nested loop format:

Process execution nested loops:
  • Execution order: ①②③④⑤⑥> ④⑤⑥> ⑦②③④⑤⑥> ④⑤⑥
  • Once outside the loop, the inner loop several times.
  • Skipping example: total jump 5 groups of 10 hops. Group 5 is an outer loop, the inner loop is 10.

demand

* Print No. 5 line per line 8

Code

public  class Demo09ForNext {
     public  static  void main (String [] args) {
         // outer loop 5, within 8 cycles 
        for ( int I = 0; I <5; I ++ ) {
             for ( int J = 0; J <8 ; J ++ ) {
                 // not a new line asterisk 
                of System.out.print ( "*" ); 
            } 
            // after printing cycle 8 the asterisk, needs a line feed 
            System.out.println (); 
        } 

    } 
}

Results of the

 

 

 

 

Guess you like

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