Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When would a do-while loop be the better than a while-loop?

This is a highly subjective question, so I'll be more specific. Is there any time that a do-while loop would be a better style of coding than a normal while-loop?

e.g.

int count = 0;
do {
   System.out.println("Welcome to Java");
   count++;
} while (count < 10);`

It doesn't seem to make sense to me to check the while condition after evaluating the do-statement (aka forcing the do statement to run at least once).

For something simple like my above example, I would imagine that:

int count = 0; 
while(count < 10) { 
   System.out.println("Welcome to Java"); count++;
}

would be generally considered to have been written in a better writing style.

Can anyone provide me a working example of when a do-while loop would be considered the only/best option? Do you have a do-while loop in your code? What role does it play and why did you opt for the do-while loop?

(I've got an inkling feeling that the do-while loop may be of use in coding games. Correct me, game developers, if I am wrong!)

like image 396
AlexMTMorgan Avatar asked Dec 09 '13 13:12

AlexMTMorgan


3 Answers

If you want to read data from a network socket until a character sequence is found, you first need to read the data and then check the data for the escape sequence.

do
{ 
   // read data
} while ( /* data is not escape sequence */ );
like image 113
Enigma Avatar answered Oct 06 '22 01:10

Enigma


The while statement continually executes a block of statements while a particular condition is true

while (expression) {
     statement(s)
}

do-while evaluates its expression at the bottom of the loop, and therefore, the statements within the do block are always executed at least once.

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

Now will talk about functional difference,

while-loops consist of a conditional branch instructions such as if_icmpge or if_icmplt and a goto statement. The conditional instruction branches the execution to the instruction immediately after the loop and therefore terminates the loop if the condition is not met. The final instruction in the loop is a goto that branches the byte code back to the beginning of the loop ensuring the byte code keeps looping until the conditional branch is met.

A Do-while-loops are also very similar to for-loops and while-loops except that they do not require the goto instruction as the conditional branch is the last instruction and is be used to loop back to the beginning A do-while loop always runs the loop body at least once - it skips the initial condition check. Since it skips first check, one branch will be less and one less condition to be evaluated.

By using do-while you may gain performance if the expression/condition is complex, since it is ensured to loop atleast once. In that casedo-while could call for performance gain

Very Impressive findings here, http://blog.jamesdbloom.com/JavaCodeToByteCode_PartOne.html#while_loop

like image 20
Satheesh Cheveri Avatar answered Oct 06 '22 00:10

Satheesh Cheveri


The do-while loop is basically an inverted version of the while-loop.

It executes the loop statements unconditionally the first time.

It then evaluates the conditional expression specified before executing the statements again.

int sum = 0;
int i = 0;
do
{
    sum += ids[i];
    i++;
} while (i < 4);

Reference material

like image 23
Anthony Russell Avatar answered Oct 06 '22 00:10

Anthony Russell