Coding/Learn/java/break continue
Java Foundational Course
01.Introduction to Java
02.Your First Java Program
03.Syntax & Code Structure
04.Comments in Java
05.Variables & Declaration
06.Primitive Data Types
07.Numbers & Arithmetic Operators
08.Strings in Java
09.Booleans & Relational Operators
10.Type Casting & Conversion
11.Scanner & Interactive Input
12.Logical Operators (&&, ||, !)
13.If Statements
14.If-Else Statements
15.If-Else-If Ladders
16.Switch Statements
17.While Loops
18.Do-While Loops
19.Standard For Loops
20.Break & Continue Statements
21.Arrays in Java
22.Array Indexing & Traversal
23.Enhanced For-Each Loop
24.Essential String Methods
25.Methods in Java
26.Method Parameters & Arguments
27.Method Overloading
28.Classes & Objects in Java
29.Constructors in Java
30.Exception Handling (try-catch-finally)
31.Capstone Mini Projects
Course Certificate0%

Complete all lessons to earn

LearnBreak & Continue Statements
20 / 31 0%
Lesson 20 of 31
5 mins read

Break & Continue Statements

PreviousNext Lesson
In Plain English

`break` and `continue` give you fine-grained control over loop execution. `break` terminates the entire loop immediately, while `continue` aborts only the current iteration and jumps directly to the next cycle.

The Mental Model

"Think of reading a playlist: `break` means stopping the music player completely and walking away. `continue` means pressing the "Skip Track" button to start the next song immediately."

Deep Dive: How It Works

`break`: Immediately halts the enclosing loop and transfers control to the line following the loop.

`continue`: Skips whatever remaining lines are in the current iteration and advances to the loop update step.

Labeled breaks: Java allows labeling outer loops (e.g. `outer: for(...)`) to break out of nested loops directly.

Syntax Blueprint

for (int i = 1; i <= 10; i++) {
    if (i == 3) continue; // skip 3
    if (i == 7) break;    // stop at 7
    System.out.println(i);
}

Use continue to skip unwanted items and break for early termination on matches.

PointCore Rules to Remember

Point star
Break Exits Loop: Stops all further iterations of the loop.
Point star
Continue Skips Cycle: Jumps directly to the next iteration step.
Point star
Works Across All Loops: Usable in while, do-while, and for loops.

Common Beginner Traps & How to Fix Them

Mistake:Putting code after break or continue in the same block

Why it happens: Java compiler detects this as "unreachable statement" and will reject the build.

How to fix: Ensure break and continue are the last statements executed in that branch.

Pro Tip: Use break when searching through data: once you find the target item, break immediately to save processing time.

Live Interactive Example

Hit Run Code to see it live
Filtering and Early Termination
Java 13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Output Console
Click "Run Code" to view the rendered output.
How it works: 1, 3, 5 are skipped with continue. 2, 4, 6 print. When i becomes 8, break terminates the loop.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Stop loop when target found

  • PointIterate from 1 to 10.
  • PointIf i == 4, print "Target 4 Found!" and break.
1
2
3
4
5
6
7
8
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates