简体   繁体   中英

I can't understand the output of the code fragment. can some one explane it to me please?

for(int i = 10; i > 0; i--) {
        if (i > 7) 
            continue;

        while (i > 3) {
            if(i == 5)  
                break;

            System.out.println(--i);
        }
        System.out.println(i);
    }

The output says 6 5 5 3 3 2 1 I am mainly confused starting from the while part.

for(int i = 10; i > 0; i--) {
    if (i > 7) //until the third loop this will skip the whole "for" statement, thus the for cycle starts at i = 7
        continue;

    while (i > 3) { //the first time it loops i will be 7, so the while statement will be executed;
        if(i == 5)  // i is still 7 so this is false
            break;

        System.out.println(--i); // this will autodecrement i. In this case it will subtract 1 from i first before using it once again in the code; i will be i = 7 - 1 => i = 6
    }
    System.out.println(i); 
}

Becasue the while cycle is inside the for loop, it will execute itself until the statement is false. So after i = 7 (passing the first if statement) it will execute the while until:

  • i becomes less than 3.
  • i is equal to 5.

So the first time it executes :

while (i > 3) {
    if(i == 5)  
        break;

    System.out.println(--i); // After we subtract 1 form i (which is 7) we output 6; 
    //Then we return in the beggining of the while loop. i is 6 so "while" is executed. We subtract 1 and output 5.
}

Because i is now 5 the while loop won't be executed so we go on along the code. We reach the end of the for statement and output i again which after the subtraction is 5 now. Because the for loop ended, 1 is subtracted from i , making i = 4 . The while block is now executed again. We subtract 1 from i and output it - now equal to 3. Because the 2nd condition of executing the while loop is false - it isn't executed getting us to the System.out.println(i); after which we output 3. Then because the while loop is no longer executed we subtract and output i until the for loop's 2nd statement is false.

Part below ensures the numbers 10-8 are skipped.

    if (i > 7) 
            continue;

This part keeps processing "i" until it reaches 3 but breaks out of "while" when it comes across 5.

    while (i > 3) {
            if(i == 5)
                break;

            System.out.println(--i);
        }

Here goes like this:

        i= 10,9,8 => skipped
        // *************************
        i=7          
           //inside while
              printed : 6   // due to --i in  println(--i)
              i=6
              printed : 5  // due to --i in  println(--i)
              i=5
              //breaks out while => i==5  
           printed : 5
           i=4 // due to i-- in for statement
        // ************************* 
        i=4  
           printed : 4  
           //inside while
              printed : 3 // due to --i in  println(--i)
              i=3  
              //breaks out while => i > 3
           printed : 3  
           i=2 // due to i-- in for statement
        // ************************* 
        i=2  
           printed : 2  
           i=1 // due to i-- in for statement
        // ************************* 
        i=1 
           printed : 1  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM