Stephen D
Stephen D

Reputation: 3076

In Java, when is a "do while" loop the only option?

Is there ever a situation where you must use a do while loop? Is it an accepted practice? It seems like it is equivalent to a plain while loop except that its first iteration happens before checking the conditional, if that is even true.

int i = 3;
while ( i > 0 ) {  // runs 3 times
    i--;
}

vs

int j = 3;
do {
    j --;
} while ( j > 0 ); // runs 3 times

The same?

EDIT: I have seen the java doc, but the example in the java docs doesn't look like it requires that the particular routine inside of the do while loop must be run in the do while loop instead of inside of a regular while loop!

Upvotes: 2

Views: 5642

Answers (6)

user902383
user902383

Reputation: 8640

basic difference between while and do-while is do while will be executed at least once.

when do-while is best option?

in case when you want to execute some actions till you meet condition, of course you could achieve same thing by using while but early termination of loop with break, is nasty and ugly solution

Upvotes: 1

auhuman
auhuman

Reputation: 972

A real time example. There is a contest with 5 level. In each level if you score 100 you can proceed to next level. Less code for do while, but not for while.

boolean playContest()
{//do while
    int level = 1;
    int score;
    do
    {
        score = 0;
        score = play();
    }while(score>99 && level++<6)
    if(level>4 && score>99)
        isWinner = true;
    else
        isWinner = false;
    return isWinner;
}

boolean playContest()
{//while
    int level = 1;
    int score;
    while(level <6)
    {
        score = 0;
        score = play();
        if(score < 100)
            break;
        level++;
    }
    if(level>4 && score>99)
        isWinner = true;
    else
        isWinner = false;
    return isWinner;
}

Upvotes: 1

Omar Mainegra
Omar Mainegra

Reputation: 4184

This example maybe help you be clearer:

int i = 3;

System.out.print("while:    ");
while (--i > 0){
    System.out.print("x");
}

System.out.print("\ndo-while: ");

int j = 3;
do
{
    System.out.print("x");
}while (--j > 0);

This prints

while:    xx
do-while: xxx

Upvotes: 2

nikdeapen
nikdeapen

Reputation: 1601

No, there is no time a do-while loops is the only option, it is used for convenience when you do not want to repeat code.

Upvotes: 0

arshajii
arshajii

Reputation: 129507

Is there ever a situation where you must use a do while loop?

No: every do-while loop can be written as a while-loop by running the body once before the loop begins. However, there are certainly cases where it makes more sense to use the do-while construct (i.e. if you always want the loop to iterate at least once), which is why it exists in the first place.

Is it an accepted practice?

If you use it appropriately, then yes absolutely.

It seems like it is equivalent to a plain while loop except that its first iteration happens before checking the conditional, if that is even true.

That's right. You can read more about do-while in its tutorial.

Upvotes: 9

Suresh Atta
Suresh Atta

Reputation: 121998

When you want to execute the statement inside do for at least once, then you can go for it.

Directly from Docs

The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once,

do {
     statement(s)
} while (expression);

Upvotes: 0

Related Questions