CodeGym /Courses /Java Syntax Zero /do-while loop

do-while loop

Java Syntax Zero
Level 4 , Lesson 8
Available

1. Reverse loop

Java has another kind of while loop — the do-while loop. It's very similar to the ordinary while loop and also consists of only two parts: a "condition" and a "loop body". The loop body is executed over and over again as long as the condition is true. In general, a do-while loop looks like this:

do
   statement;
while (condition);

or

do
{
   block of statements
}
while (condition);

For a while loop, the sequence of execution is: condition, loop body, condition, loop body, condition, loop body, ...

But for a do-while loop, it is slightly different: loop body, condition, loop body, condition, loop body, ...

In fact, the only difference between a while loop and do-while loop is the fact that the loop body is executed at least once for a do-while loop.


2. Benefits of using a do-while loop

Basically, the only difference between a do-while loop and a while loop is that the body of a do-while loop is executed at least once.

Generally, a do-while loop is used when it makes no sense to check the loop condition if the loop body has not been executed. For example, if certain calculations are performed in the loop body and then used in the condition.

Example:

The program reads lines from the keyboard until the word exit is entered

while do while
String s;
while (true)
{
   s = console.nextLine();
   if (s.equals("exit"))
      break;
}
String s;
do
{
   s = console.nextLine();
}
while (!s.equals("exit"));

The break and continue statements in a do-while loop work in the same way as in a while loop.


3. Comparing do-while loops: Java vs Pascal

Once again, Pascal has an analogue of the do-while loop, but it is called a repeat-until loop. Also, it is slightly different from the do-while loop. In a repeat-until loop, the condition indicates when to exit the loop rather than when to continue it.

Examples:

Pascal Java
 
Repeat
   ReadLn(s);
Until s = 'exit';
 
String s;
do {
   s = console.nextLine();
}
while ( !s.equals("exit") );

Compared to Java, the way Pascal represents this is downright beautiful. We have to start with examples from Pascal, otherwise you'll laugh.


4
Опрос
Loops,  4 уровень,  8 лекция
недоступен
Loops
Loops
Comments (11)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Juan Carlos Gil Level 20, Medellín, Colombia
18 October 2023
Because of this exercise, I am about to have a sentiment of euphoria.
Anonymous #11270891 Level 16, United States of America, United States
5 May 2023
I set a counter and then decremented it. int counter = ( i <= 0 || i >= 5) ? 1 : i;
Anonymous #11225109 Level 5, United States of America, United States
5 February 2023
I updated the code by this; {s= "abc" ; System.out.println(s);} and also While(count=4);
Chrizzly Level 12, Earth, Germany
11 August 2022
Does anyone know why the counter here has to be while

( number > 0 && number < 4 );
4 instead of 5? If you type in 5, then the condition should be untested, even if I say number < 5 (means max. 4), why does it display the text 5 times then, instead of 1 times? Maybe I do a fundamental mistake here. Thank you, guys.
Eggnoch Level 17, United States
28 February 2023
In the "do" part of the loop, the code subtracts one from your number variable, which means that, in order to make the condition match the number, you must make the counter one lower as well.
Yurko Level 7
27 June 2022
Check requirement number 4
Yurko Level 7
27 June 2022
this is not correct solution
Chrizzly Level 12, Earth, Germany
11 August 2022
Mhm, it seems to be correct. The do-while loop will be executed once, no matter what the condition says. Then it checks the condition, and if it should be displayed only once for the numbers (number <= 0 && number >= 5), then you want the condition to be false - and leave it with only one execution. Once the number entered meets the counter-event (number > 0 && number < 4), then it should go into the loop and count it down. Usually I would have preferred a usual while loop here, but it's an excercise :)
7 September 2022
yes
KassidyPrice Level 14, United States of America, United States
8 October 2022
No this solution is not correct. It needs to be either: while (number > 0 && number < 5) or while (number > 0 && number <= 4) other wise if the variable number = 5 then the statement only runs once and the condition becomes false.
Antonina Dominiak Level 6, Poland, Poland
12 July 2023
it actually does say it's supposed to only print it once if the number is equal to (or greater than) 5 :)