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?
- First, it runs the
docode block. - Then it checks the
whilecondition. - If the condition is
true, the block repeats. - If the condition is
false, the loop ends.
do while syntax
do {
// The code block runs before the condition is checked
} while (sąlyga);It only shows where to write the repeated code and where the actual condition is checked.
- Java first enters the
doblock without checking any condition beforehand. - After executing every statement in the block, Java reaches the
while. - If the condition is
true, the program returns to the start of thedoblock. - If the condition is
false, the loop ends and the code following it runs.
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");Plaunamas puodelis. Liko: 3
Plaunamas puodelis. Liko: 2
Plaunamas puodelis. Liko: 1
Visi puodeliai išplauti
- The variable
neplautiPuodeliaiinitially stores the number3. doblock runs immediately and displays the current number of cups.- The statement
neplautiPuodeliai--reduces their number by one. - At the bottom,
whilechecks whether the number is still greater than zero. If so, the block repeats. - 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);Kambaryje yra 25 °C
- The variable
kambarioTemperaturastores the value25. - Java first runs the
doblock, so the temperature appears in the console once. - Only then is the condition checked:
kambarioTemperatura < 20. - The comparison
25 < 20isfalse, so the loop does not repeat. - This is the key difference: a regular
whilewould have skipped this block, whiledo whileran 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);do while blokas
- The variable
kiekisis initially0. - A regular
whilefirst checks0 > 0. The condition is false, so its text does not appear. do whilefirst displays the text do while blokas.- Only then does it check the same false condition and decide not to repeat the block.
| Loop | When is the condition checked? | Minimum number of iterations |
|---|---|---|
while | Before the code block | 0 |
do while | After the code block | 1 |
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.
If it always remains true, do while will also become an infinite loop.