Loading...

Java break and continue

Loop i from 1 to 5. When i equals 3, leave the loop with break before you print. The screen should show only two lines: 1, then 2. That early stop is one tool on this page. The other tool is continue, which skips one pass and then keeps looping so 3 never prints but 4 and 5 still do.

break ends the loop early

Create a small program file named Skip.java. The public class on the first line must match the file name: public class Skip. Do not leave an older Pulse or Beat class name in this file; the compiler expects the class name and the file name to agree.
Inside main, write a for that can stop early:
for (int i = 1; i <= 5; i = i + 1) {
if (i == 3) {
break;
}
System.out.println(i);
}
The for header is the same shape you already know: start, test, step. Inside the braces, an if asks whether i equals 3. When that is true, break; runs. break leaves the whole for loop at once. The println under the if does not run for that pass, and i never reaches 4 or 5 inside this loop. When i is 1 or 2, the if is false, so println(i) writes the number. Save the file before you compile.

Program that stops at break

This is the version you compile first.

break when i is 3

java
public class Skip {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i = i + 1) {
            if (i == 3) {
                break;
            }
            System.out.println(i);
        }
    }
}

From the folder that holds Skip.java, run javac Skip.java. When the parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Skip — not java Skip.java. Leave the .java ending off the second command. The same two commands work on Windows and on macOS; this page does not need a different shell spelling for either system. The program should print two lines and stop: 1, then 2. It should not ask you to type. If the terminal waits for keyboard input, a nextLine call from an older exercise is still in the file. Delete that call and keep the for with break above. A wrong screen line does not mean you need a new JDK install.

Walk the break run in order. i starts at 1. The if is false, so println writes 1. The step stores 2. The if is false, so println writes 2. The step stores 3. Now i == 3 is true, so break runs and the for ends. Nothing prints for 3, 4, or 5. A plain loop without break would have printed five lines; break cut the loop short after two prints. The step never runs after break on that pass, and the test is not asked again, because the loop is already over.

continue skips one pass

Change the body so continue replaces break, and keep the println after the if:
for (int i = 1; i <= 5; i = i + 1) {
if (i == 3) {
continue;
}
System.out.println(i);
}
When i equals 3, continue; skips the rest of that pass — including println — and the for moves on to the step and the next test. The loop itself does not end. The screen should show four lines: 1, 2, 4, and 5. The hole is where 3 would have been.

continue when i is 3

java
public class Skip {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i = i + 1) {
            if (i == 3) {
                continue;
            }
            System.out.println(i);
        }
    }
}

Compile and run again: javac Skip.java, then java Skip. You should see four lines. You did not change the for header — only break became continue. After you have seen four lines, you can put break back to return to the first demo. Both versions are valid Java. They teach two different early moves inside the same loop shape.

Read the continue run once more. i goes 1, 2, 3, 4, 5. For 1 and 2 the if is false and the numbers print. For 3 continue skips println. For 4 and 5 the numbers print again. The loop still finishes its normal stop when i becomes 6. continue never ends the for; it only skips the rest of one pass. After continue, the for still runs its step i = i + 1, so the counter keeps moving even though that pass did not print.

📊 tool and the screen

Tool at i == 3 Screen
break 1 then 2
continue 1 then 2 then 4 then 5

Use the table as a check after a run, not instead of one. break leaves two lines. continue leaves four lines with 3 missing. If 3 still appears when you meant continue, the println is probably above the if — move println below the continue so the skip can reach it. Fix the order rather than reinstalling Java.

Rules that keep the two tools clear

break and continue belong inside a loop body. Writing break; alone in main with no enclosing for or while makes javac fail. The same is true for continue;. On this page an if chooses the moment: when i equals 3. You can write other tests later; the demos keep i == 3 so the screen change is easy to see.

break is not continue. break ends the loop. continue skips one pass and stays in the loop. Neither tool is switch’s break from an earlier page in the same way you use it here — here the enclosing structure is a for. Do not leave a Pulse while that only counts, or a Beat for without an if, in the same main and wonder why every number from 1 to 5 still prints. This session’s main should hold the for plus the if with break or continue.

A common mix-up is printing the word itself: System.out.println("break");. That writes the characters break. It does not stop a loop. Another mix-up is putting println(i); before the if that holds continue, so 3 still prints and then continue runs with nothing left to skip. Keep the println after the if in both demos. If you write continue with no enclosing loop, javac reports an error; the same happens for a lone break outside any loop. These two words only make sense inside a loop body on this lesson’s shape.

What you can do now

You can put break inside a loop to leave early, or continue to skip one pass and keep going. An if can choose when that happens. The next page is for-each, where a loop walks each value in a collection without writing the start, test, and step by hand the same way.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top