Loops

Basic Java
≈ 4 min read

When an action needs to be repeated

Imagine that you need to print the numbers from 1 to 100. You could write one hundred calls to System.out.println(), but that code would be long, repetitive, and inconvenient to change. A loop lets you describe a repeated action once and specify while or how many times it should run.

Java most commonly uses three loop forms: while, do-while, and for. They solve the same general task — repeating a block of code — but are convenient in different situations.

The while loop

while is useful when repetition should continue while a certain condition is true. The condition is checked before the loop body runs, so the loop may not run even once.

int remaining = 3;

while (remaining > 0) {
    System.out.println(remaining);
    remaining--;
}

System.out.println("Go!");

First, remaining > 0 is checked. If the condition is true, the loop body runs, and then the check is repeated. When the value becomes zero, the condition becomes false and execution continues after the loop.

Keep track of what changes the loop condition. If you write while (remaining > 0) but never decrease remaining, the condition will remain true and the loop will become infinite.

The do-while loop

do-while is similar to while, but the condition is checked after the loop body. That means the body always runs at least once.

int attempts = 0;

do {
    System.out.println("Attempt " + attempts);
    attempts++;
} while (attempts < 3);

The code inside do runs first, and then the condition is checked. If it is true, the next iteration begins. Notice the semicolon after while (attempts < 3); — it is part of the do-while syntax.

The for loop

for is especially convenient when there is a counter and you already know how it should change from one iteration to the next. The loop header keeps together the initial action, the continuation condition, and the action performed after each iteration.

for (int i = 1; i <= 3; i++) {
    System.out.println(i);
}

Here, int i = 1 runs once before the loop starts. Before each iteration, i <= 3 is checked, and after the body runs, i++ is executed. As a result, the numbers 1, 2, and 3 are printed to the console.

A counter declared in the for header exists only inside that loop. After the closing brace, i can no longer be accessed.

break and continue

A loop normally ends when its condition becomes false. Sometimes it needs to be stopped earlier. For that, Java uses break.

for (int number = 1; number <= 10; number++) {
    if (number == 4) {
        break;
    }

    System.out.println(number);
}

As soon as number becomes 4, break immediately ends the nearest loop. The code after the loop continues to run normally.

continue does something different: the current iteration stops, but the loop itself continues with the next iteration.

for (int number = 1; number <= 5; number++) {
    if (number == 3) {
        continue;
    }

    System.out.println(number);
}

The number 3 will not be printed: on that iteration, continue skips the rest of the loop body. The for loop then moves to the next counter value.

A loop inside a loop

Loops can be nested inside one another. In that case, for each iteration of the outer loop, the inner loop goes through all of its own iterations.

for (int row = 1; row <= 2; row++) {
    for (int column = 1; column <= 3; column++) {
        System.out.println(row + ":" + column);
    }
}

The outer loop runs twice. On each of its iterations, the inner loop runs three times, so the System.out.println() line runs six times in total. This pattern is useful when an action needs to be repeated across two levels of counters.