1

This is the class whose behaviour I am unable to understand.

class loop1 {
    public static void main(String args[]) {
        int i = 10;

        do
            while(i++ < 15) {
                System.out.println(i);
                i = i + 20;
                System.out.println(i);
            } 
        while(i<2);

        System.out.println(i);    
    }

}

I expected it to print

11
31
31

But it prints

11
31
32

I am not able to understand why this "32" has come up in the output.

This is my understanding of the flow

  • i = 10
  • In the while loop because of unary incremental, it becomes 11 so this explains the first output
  • 11 gets incremented to 31 by (+20)
  • Then 31 < 15 should fail (during the next iteration) so it should proceed to the last print statement and print 31, but it instead it is printing 32.

Can someone tell me what I am missing ?

4
  • 4
    For the same reason the first print statement print 11. 31 is increment before the last print statement is executed. Commented Aug 19, 2013 at 11:36
  • Thanks Rohit.You could have put this as answer..would have upvoted it :) Commented Aug 19, 2013 at 11:43
  • You're welcome :) Well, there are already 3 duplicate answers. Commented Aug 19, 2013 at 11:45
  • possible duplicate of Why does this go into an infinite loop? Commented Jul 20, 2014 at 9:15

6 Answers 6

5

During the final evaluation of the first while loop i++ still increments i even though the loop does not execute because the condition fails.

class loop1 {
    public static void main(String args[]) {
        //1.  i = 10
        int i = 10;

        do 
            // 2. while loop condition = (10 < 15), i = 11
            // 6. while loop condition = (31 < 15), i = 32
            while(i++ < 15) {
                System.out.println(i);  //3. prints 11
                i = i + 20; //4. i = 31
                System.out.println(i);  //5. prints 31
            } 

        while(i<2); //this really has no effect on the codes execution, given i values

        System.out.println(i); //7.  Prints 32
    }

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

3 Comments

i++ increments by 1 but returns the original value, so the comparison check in 6 is 31 < 15 but i == 32.
@fommil the expression evaluates 31 < 15, but after the evaluation i++ still increments i to 32. I think we are on the same page here.
yes, I was suggesting a minor improvement to your comments ;-)
3
i++

You're increasing the value by 1. The value, when you increase it after the first iteration is 31. 31 + 1 is, surprisingly, 32. And you print out the value directly after incrementing it.

Comments

2

In 2nd iteration when condition of while loop is check
while(i++<15)
at that time i is 31 so condition fail but i++ change the value of i 31 -> 32

Comments

2

while(i++ < 15) compare the value of i with and after that increment i by 1

Comments

1

My guess is:

while(i++ < 15)

The i++ increments the value from 31 to 32 on the second loop.

Note, the ++ will be executed even if the condition fails - which in your case, the 31 is greater than 15 (condition fails), however because of the ++ the value is incremented to 32, which is being printed out by the System.out at the end.

Comments

0

In the program :

 while(i++<15)

above statement is condition checking in this statement you are post increment i therefor 31+1=32 is comming

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.