Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

Java
  • Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program
  • Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA
  • Java Hello World with Maven: What the JDK Does and What Maven Adds
  • Java Variables, Keywords and Naming Rules
  • Maven in IntelliJ: pom.xml, Dependencies and the Build Lifecycle
  • Java Primitive Types and Memory: What int x = 5 Really Does
  • Java Strings and Text Blocks: Quotes, Concatenation and parseInt
  • Java Operators: Arithmetic, a++ vs ++a, Precedence, && || and the Ternary
  • Java Type Casting: Widening, Narrowing and Converting Text to Numbers
  • Java Practice Lab: A Cafe Bill with Variables, Operators and Casting
  • Java if, else and switch: Braces, Else-If Ladders and the Modern Switch
  • Java Loops: for, while, do-while and for-each
  • Java break, continue and Nested Loops
  • What's New in Java 21 to 25: Compact Source Files and Sequenced Collections
  • Java Arrays: Indexes, Default Values, Loops and 2D Grids
  • Java Methods and Overloading: Parameters, return and Pass-by-Value
  • Java Scope, Code Blocks and the Call Stack
  • Read User Input in Java with Scanner (and the Leftover Enter Trap)
  • Java Practice Lab: A Gym Weekly Tracker with Loops, Arrays and Methods
  • Debugging Java in IntelliJ: Stack Traces, Breakpoints and a Better AI Prompt
  • Java var: Let Java Infer the Type
  • Push and Pull in IntelliJ: Two Developers, One GitHub Repository, a Rejected Push and a Conflict
  • Java Classes and Objects: One Blueprint, Many Cars
  • Java Constructors and the this Keyword
  • Java Instance Methods, Getters, Setters and toString
  • Java static: Fields, Methods and Blocks Shared by the Whole Class
  • Java Packages and Imports: Every Class Has an Address
  • Java Access Modifiers and Encapsulation: Private Data, Public Methods
  • Java final and Immutable Objects: Assign Once, Share Safely
  • Java Strings as Objects: The String Pool, == vs equals, StringBuilder and printf
  • Java Practice Lab: A Library Book Catalogue with final Fields and StringBuilder
  • Java Inheritance: extends, super and the Order Objects Are Built
  • Java Method Overriding, @Override and the Object Class
  • Java Polymorphism: Upcasting, Dynamic Dispatch and Downcasting
  • Java Composition: Has-A vs Is-A
  • Java Abstract Classes: A Parent That Leaves One Blank
  • Java Interfaces: Contracts and Multiple Inheritance
  • Java Nested, Inner and Anonymous Classes
  • Java Enums with Fields, Methods and switch
  • Java Practice Lab: Payment Processing with an Interface, an Abstract Class and an Enum
  • Java Records: A Whole Data Class in One Line
  • Java Sealed Classes and Interfaces
  • Java Pattern Matching with instanceof and switch
  • Java Record Patterns: Unpacking a Record
  • Java equals and hashCode: Why a HashSet Needs Both
  • From Arrays to Collections: The Java Collections Framework Map
  • Java Wrapper Classes and Autoboxing
  • Java ArrayList: The Stretchy Array
  • Java LinkedList vs ArrayList: Nodes or an Array
  • Java Sets: HashSet, LinkedHashSet and TreeSet
  • Java Sorting with Comparable and Comparator
  • Java Maps: HashMap, LinkedHashMap and TreeMap
  • Java Stacks, Queues and Deques: ArrayDeque and PriorityQueue
  • Choosing a Java Collection: Immutable and Sequenced Collections
  • Java Exception Handling: try, catch and finally
  • Java throw, throws, Checked vs Unchecked and Custom Exceptions
  • Java try-with-resources and Clean Error Handling
  • Java Generics: Type-Safe Containers
  • Java Generic Methods, Bounded Types and Wildcards
  • Java Lambdas and Functional Interfaces
  • Java Built-in Functional Interfaces: Predicate, Function, Consumer, Supplier
  • Java Optional: Saying Maybe Without null
  • Java Streams I: filter, map and reduce
  • Java Streams II: Collectors, groupingBy and flatMap
  • Why Concurrency: Processes, Threads and the Main Thread in Java
  • Creating Threads in Java: Thread, Runnable and Lambdas
  • Java Thread Lifecycle: sleep, join and interrupt
  • Java Race Conditions and synchronized
  • Java volatile and Atomic Variables
  • Java ExecutorService and Thread Pools
  • Java Callable, Future and CompletableFuture
  • Java Locks, Deadlock and tryLock
  • Java Concurrent Collections and Virtual Threads
  • Why Slow Work Belongs off the Request Thread: 202 Accepted and a Job Id in Spring Boot
  • Virtual Threads in Spring Boot: One Property, and the Connection Pool Trap
  • @Async, Named TaskExecutors and Structured Concurrency in Spring Boot
  • Java Files and Directories with NIO.2: Path and Files
  • Reading and Writing Text Files in Java
  • Java Structured Data: Reading and Writing JSON and CSV
  1. Blog
  2. /
  3. Java
  4. /
  5. Basics

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.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026Basics

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.

Ten passes, 0 to 9. With break at 5, only 0 to 4 run and 5 to 9 never run. With continue at 5, only pass 5 is skipped and 6 to 9 still run. break at i == 5 continue at i == 5 01234 56789 01234 56789 never run skipped
break ends the whole loop; continue skips just one pass.

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

breakcontinue
Effectleaves the whole loop nowskips the rest of this pass
Works inloops and switchloops only
In nested loopsacts on the nearest loop, the one it sits in
Outside any loopcompile 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

  • break ends the loop; continue ends one pass.
  • Use break to stop searching once you've found the answer.
  • In a while loop, update the counter before continue.
  • A nested loop runs the inner loop fully for every outer pass.
  • break and continue act 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).

Keep going in Java · 13 of 79

Previous
Java Loops: for, while, do-while and for-each
Next post
What's New in Java 21 to 25: Compact Source Files and Sequenced Collections
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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.

On this page

  • break: the loop ends right there
  • Find it, then stop looking
  • continue: skip one pass, keep looping
  • The continue trap in a while loop
  • Side by side
  • Nested loops: rows and columns
  • break leaves only the inner loop
  • Recap
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.