Java course

Break, continue, and nested loops

1.4.2 — Break, continue, and nested loops

break stops a loop early. continue skips the rest of one repetition. This lesson shows where execution goes after each.

A loop such as for (int i = 1; i <= 5; i = i + 1) visits the values 1 through 5. if (i == 3) checks whether the current value is 3. Braces group statements, and System.out.println(...) prints a value. All inputs are provided here.

Stop with break

When Java reaches break, it exits the nearest enclosing loop. Execution continues after that loop.

In the next cell, the check comes before printing. When i reaches 4, the loop stops without printing 4.

Skip one repetition with continue

continue skips the remaining statements in the current repetition. In a for loop, the update still runs, then the condition is checked again.

The next loop skips printing 3, but carries on with 4 and 5.

Keep the value, count, and total separate

  • value is the number currently being checked.
  • acceptedCount counts how many numbers were kept.
  • acceptedSum adds the kept numbers.

% gives a remainder. value % 2 == 0 is true for an even number. The next cell skips even numbers, keeping 1, 3, and 5.

Stop at a sentinel value

A sentinel is a value used to signal that processing should stop. Here, 4 is the sentinel. The loop visits numbers from 1 onward, but stops before adding 4.

The upper limit of 6 also bounds the loop if the sentinel is not encountered. This example uses generated numbers so you can focus on the stopping rule.

Update before a while-loop continue

A while loop has no automatic update. If continue skips your update, the same value can be checked forever.

The next cell prints odd numbers below 6. It updates nextValue before the skip check, so every repetition makes progress. value keeps the number for this repetition.

A loop inside another loop

A nested loop finishes its inner loop for each outer value. The next example has two rows and three columns. Its break stops only the inner loop when column is 3.

The outer loop still moves to row 2. Both loops have explicit limits.

Find a missing update

This read-only example would keep printing 1 because current never changes:

int current = 1;
while (current <= 3) {
    System.out.println(current);
}

To make it stop, put current = current + 1; inside the loop after printing. The runnable practice cell below includes a safety limit, so its unfinished version also stops.

If you run an endless loop, you must stop it yourself. Click Stop in the notebook toolbar, or choose Kernel → Restart kernel. Both terminate the loop and clear Java variables from memory; your code stays in the notebook. Fix the loop before running it again. Rerun any cells that define variables you need.

Practice: repair the update

Replace the TODO comment with practiceCurrent = practiceCurrent + 1;. Keep the other lines.

The unfinished cell prints 1 three times. After your fix, it should print 1, 2, and 3. attempts limits the example to three repetitions even before you fix it. && requires both conditions to be true.

Hint

Add practiceCurrent = practiceCurrent + 1; directly after the print statement. Do not remove the attempt limit.

Worked solution
int practiceCurrent = 1;
int attempts = 0;
while (practiceCurrent <= 3 && attempts < 3) {
    System.out.println(practiceCurrent);
    practiceCurrent = practiceCurrent + 1;
    attempts = attempts + 1;
}

Check stopping and skipping

In the sentinel example, change only stopValue:

stopValueExpected CountExpected Sum
100
436
9621

With 9, the sentinel is never encountered, but the loop still ends at its upper limit.

In your repaired practice cell, change only the starting practiceCurrent to 4. Its condition is immediately false, so it prints nothing.

Enable JavaScript to run the Java notebook.