Computer Applications
Answer
do-while loop | while loop |
---|---|
do-while is an exit-controlled loop. | while is an entry-controlled loop. |
do-while loop checks the test condition at the end of the loop. | while loop checks the test condition at the beginning of the loop. |
do-while loop executes at least once, even if the test condition is false. | while loop executes only if the test condition is true. |
do-while loop is suitable when we need to display a menu to the user. | while loop is helpful in situations where number of iterations is not known. |
Related Questions
Convert the following for loop statement into the corresponding while loop and do-while loop:
int sum = 0; for (int i= 0; i <= 100; i++) sum = sum + i;
What is an empty statement? Explain its usefulness.
How many times are the following loop bodies repeated? What is the final output in each case?
int y = 1; while (y < 10) if (y % 2 == 0) System.out.println(y++);
How many times are the following loop bodies repeated? What is the final output in each case?
int x = 1; while (x < 10) if(x % 2 == 0) System.out.println(x);