It works indeed, but the behaviour depends on what conditions your are testing. E.g. this code:
int i = 2;
int j = 4;
do while(j > 0) {
i--; j--;
System.out.println("i:" + i + " j:" + j);
} while(i > 0);
outputs:
i:1 j:3
i:0 j:2
i:-1 j:1
i:-2 j:0
So it works like:
while(j>0) {
}
Whereas by exchanging the variable names:
do while(i > 0) {
//.. same as above
} while(j > 0);
The output is:
i:1 j:3
i:0 j:2
It looks like it behaves the same as in the first case (i.e. the first while is considered), but here, the application is not terminating!
Summary:
At the time when testA
is not satisfied anymore and testB
is also not satisfied, the code works like a normal while(testA){}
loop.
But: If, at the time when testA
is no longer satisfied, testB
is still satisfied, the loop is not executed any more and the script is not terminating. This only applies if the condition of the "outer" loop needs to be changed inside the loop.
Update:
And after reading other answer, I realize that this is exactly the behavior of the nested do-while
- while
loop.
Anyway, lesson learned: Don't use this kind of syntax because it can confuse you ;)