6

do-while:

do
{ 
    i++; 
    ++j;
    System.out.println( i * j );

}
while ((i < 10) && (j*j != 25));

I am learning about do-while vs while at the moment and would like to rewrite the above java fragment (already declared and initialized) using a while instead. Are the below rewritten codes correct way to do so:

while:

while ((i < 10) && (j*j != 25))
{
    i++;  
    ++j;
    System.out.println( i * j );
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
user3003605
  • 385
  • 4
  • 6
  • 13
  • 4
    The difference is *when* the condition check occurs, of course. In a do-while, the body is always executed *at least once*. Otherwise, there really is nothing to say. – user2864740 Nov 18 '13 at 09:59
  • 3
    `while` is an *entry check loop*, whereas `do-while` is an *exit-check loop*. And **NO**, what you mentioned is not the right way to interchange the loop, while having the functionality intact. – Rahul Nov 18 '13 at 09:59
  • 1
    dont compare by execution , compare by definition first , then you'll know where to expect the change – Hussain Akhtar Wahid 'Ghouri' Nov 18 '13 at 10:00
  • Thank you for explaining this so quickly guys, much appreciated. – user3003605 Nov 18 '13 at 10:14

4 Answers4

19

The difference between a do-while and a while is when the comparison is done. With a do-while, you'll compare at the end and hence do at least one iteration.

Equivalent code for your example

do
{ 
    i++; 
    ++j;
    System.out.println( i * j );

}
while ((i < 10) && (j*j != 25));

is equivalent to:

i++; 
++j;
System.out.println( i * j );
while ((i < 10) && (j*j != 25)) {
    i++; 
    ++j;
    System.out.println( i * j );
}

General comprehension

A do-while loop is an exit controlled loop which means that it exits at the end. A while loop is an entry controlled loop which means that the condition is tested at the beginning and as a consequence, the code inside the loop might not even be executed.

do {
    <block>
} while (<condition>);

is equivalent to:

<block>
while (<condition>) {
    <block>
};

Use case

A typical use case for a do-while is the following: you ask the user something and you want do repeat the operation while the input is not correct.

do {
   // Ask something
} while (input is not correct);

In that case, you want to ask at least once and it's usually more elegant than using a while which would require either to duplicate code, or to add an extra condition or setting an arbitrary value to force entering the loop the first time.

At the opposite, while loops are much more commons and can easily replace a do-while (not all languages have both loops).

Maxime Chéramy
  • 17,761
  • 8
  • 54
  • 75
  • Whilst it shows for this example how it is done, it misses the point that the logic needs to be rethought. – Peter_James Nov 18 '13 at 10:10
  • @user2860598 I tried to take into consideration your comment. Is it better? Do you think something else should be explained? – Maxime Chéramy Nov 18 '13 at 10:31
  • @user2860598 I suppose what I was trying to get it, is how do you change from to the other...which the only way to explain is very examples. Good post :D – Peter_James Nov 18 '13 at 10:35
  • I know this is old but, why do you need to repeat the whole block outside the while? Why not just have the declaration and initialization of the variables? Like `int i = 0, j = 0; while ((i < 10) && (j*j != 25)) { ... }` – Chazy Chaz Feb 28 '20 at 16:25
3

The key difference between do-while and while, with do-while you are guaranteed at least one run of your code before the checks.

*It does not need to get anymore complicated than that.

Peter_James
  • 647
  • 4
  • 14
2

No, the two codes are not equivalent. do-while only checks the condition at the end of the loop so i++, ++j and System.out.println(i * j) happen at least once regardless of the initial values of i and j. while skips the entire loop if the condition isn't true for the first time the loop is entered.

You can achieve the same effect with while by copying the loop's body once before the loop, for instance.

kviiri
  • 3,282
  • 1
  • 21
  • 30
1

Do-While loops are pretty useful for cases like these, where you ask the user a question, and the user has to answer it for the program to terminate. For example, this code:

import java.util.Scanner;

public class DoWhile {

    public static void main(String[] args) {
        int answer;
        do {
            System.out.println("What is 5+5?");
            Scanner scan = new Scanner(System.in);
            answer = scan.nextInt();
        } while (answer != 10);
    }
}
dat3450
  • 954
  • 3
  • 13
  • 27
ViceroyFaust
  • 162
  • 1
  • 1
  • 12