Break Statement in Java with Examples

In this article, we will be looking at the break statement using Java programming language; as we proceed through the article, we will look at various topics like the use of the break statement and its syntax, along with a flowchart.

We will also be looking at programs that use break statements in loops, switch statements, and if-else statements. The best way to learn is to practice the codes shown in the article and try some variations of the same codes to master the concept.

What is the break statement in Java, and what is it used for?

As the name suggests, the break statement is used to terminate a loop. Upon encountering a break statement in a loop, the loop iterations terminate there and exit from the loop, going to the first statement immediately after the loop.

The break statement can also be used in switch statements to terminate a sequence. This means that when a case is matched, only that case will be executed. After the case is executed, the break statement will ensure that the switch case is terminated, and the next line after the switch case will be executed.

Honestly speaking, the break statement is used as a ‘formal’ version of got0. You will understand why both are similar once we look at some examples of the break statement being used in multiple scenarios.

Syntax and Flowchart depicting the use of break:

Actually, there is no syntax for the break statement. In order to use a break statement in Java programming language or most other programming languages, all you have to do is write the word break and follow it with a semicolon, as shown below:

break;

To better understand how the break statement works in a loop, let us take a look at a flowchart:

break statement java

Usage of break statements

Now that we have a good understanding of how the break statement works let’s look at how to use it in different situations.

1. Using “break” in switch statements:

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

Output:

FirstCode

In this example, notice that there is a break statement at the end of each case. This is because when the variable passed into the switch is matched with a specific case, only the code in that specific case will be executed. The others will not be executed because the break statement brings the compiler out of the switch case (in this case, to the line System.out.println(x);).

2. Using “break” in a for loop:

public class FirstCode
{
   public static void main(String args[]) 
   {
      int [] numbers = {1, 2, 3, 4, 5};

      for(int x : numbers ) {
         if( x == 4 ) {
            break;
         }
         System.out.print( x + "  ");
      }
   }
}

Output:

1 2 3

In this example, we have an array of integers. In the for loop, we have a condition that if the number is 4, the loop will be terminated. Therefore, the output will only print until 3. When X is equal to 4, the break statement will take the program out of the for loop and go to the line “System.out.print(x + ” “);”.

3. Using “break” as a goto statement

As mentioned above, the break statement is essentially a civilized form of a goto statement. Here is an example illustrating how the break statement can also be used as a goto statement:

class FirstCode
{
    public static void main(String args[])
    {
        boolean t = true;
        first : 
        {
            second : 
            {
                third : 
                {
                    System.out.println("Before break (In the third label)");
                    if (t) {break second;}
                    System.out.println("This line will not execute");
                }
                System.out.println("This line will not execute");
            }
            System.out.println("After break statement (In the first label)");
        }
    }
}

Output:

Before break (In the third label)
After the break statement (In the first label)

In this example, we have 3 labels (which are basically blocks of code). The break statement is in the 3rd label, and it acts as a goto statement that says to go to the 2nd label. Hence, the program only prints the line before the break statement and the line after the second label (the print statement in the first label)

4. Using the break statement in a while loop

import java.util.Scanner;
class FirstCode 
{
    public static void main(String[] args) 
    {
        Double number, 
        Double sum = 0.0;
        Scanner input = new Scanner(System.in);
        while (true) 
        {
            System.out.print("Enter any number: ");
            number = input.nextDouble();
            if (number < 0.0) {break;}
           sum += number;
        }
        System.out.println("The sum is " + sum);
    }
}

Output:

Enter a number: 9.2
Enter a number: 8
Enter a number: 2.4
Enter a number: -3
The sum is 19.6

In the provided example, the condition of the while loop is always met. However, when the user inputs negative numbers, the while loop is terminated. This is because when the number is entered, i.e., negative, the program enters the if statement, and the break statement makes the program exit from the while loop.

5. Using the break statement in a do-while loop

public class FirstCode 
{  
    public static void main(String[] args) 
    { 
        int i=1; 
        do
        {  
            if(i==7){i++;break;}  
            System.out.print(i + "  ");  
            i++;  
        }
        while(i<=10);  
    }  
} 

Output:

1 2 3 4 5 6

Conclusion

You have now mastered the break statements as you have seen their uses and the syntax of the break statement, along with a flowchart. We have also discussed the usage of the break statement as we saw programs of its implementation in switch statements, loops, and also as a goto statement.

Leave a Reply

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