JAVA LOOPS
Java while loop
while repeats a code block while the specified condition is true. It is especially useful when you do not know in advance how many repetitions are needed.
Why use loops?
A loop lets you write an action once and run it many times. This reduces repeated code, makes changes easier and helps avoid copying errors.
while checks a boolean condition before every iteration. When its result becomes false, the loop ends and the program continues after it.
while syntax
while (sąlyga) {
// Repeated code block
}It simply shows where to put the actual condition and repeated Java statements.
- Java first checks the condition in parentheses.
- If the condition is
true, the code inside the braces runs. - Upon reaching the end of the block, Java returns to the top and checks the condition again.
- When the condition becomes
false, the loop stops and the program continues after it. - A value affecting the condition must change inside the loop, otherwise the repetition may never end.
A cookie box example
Jonas eats cookies one at a time until the box is empty:
int sausainiai = 3;
while (sausainiai > 0) {
System.out.println("Dėžutėje liko sausainių: " + sausainiai);
sausainiai--;
}
System.out.println("Sausainių nebeliko");Dėžutėje liko sausainių: 3
Dėžutėje liko sausainių: 2
Dėžutėje liko sausainių: 1
Sausainių nebeliko
- Initially, the variable
sausainiaistores the number3. - Java checks
sausainiai > 0. Since3 > 0, the loop body runs. - The console displays the current number, and
sausainiai--reduces it by one. - The same repeats with the values
2and1. - When the value becomes
0, Java exits the loop and displays Sausainių nebeliko.
A loop with several conditions
Ona washes dishes while there are dirty dishes and water is available:
int neplautiIndai = 3;
boolean yraVandens = true;
while (neplautiIndai > 0 && yraVandens) {
System.out.println("Plaunamas indas");
neplautiIndai--;
}
System.out.println("Indai išplauti");Plaunamas indas
Plaunamas indas
Plaunamas indas
Indai išplauti
- Initially there are
3dirty dishes, andyraVandensvalue istrue. - The operator
&&means “and”, so both conditions must be true. - One dish is washed on each iteration, and
neplautiIndai--reduces their number. - The block runs three times, so the text Plaunamas indas appears three times.
- When the dirty dish count reaches
0, the loop ends and displays Indai išplauti.
If yraVandens were false, the loop would not start even if there were still dirty dishes in the sink.
When the initial condition is false
Because while checks its condition before running the block, the loop may never run:
int laisvosVietos = 0;
while (laisvosVietos > 0) {
System.out.println("Registruojamas dalyvis");
laisvosVietos--;
}
System.out.println("Registracija nepradėta");Registracija nepradėta
- The variable
laisvosVietosis initially0. - Before the first iteration, Java checks
laisvosVietos > 0. - The comparison
0 > 0isfalse, so the registration block is skipped. - The program continues after the loop and displays Registracija nepradėta.
- If the block must run at least once before the condition is checked, use a
do while.
The risk of an infinite loop
If the condition never becomes false, the loop never ends:
int sausainiai = 3;
// Error: the number of cookies does not change inside the loop
while (sausainiai > 0) {
System.out.println("Liko sausainių: " + sausainiai);
}Liko sausainių: 3
Liko sausainių: 3
Liko sausainių: 3
…
- The initial value is
3, so the conditionsausainiai > 0is true. - The loop displays text but never reduces the variable's
sausainiai. - On the next iteration, the value is still
3, so the condition is true again. - The program repeats the same line until we force it to stop.
- Adding
sausainiai--would decrease the number and allow the loop to end normally.
In this example, you need to decrease sausainiai or define a clear exit condition, for example using break.