do-while and while comparison

匆匆过客 提交于 2019-12-03 21:00:18

问题


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

Cheers


回答1:


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




回答2:


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.




回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/20044845/do-while-and-while-comparison

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!