0

if i++ = 11, this will end the for loop , but I am confused why i = 11 will be stored and printed out at the next println

public class kkk {
    public static void main(String args[]) {
        int i;
        for (i = 1; i < 11; i++) {
            if (i > 5)
                continue;
            System.out.println(i);
        }
        System.out.println(i);
    }
}
5
  • 1
    When i++ is 11, it doesn't mean that i wont be updated to 11, it just ends the loop. Also you have println outside for loop which prints 11! Commented May 19, 2017 at 6:42
  • 2
    When i == 11 you end the loop, no surprise that after the loop, i == 11 . Commented May 19, 2017 at 6:42
  • i need to be incremented to 11 to give the condition i<11 a false. So it seems legit that it will keep it value. The for loop don't reverse the last incremental block before exiting it. It would be messy Commented May 19, 2017 at 6:46
  • Why did you use the last println function.? Commented May 19, 2017 at 6:51
  • @NEKIBURRAHMAN I saw this question on internet. Commented May 20, 2017 at 18:08

9 Answers 9

2

To explain the logic of the for loop let compare it to a while

for (
    i = 1;   //Block of initialisation
    i < 11;  //condition
    i++      //Block of incrementation

){
    if (i > 5)
        continue;
    System.out.println(i);
}

This would look like

{ //Block of initialisation
    i = 1
}
while(i < 11){ //condition
    { //Block of code
        if (i > 5)
            continue;
        System.out.println(i);

    }
    { //Block of incrementation
        ++i;
    }
}

Here you will see exactly the order of each block of the for loop. So indeed, i will be equal to 11 when it exit the loop.

Note : The block statements are not really present, it is used to show where everything is present. Because of course, the variable declare in the initialisation would not exist outside the block

Sign up to request clarification or add additional context in comments.

1 Comment

Books usually forget to mention how loops actually work no wonder people sometimes get confused, this should be the explanation always when it comes to for loop.
1

Because you are declaring the i variable outside the scope of for loop, means it will retail its value even after for loop has ended.

Clearly for loop ended when the value of i was 11, hence after for loop it`s printing 11

Comments

1

Terminating condition of the loop isn't i++ == 11, but i < 11. Incrementing is done after at the end of the cycle before checking the condition.

Compare it yourself to this kind of cycle

int i = 0;
while (i++ < 11) {
    if (i > 5)
        continue;
    System.out.println(i);
}
System.out.println(i);

4 Comments

Why the last out put is 12??
Because i++ < 11 is still true for i == 10 and is checked last time for i == 11 (where iis being incremented to 12)
I am confused , if i == 10 , i++ should be 11 < 11??
i++ < 11 is true for i == 10 and right after the condition is checked, the i is incremented to 11. Then body of the cycle executes and terminating condition is checked again, now for i == 11, where 11 < 11 is now false but i is again immediately incremented to 12. Compare it yourself with while (++i < 11)
1

As soon as the actual value of i is no longer less than 11, the loop terminates.

In your example, i will start as 1, then continue to be incremented with each iteration. When i is equal to 10, it will be incremented to 11 when the loop executed. Thereafter, i is already equal to 11 when the loop condition is checked. Hence, the loop terminates at the println() prints out 11 - the current value of i.

Comments

1
   int i; // A
   for (i = 1; i < 11; i++) {
       if (i > 5)
           continue;
       System.out.println(i);
   }
   System.out.println(i); // B

In Line B, you are printing the value of i are you not?

i's value does not just disappear after the for loop ended. After the for loop ended, i stores 11, and so it will continue to store 11 until you change it.

This is because in line A, you declared i. If you had declared i again in the for loop, then the i in the loop is a different i from the i outside the loop and will not have the value 11.

Comments

1

Basically because that's how the for loop works.

When i == 10 it will do the work (in your case printing i), but it will not exit the loop.

After that, it will increment becoming 11 making the condition i < 11 invalid and then it will exit the loop.

Comments

1

when main method run i initial value is 1 but when end for loop i = 11 then interpreter go end System.out.println(i);

enter image description here

int i; // initial value 1
        for (i = 1; i < 11; i++) {
            if (i > 5)
                continue;
            System.out.println(i);
        }
// now i value is 11 so system out printed 11
        System.out.println(i);
    }

Comments

-1

You have a println() outside the for loop which is printing 11.

Comments

-1
public class kkk {
    public static void main(String args[]) {
        int i;
        for (i = 1; i < 10; i++) {
            if (i > 5)
                continue;
            System.out.println(i);
        }
        System.out.println(i);
    }
}

Try this one. it will tell you how the programme is executing.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.