Java break, continue and Nested Loops
break ends the loop, continue ends one pass. Six programs: stop at 5, find the first failing score, skip 5, the while-loop continue trap, a 3 by 3 times grid, and why a break in the inner loop leaves the outer one running.
What you will learn
- Stop a loop early with break and skip one pass with continue
- Use break to stop searching once you've found what you need
- Avoid the continue trap that freezes a while loop
- Build a grid with nested loops and know which loop break leaves
Before you start
- Java loops: for, while, do-while and for-each
You know how loops run. Now you'll steer them: stop one early with break, skip a single pass with continue, and put one loop inside another.
break: the loop ends right there
package org.studyeasy.s02l11;
public class Ex01_Break {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
System.out.println("i = " + i);
}
System.out.println("loop finished");
}
}
i = 0
i = 1
i = 2
i = 3
i = 4
loop finished
The header sets up ten passes, but at i == 5 the break leaves the loop. The line after the loop still runs: only the loop stopped, not the program.
Find it, then stop looking
package org.studyeasy.s02l11;
public class Ex02_FindFirstFail {
public static void main(String[] args) {
int[] scores = {72, 85, 38, 91, 45};
for (int i = 0; i < scores.length; i++) {
System.out.println("Checking " + scores[i]);
if (scores[i] < 40) {
System.out.println("First fail: " + scores[i]);
break;
}
}
}
}
Checking 72
Checking 85
Checking 38
First fail: 38
That's break in real code. (For now, think of scores as a list of numbers; arrays get their own article.) Once a score below 40 turns up, there's no reason to keep looking, so 91 and 45 are never checked. Remove the break and the loop checks them anyway.
continue: skip one pass, keep looping
package org.studyeasy.s02l11;
public class Ex03_Continue {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
System.out.println("i = " + i);
}
}
}
i = 0
i = 1
i = 2
i = 3
i = 4
i = 6
i = 7
i = 8
i = 9
At 5, continue skips everything below it in the body, so nothing prints for that pass, and the loop goes on to 6. Move the println above the if and 5 prints too, because the skip comes too late.
The continue trap in a while loop
A for loop runs i++ for you after every pass, even after continue. A while loop doesn't. If its i++ sits at the bottom of the body, continue jumps over it: i stays 2 and the loop never ends (it prints i = 0, i = 1, then hangs). Update first, then continue:
package org.studyeasy.s02l11;
public class Ex04_WhileContinue {
public static void main(String[] args) {
int i = 0;
while (i < 5) {
if (i == 2) {
i++;
continue;
}
System.out.println("i = " + i);
i++;
}
}
}
i = 0
i = 1
i = 3
i = 4
Side by side
break | continue | |
|---|---|---|
| Effect | leaves the whole loop now | skips the rest of this pass |
| Works in | loops and switch | loops only |
| In nested loops | acts on the nearest loop, the one it sits in | |
| Outside any loop | compile error | |
Nested loops: rows and columns
package org.studyeasy.s02l11;
public class Ex05_NestedGrid {
public static void main(String[] args) {
for (int row = 1; row <= 3; row++) {
for (int col = 1; col <= 3; col++) {
System.out.print(row * col + " ");
}
System.out.println();
}
}
}
1 2 3
2 4 6
3 6 9
The outer loop gives the rows; the inner loop gives the columns. For every pass of the outer loop, the inner loop runs completely: three rows times three columns is nine prints. print (without ln) stays on the same line, and the println() after the inner loop ends the row. Delete that line and all nine numbers land on one line.
break leaves only the inner loop
package org.studyeasy.s02l11;
public class Ex06_InnerBreak {
public static void main(String[] args) {
for (int r = 1; r <= 3; r++) {
for (int c = 1; c <= 3; c++) {
if (c == 2) {
break;
}
System.out.println("r=" + r + " c=" + c);
}
}
}
}
r=1 c=1
r=2 c=1
r=3 c=1
The break fires when c reaches 2 and leaves the c loop only. The outer loop keeps going, so every row still starts and prints just c=1. Put if (r == 2) break; outside both loops and javac says "break outside switch or loop".
Recap
breakends the loop;continueends one pass.- Use
breakto stop searching once you've found the answer. - In a
whileloop, update the counter beforecontinue. - A nested loop runs the inner loop fully for every outer pass.
breakandcontinueact on the loop they sit in.
Try it yourself: change the grid to print a 5 by 5 times table, then add a continue that skips the diagonal (where row == col).
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program
Install JDK 25 (Microsoft Build of OpenJDK) with JAVA_HOME, install IntelliJ IDEA, then point a run configuration at the JDK and the main class and run Hello World.
Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA
Open IntelliJ's AI Chat, pick Claude Agent (the agent behind Claude Code) or Copilot and a model, ask in plain words for Main.java to print the date and time, review the change, and run it.
Java Hello World with Maven: What the JDK Does and What Maven Adds
javac compiles HelloWorld.java into bytecode and java runs it on the JVM. Maven adds one pom.xml, the same folders in every project and one build command. Then a real Maven Hello World in IntelliJ.
Java Variables, Keywords and Naming Rules
A variable is a labelled jar that holds one value at a time. Four small programs show declaring, reassigning, the names Java accepts, the keywords it reserves, and why daysUntilLaunch beats d.
