JAVA LOOPS

Java do while loop

do while is while loop variant that runs the code block first and only then checks the condition. Its body therefore always runs at least once.

How does do while work?

  1. First, it runs the do code block.
  2. Then it checks the while condition.
  3. If the condition is true, the block repeats.
  4. If the condition is false, the loop ends.

do while syntax

do {
    // The code block runs before the condition is checked
} while (sąlyga);
This syntax template produces no console output.

It only shows where to write the repeated code and where the actual condition is checked.

  1. Java first enters the do block without checking any condition beforehand.
  2. After executing every statement in the block, Java reaches the while.
  3. If the condition is true, the program returns to the start of the do block.
  4. If the condition is false, the loop ends and the code following it runs.
The semicolon after while (sąlyga) is required.

Unlike a regular while block, the do while statement ends with ;.

Washing cups example

Ona has three cups to wash. After washing each cup, she checks whether any remain:

int neplautiPuodeliai = 3;

do {
    System.out.println("Plaunamas puodelis. Liko: " + neplautiPuodeliai);
    neplautiPuodeliai--;
} while (neplautiPuodeliai > 0);

System.out.println("Visi puodeliai išplauti");
Console output

Plaunamas puodelis. Liko: 3
Plaunamas puodelis. Liko: 2
Plaunamas puodelis. Liko: 1
Visi puodeliai išplauti

  1. The variable neplautiPuodeliai initially stores the number 3.
  2. do block runs immediately and displays the current number of cups.
  3. The statement neplautiPuodeliai-- reduces their number by one.
  4. At the bottom, while checks whether the number is still greater than zero. If so, the block repeats.
  5. After the third cup, the value becomes 0, the condition is false and the program displays Visi puodeliai išplauti.

When the condition is false from the start

Even if the condition is false immediately, the do block has already run:

int kambarioTemperatura = 25;

do {
    System.out.println("Kambaryje yra " + kambarioTemperatura + " °C");
} while (kambarioTemperatura < 20);
Console output

Kambaryje yra 25 °C

  1. The variable kambarioTemperatura stores the value 25.
  2. Java first runs the do block, so the temperature appears in the console once.
  3. Only then is the condition checked: kambarioTemperatura < 20.
  4. The comparison 25 < 20 is false, so the loop does not repeat.
  5. This is the key difference: a regular while would have skipped this block, while do while ran it once.

The difference between while and do while

int kiekis = 0;

while (kiekis > 0) {
    System.out.println("while blokas");
}

do {
    System.out.println("do while blokas");
} while (kiekis > 0);
Console output

do while blokas

  1. The variable kiekis is initially 0.
  2. A regular while first checks 0 > 0. The condition is false, so its text does not appear.
  3. do while first displays the text do while blokas.
  4. Only then does it check the same false condition and decide not to repeat the block.
while and do while loop comparison
LoopWhen is the condition checked?Minimum number of iterations
whileBefore the code block0
do whileAfter the code block1

When should you choose do while?

This loop is useful when you need to do something at least once before checking whether to continue:

  • display a menu or message;
  • request user input;
  • display the current value before deciding whether to repeat the action;
  • make an initial attempt before deciding whether to try again.
As with a while loop, the condition's state must change.

If it always remains true, do while will also become an infinite loop.