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
}
This syntax template produces no console output.

It simply shows where to put the actual condition and repeated Java statements.

  1. Java first checks the condition in parentheses.
  2. If the condition is true, the code inside the braces runs.
  3. Upon reaching the end of the block, Java returns to the top and checks the condition again.
  4. When the condition becomes false, the loop stops and the program continues after it.
  5. 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");
Console output

Dėžutėje liko sausainių: 3
Dėžutėje liko sausainių: 2
Dėžutėje liko sausainių: 1
Sausainių nebeliko

  1. Initially, the variable sausainiai stores the number 3.
  2. Java checks sausainiai > 0. Since 3 > 0, the loop body runs.
  3. The console displays the current number, and sausainiai-- reduces it by one.
  4. The same repeats with the values 2 and 1.
  5. 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");
Console output

Plaunamas indas
Plaunamas indas
Plaunamas indas
Indai išplauti

  1. Initially there are 3 dirty dishes, and yraVandens value is true.
  2. The operator && means “and”, so both conditions must be true.
  3. One dish is washed on each iteration, and neplautiIndai-- reduces their number.
  4. The block runs three times, so the text Plaunamas indas appears three times.
  5. When the dirty dish count reaches 0, the loop ends and displays Indai išplauti.
What if there were no water?

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");
Console output

Registracija nepradėta

  1. The variable laisvosVietos is initially 0.
  2. Before the first iteration, Java checks laisvosVietos > 0.
  3. The comparison 0 > 0 is false, so the registration block is skipped.
  4. The program continues after the loop and displays Registracija nepradėta.
  5. 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);
}
Console output repeats endlessly

Liko sausainių: 3
Liko sausainių: 3
Liko sausainių: 3
…

  1. The initial value is 3, so the condition sausainiai > 0 is true.
  2. The loop displays text but never reduces the variable's sausainiai.
  3. On the next iteration, the value is still 3, so the condition is true again.
  4. The program repeats the same line until we force it to stop.
  5. Adding sausainiai--would decrease the number and allow the loop to end normally.
Check what changes the loop condition.

In this example, you need to decrease sausainiai or define a clear exit condition, for example using break.