what behaviour can I expect when I run this code:
do while(testA) {
// do stuff
} while(testB);
Will it behave like:
do {
while(testA) {
// do stuff
}
} while(testB);
Or:
if(testA) {
do {
// do stuff
} while(testA && testB);
}
Or something totally unexpected?
I ask this question because I think this is quite ambiguous, and for other people searching on this topic, not because I am lazy to test it out.
The while loop in java executes one or more statements after testing the loop continuation condition at the start of each iteration. The do-while loop, however, tests the loop continuation condition after the first iteration has completed.
Java do-while loop is used to execute a block of statements continuously until the given condition is true. The do-while loop in Java is similar to while loop except that the condition is checked after the statements are executed, so do while loop guarantees the loop execution at least once.
The do-while loop is very similar to that of the while loop. But the only difference is that this loop checks for the conditions available after we check a statement. Thus, it is an example of a type of Exit Control Loop.
The while loop will only execute of the conditions are met. Whereas the do while loop will execute the first time without verifying the conditions, not until after initial execution.
It is equivalent to your first block:
do {
while(testA) {
// do stuff
}
} while(testB);
The relevant parts of the Java grammar when parsing this are:
DoStatement:
do Statement while ( Expression ) ;
Statement:
WhileStatement
WhileStatement:
while ( Expression ) Statement
Statement:
Block
Block:
{ BlockStatements_opt }
You can see that the Java compiler will parse this as do <WhileStatement> while ( Expression ) ;
. That's the only valid way to parse the code that you wrote.
Keep in mind that it doesn't have any special rule to parse this construct. It just ends up being confusing for a human to read due to the unusual way the do-while loop is written. In normal usage do-while is always written as do { ... } while
with explicit curly braces.
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 ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With