JAVA LOOPS

Java for loop

for loop is useful when you know the number of repetitions in advance or want to update a counter consistently. All loop control information appears on one line.

for loop syntax

for (inicializavimas; sąlyga; pakeitimas) {
    // Repeated code block
}
This syntax template produces no console output.

It simply shows the three places for the actual counter, condition and update.

The three parts of for loop control
PartWhen does it run?Purpose
InitializationOnce before the loopCreates or sets the counter
ConditionBefore each iterationDecides whether to run the block
UpdateAfter each iterationChanges the counter's value
  1. Initialization prepares the counter. For example, int diena = 1.
  2. Condition asks before each repetition whether the loop can continue.
  3. If the condition is true, Java runs the code inside the braces.
  4. Update updates the counter, then the condition is checked again.

Counting four days

Imagine we want to number four days:

for (int diena = 1; diena <= 4; diena++) {
    System.out.println("Diena: " + diena);
}
Console output

Diena: 1
Diena: 2
Diena: 3
Diena: 4

  1. Before the loop starts, a counter is created: diena, whose initial value is 1.
  2. Java checks diena <= 4. The first check is 1 <= 4, so the condition is true.
  3. The current day value is displayed in the console.
  4. diena++ adds one, so the counter's next value is 2.
  5. This repeats until day 4. When the counter becomes 5, the condition 5 <= 4 is false and the loop ends.

The counter can change by more than one

This loop lists the even house numbers on one side of a street:

for (int namoNumeris = 2; namoNumeris <= 10; namoNumeris += 2) {
    System.out.println("Namo numeris: " + namoNumeris);
}
Console output

Namo numeris: 2
Namo numeris: 4
Namo numeris: 6
Namo numeris: 8
Namo numeris: 10

  1. The counter namoNumeris starts at the first even number, 2.
  2. After each number is displayed, the statement namoNumeris += 2 adds two.
  3. The counter therefore moves as follows: 2, 4, 6, 8, 10 and skips the odd numbers.
  4. After 10 the counter becomes 12. The condition 12 <= 10 is false, so the loop stops.

Accumulating a result in a loop

Ona reads 10 pages of a book each day for five days. A loop can calculate the total:

int perskaitytaPuslapiu = 0;

for (int diena = 1; diena <= 5; diena++) {
    perskaitytaPuslapiu += 10;
}

System.out.println("Perskaityta puslapių: " + perskaitytaPuslapiu);
Console output

Perskaityta puslapių: 50

  1. Initially, perskaitytaPuslapiu is 0, because Ona has not started reading yet.
  2. The loop runs five times, once for each day.
  3. On each iteration, += 10 adds another 10 pages to the previous result.
  4. The value grows as follows: 10, 20, 30, 40, 50.
  5. When the loop ends, println() displays the final accumulated value, 50.

A decreasing counter

Jonas needs to carry four shopping bags from the car. The counter can decrease using --:

for (int maiseliai = 4; maiseliai >= 1; maiseliai--) {
    System.out.println("Automobilyje liko maišelių: " + maiseliai);
}

System.out.println("Visi pirkiniai parnešti");
Console output

Automobilyje liko maišelių: 4
Automobilyje liko maišelių: 3
Automobilyje liko maišelių: 2
Automobilyje liko maišelių: 1
Visi pirkiniai parnešti

  1. The counter's initial value is 4, because there are four bags in the car.
  2. After each iteration, maiseliai-- reduces the counter by one.
  3. The console displays these values in order: 4, 3, 2, 1.
  4. When the counter becomes 0, the condition maiseliai >= 1 becomes false and the loop ends.
  5. Java continues after the loop and displays the text Visi pirkiniai parnešti.

When the condition is false from the start

The program should water the flowers only on days one through five, but the counter incorrectly starts at eight:

for (int diena = 8; diena <= 5; diena++) {
    System.out.println("Laistome gėles");
}
Console output

Nieko neišvedama

  1. The counter diena immediately receives the value 8.
  2. Before the first iteration, Java checks the condition 8 <= 5.
  3. The answer is false, so the loop's println() never runs.
  4. The console therefore remains empty. This is not an error: the loop simply had no valid iterations.

for or while?

Choose forwhen repetitions are naturally controlled by a counter or their number is known. while is usually clearer when the number of repetitions is unknown and the loop depends on a changing program state.