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 ?