Nearby lessons

26 of 125

Java - break Statement

📌 What You Will Learn
  • The small building blocks of Java: tokens
  • All 8 primitive data types with their ranges
  • Type casting — implicit and explicit
  • Control statements: if, switch, loops
  • Arrays — single, double and jagged
  • Variable length arguments (var-args)

break Statement is a core concept of the Java language. This lesson explains break and continue and break and continue — With Labels with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

break and continue

  • break — immediately stops the loop (or switch) completely.
  • continue — skips the current round and jumps to the next round of the loop.
Example01
JCode Cell
1class BreakContinue {
2 public static void main(String[] args) {
3 for (int i = 1; i <= 5; i++) {
4 if (i == 3) break; // stops at 3
5 System.out.print(i + " ");
6 }
7 System.out.println();
8 for (int i = 1; i <= 5; i++) {
9 if (i == 3) continue; // skips 3
10 System.out.print(i + " ");
11 }
12 }
13}
Output
1 2 1 2 4 5

break and continue — With Labels

break stops a loop entirely. continue skips the current round. Both apply to the nearest loop by default.

Labeled break and continue

In nested loops, break/continue affect only the inner loop. To control the outer loop, give the outer loop a label and use break label; / continue label;.

Example02
JCode Cell
1class BreakContinue {
2 public static void main(String[] args) {
3 for (int i = 1; i <= 5; i++) {
4 if (i == 3) break; // stops at 3
5 System.out.print(i + " ");
6 }
7 System.out.println();
8 for (int i = 1; i <= 5; i++) {
9 if (i == 3) continue; // skips 3
10 System.out.print(i + " ");
11 }
12 }
13}
Output
1 2 1 2 4 5

break and continue — With Labels

Trainer's Note: A statement immediately after break or continue inside the same block is an unreachable statement — the compiler rejects it. This is a favourite error to show in exams.
Example03
JCode Cell
1class Labeled {
2 public static void main(String[] args) {
3 l1: for (int i = 0; i < 3; i++) {
4 for (int j = 0; j < 5; j++) {
5 if (j == 2) continue l1; // jump to the NEXT i
6 System.out.print(i + "" + j + " ");
7 }
8 }
9 }
10}
Output
00 01 10 11 20 21
📝 Key Takeaways
  • Tokens are the smallest pieces of a program: identifiers, literals, keywords, operators, separators.
  • Java has 8 primitive types: byte, short, int, long, float, double, char, boolean.
  • Widening casting is automatic and safe; narrowing casting needs brackets and may lose data.
  • if / switch decide which path runs; for / while / do-while repeat code; break and continue control loops.
  • Arrays hold many same-type values; index starts at 0; size is fixed; for-each loop reads them simply.
  • Var-args (int... x) lets a method take any number of arguments.

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1