Break Statement in Java with Examples

The break statement in Java is a powerful control flow statement used to exit a loop or switch block immediately before its normal termination condition becomes false. Once the break statement executes in the program:

  • Control jumps outside the loop or switch block.
  • Remaining statements inside the block are skipped.

Java supports two types of break statement:

  • Unlabeled break
  • Labeled break
Learn break statement in Java with syntax and basic to advanced examples.

Basic Syntax of Break Statement in Java

The break statement in Java has a very simple syntax, but its behavior depends on where it is used.

Syntax 1: Unlabeled Break Statement

break;

In the above syntax of unlabeled break statement:

  • The keyword break is written in lowercase. It does not take any argument.
  • It is used inside:
    • for loop
    • while loop
    • do-while loop
    • switch statement
  • When break statement executed within the block:
    • The current loop or switch block terminates immediately.
    • Program control moves to the next statement after the loop or switch.
  • Mostly used when a specific condition is met and no further execution is required.

Syntax 2: Labeled Break Statement

break labelName;

In the above syntax:

  • labelName is a user-defined identifier, which must be defined before the loop statement.
  • The break statement with labelName is mainly used with nested loops.
  • When it is executed inside the loops:
    • Terminates the loop associated with the given label.
    • Control jumps directly outside the labeled block.

Syntax of Label Declaration

labelName:
loop_statement {
// code
}

In the above syntax:

  • The label ends with a colon (:).
  • Labels can be applied only to loops and blocks.
  • It improves control flow in deeply nested structures.

Where Can We Use the Break Statement?

The break statement is mainly used in:

Read Also: Top Java Interview Questions with Answer

Basic Example of Break Statement

Let us some important example programs based on using break statement in Java

Example 1: Using Break in for Loop

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

Output:

1
2

In this example:

  • The loop starts from the counter variable i = 1.
  • When the value of i becomes 3, the break statement executes.
  • The loop terminates immediately and numbers 3, 4, and 5 are not printed on the console.

Example 2: Using Break in while Loop

public class BreakWhileLoop {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
if (i == 4) {
break;
}
System.out.println(i);
i++;
}
}
}

Output:

1
2
3

In this example:

  • The counter variable i is set to 1. Therefore, the loop starts from i = 1 and executes as long as i <= 5.
  • When the value of i == 4, break statement executes and stops the loop for further execution.
  • The program control moves outside the loop and remaining iterations are skipped.

Example 3: Using Break in do-while Loop

public class BreakDoWhile {
public static void main(String[] args) {
int i = 1;
do {
if (i == 2) {
break;
}
System.out.println(i);
i++;
} while (i <= 5);
}
}

Output:

1

In this example:

  • The do-while loop executes at least once.
  • When the value of i == 2, the break statement terminates the loop and stops even though condition is still true.

Example 4: Using Break in Switch Case

public class BreakSwitch {
public static void main(String[] args) {
int number = 2;
switch (number) {
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
case 3:
System.out.println("Three");
break;
default:
System.out.println("Invalid number");
}
}
}

Output:

Two

In this example:

  • Since the value of number is 2, matching case 2 executes.
  • The break statement prevents execution of other cases.
  • Without break, fall-through would occur in the program.

Example 5: Labeled Break in Nested Loops

public class LabeledBreak {
public static void main(String[] args) {
outerLoop:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
break outerLoop;
}
System.out.println(i + " " + j);
}
}
}
}

Output:

1 1

In this example:

  • The outerLoop is a label for the outer loop.
  • When the value of j == 2, the break outerLoop executes.
  • Both inner and outer loops terminate and the program control jumps outside the labeled loop.

Error Coding Examples

Example 1:

public void invalidBreak() {
if (true) {
break; // COMPILE ERROR: break outside loop or switch
}
}

The compile-time error occurs because break statement can only be used within loops or switch statements.

Example 2:

int value = 1;
switch (value) {
case 1:
System.out.println("One");
// Missing break here causes fall-through
case 2:
System.out.println("Two");
break;
}
// Output: One Two (unexpected fall-through)

This is common error when break statement is missing and causes unintentional fall-through behavior. So, you should always include break statement unless fall-through is intentional.

Best Practices for Using Break Statement

There are the following key points for using break statement that you should keep in mind. They are:

  • You should use the break statement for natural loop termination when possible.
  • Always use meaningful names for labels, such as searchLoop, matrixIteration, etc.
  • Always try to avoid deep nesting. If you need labeled breaks frequently, consider refactoring.
  • Comment when omitting break in switch is deliberate.
  • Avoid overusing labeled break.

Frequently Asked Questions (FAQ)

1. What is the use of break statement in Java?

The break statement in Java is used to immediately terminate a loop or switch statement.

2. Can we use break without loop in Java?

No. Using break outside a loop or switch causes a compile-time error.

3. What is labeled break in Java?

A labeled break allows you to exit from an outer loop in nested loop scenarios.

4. Is break mandatory in switch statement?

No, but without break, fall-through occurs when executing multiple cases.

5. What happens after break executes?

Control transfers to the statement immediately following the loop or switch block.

Please Share Your Love.

Leave a Reply

Your email address will not be published. Required fields are marked *