Break Statement in Java

by Jasleen Chhabra | Updated on 24 August 2024

Table of Contents (Click any Topic to view first)

  1.  Syntax of the Break Statement
  2. Example 1: Using Break in a for Loop
  3. Example 2: Using Break in a while Loop
  4. Example 3: Using Break in a switch Statement

Understanding the Break Statement in Java

In Java programming, the break statement provides a mechanism to prematurely exit a loop or switch statement. It's particularly useful for terminating loop execution based on certain conditions or when a specific task is completed. Let's explore the break statement in Java with detailed explanations and examples.

Syntax of the Break Statement:

The break statement is simple in its syntax and usage:

break;

When encountered inside a loop or switch statement, the break statement immediately terminates the loop or switch, and the program continues with the statement following the loop or switch.

Example 1: Using Break in a for Loop

public class BreakInForLoop {

    public static void main(String[] args) {

        for (int i = 1; i <= 5; i++) {

            System.out.println("Iteration " + i);

            if (i == 3) {

                break; // Exit the loop when i is 3

            }

        }

        System.out.println("Loop exited");

    }

}

In this example, the for loop iterates from 1 to 5, printing each iteration number. However, when i becomes 3, the break statement is encountered, causing the loop to terminate prematurely. As a result, the program prints "Loop exited" after exiting the loop.

Example 2: Using Break in a while Loop

public class BreakInWhileLoop {

    public static void main(String[] args) {

        int i = 1;

        while (i <= 5) {

            System.out.println("Iteration " + i);

            if (i == 3) {

                break; // Exit the loop when i is 3

            }

            i++;

        }

        System.out.println("Loop exited");

    }

}

Similar to the previous example, this code demonstrates the use of the break statement within a while loop. The loop exits when i equals 3, and the program prints "Loop exited" afterward.

Example 3: Using Break in a switch Statement

public class BreakInSwitchStatement {

    public static void main(String[] args) {

        int day = 4;

        switch (day) {

            case 1:

                System.out.println("Sunday");

                break;

            case 2:

                System.out.println("Monday");

                break;

            case 3:

                System.out.println("Tuesday");

                break;

            default:

                System.out.println("Unknown day");

                break;

        }

    }

}

In this example, the break statement is used within a switch statement to exit the switch block after executing the corresponding case. Once the appropriate case is found and executed, the break statement ensures that the switch block is exited, preventing fall-through to subsequent cases.

Conclusion:

The break statement in Java provides a means to control loop and switch execution flow by prematurely exiting the enclosing loop or switch. Whether used in for loops, while loops, or switch statements, the break statement offers flexibility and control in program flow.

 


FAQ

Any Questions?
Look Here.

Related Articles

Abstraction in Java

Binding in Java

Classes & Objects in Java

Collections Framework in Java

Comments in Java

Continue Statement in Java

Control Statements in Java

Data Types in Java

Do While-Loop in Java

Encapsulation in Java

Exception Handling in Java

For-Loop In Java

Hello World Program in Java

If-else Statement in Java

Inheritance in Java

Introduction to Java

Java Database Connectivity (JDBC)

Java Development Tools and Frameworks

Java GUI (Graphical User Interface) Programming

Java I/O

Java Vs. C++

Methods and Constructors in Java

Multithreading in Java

Object Oriented Programming in Java

Operators in Java

Polymorphism in Java

Scanner Class in Java

Setting Up Java Environment

Static Keyword in Java

Super Keyword in Java

Switch Statement in Java

This Keyword in Java

Variables in Java

While-Loop in Java