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 ?

Rohit Jain
  • 203,151
  • 43
  • 392
  • 509
UnderDog
  • 3,003
  • 10
  • 30
  • 47

6 Answers6

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
    }

}
fommil
  • 5,539
  • 6
  • 37
  • 77
Kevin Bowersox
  • 90,944
  • 18
  • 150
  • 184
  • [i++ increments by 1 but returns the original value](http://stackoverflow.com/questions/3831341), so the comparison check in 6 is `31 < 15` but `i == 32`. – fommil Aug 19 '13 at 11:48
  • @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. – Kevin Bowersox Aug 19 '13 at 11:51
  • yes, I was suggesting a minor improvement to your comments ;-) – fommil Aug 19 '13 at 11:51
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.

christopher
  • 25,939
  • 5
  • 53
  • 88
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

gifpif
  • 4,277
  • 3
  • 30
  • 45
2

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

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.

Eric
  • 1,254
  • 2
  • 15
  • 27
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