Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java do while, while

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.

like image 239
Pindatjuh Avatar asked Mar 12 '10 17:03

Pindatjuh


People also ask

What is difference between while and do while in Java?

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.

Do while in do while Java?

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.

What is while and do while?

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.

Which is better do while or while 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.


2 Answers

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.

like image 117
John Kugelman Avatar answered Sep 20 '22 02:09

John Kugelman


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 ;)

like image 32
Felix Kling Avatar answered Sep 23 '22 02:09

Felix Kling