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

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

difference between while and do-while loop in c++
difference between while and do-while loop in java
difference between while and for loop in c
do-while vs while
example of while and do-while loop
diff between do-while and while
difference between while and do while in php
in do while loop termination condition is checked at

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?


int count = 0;
do {
   System.out.println("Welcome to Java");
} 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!)

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.

   // read data
} while ( /* data is not escape sequence */ );

Difference Between While and Do-While Loop, What is the main difference between a while loop and a do while loop? The while loop is used to repeat a statement or a group of statements while a given condition is true. In do while loop, the condition is checked at the end of the execution of the statements inside the loop. The do while loop is similar to while loop but do while loop can execute at least once even though the condition is true or false.

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

while (expression) {

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 {
} 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

When do I use a for loop and when do I use a while loop in the , When should you use a while loop over a do loop? 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.

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;
    sum += ids[i];
} while (i < 4);

Reference material

Loops Part 10: do..while vs while (Java), loop will run a minimum of zero times this is also true for a for loop. Usually, an_____is a better choice than an exit-condition loop because the entry-condition loop checks before looping. null character and its code For example, suppose Listing 5.13 used do while instead of while.

Simply, when you want to check condition before and then perform operation while is better option, and if you want to perform operation at least once and then check the condition do-while is better. As per your question a working example, 1. when I needed to find the field which could be declared in the same class or the super class or the super class of that super class and so on i.e. finding the field located in deep class hierarchy. (A extends B B extends C and so on)

public Field SearchFieldInHierarchy(Object classObj, String fieldName )
    Field type = null;
    Class clz = classObj.getClass();
            type = clz.getDeclaredField(fieldName);
        } catch (NoSuchFieldException e)
            clz = clz.getSuperclass();
    } while(clz != null || clz != Object.class);        
    return type;

2. When reading input stream from Http response

        bytesRead = inputStream.read(buffer, totalBytesRead, buffer.length - totalBytesRead);
        totalBytesRead += bytesRead;
    } while (totalBytesRead < buffer.length && bytesRead != 0);

Do while loop, What happens when the condition is false in while loop? I used a for and while loop on a solid test machine (no non-standard 3rd party background processes running). I ran a for loop vs while loop as it relates to changing the style property of 10,000 <button> nodes. The test is was run consecutively 10 times, with 1 run timed out for 1500 milliseconds before execution:

/* while loop

5 bucks

1 chocolate = 1 bucks

while my money is greater than 1 bucks 
  select chocolate
  pay 1 bucks to the shopkeeper
  money = money - 1

come to home and cant go to while shop because my money = 0 bucks */

int main(){
  int money = 5;

  while( money >= 1){   
    printf("inside the shopk and selecting chocolate\n");
    printf("after selecting chocolate paying 1 bucks\n");
    money = money - 1 ;  
    printf("my remaining moeny = %d\n", money);

  printf("dont have money cant go inside the shop, money = %d",  money);

  return 0;

infinite money

while( codition ){ // condition will always true ....infinite loop

please visit this video for better understanding https://www.youtube.com/watch?v=eqDv2wxDMJ8&t=25s

When would a do-while loop be the better than a while-loop?, do - while loop allows you to ensure that the piece of code is executed at least once before it goes into the iteration. It would output Value is greater than 10 because the condition is tested after the loop is executed. After this it would not output anything further. sometimes one loop does the job better than the other. For instance if you were making a game and needed to make the game loop over and over, you could use a while loop that would run infinitely by using

Difference Between while and do-while Loop (with Comparison , Content: while Loop Vs do-while Loop. Comparison Chart The condition in while loop can be any boolean expression. When an expression  They are pretty much same except for do-while loop. The for loop is good when you have a counter kind of variable. It makes it obvious. while loop makes sense in cases where a flag is being checked as show below : while (!done) { if (some condtion) done = true; }

do…while loop vs. while loop in C/C++, Here we will see what are the basic differences of do-while loop and the while loop in C or C++.A while loop in C programming repeatedly  What is better – While or Do While/Until? Officially, the Do while/Until gives more flexibility than While loop. You may place the condition at first or at the end as using Do loops.

Difference between while and do-while loop in C, C++, Java , We use cookies to ensure you have the best browsing experience on our website​. The while loop can be thought of as a repeating if statement. do while loop is similar to while loop with the only difference that it checks for the condition after executing the Condition is checked first then statement(s) is executed. The only time you should use a do-while loop is when you want to execute the statements inside the loop at least once, even though condition expression returns false. Otherwise, it’s always better to use a while loop. Java while loop looks cleaner than a do-while loop. That’s all for java do while loop.