Java break keyword

The Java break keyword is used to terminate for, while, or do-while loop. It may also be used to terminate a switch statement as well.

Using break keyword is also called break statement.

1. Java break keyword syntax

Syntax is pretty much simple. Use break keyword with a semicolon (;). You can additionally use a label as well.

for(...)
{
	//loop statements

	break;
}

2. Types of break statement

A break statement is used to exit from a block. There are two forms of the break Statements:

  1. Unlabeled break statement
  2. Labeled break statement

2.1. Unlabeled break statement

Unlabeled break statement are without any labels. They are written as simply "break;". An example of unlabeled break statement can be:

int i = 1;
while (true) {   // Cannot exit the loop from here
        if (i <= 10) {
                System.out.println(i);
                i++;
        }
        else {
                break; // Exit the loop
        }
}

OR, you must have seen them in switch statements.

switch (switch-expression) {
        case label1:
                statements;
 				break;
        case label2:
                statements;
 				break;
        default:
                statements;
}

2.2. Labeled break statement

Here, you write a label name after break statement. An example of a labeled break statement is :

break label;

A more detailed example could be:

blockLabel:
{
        int i = 10;
        if (i == 5) {
                break blockLabel; 	// Exits the block
        }
         
        if (i == 10) {
                System.out.println("i is not five");
        }
}

The break statement terminates the labeled statement; it does not transfer the flow of control to the label. Control flow is transferred to the statement immediately following the labeled (terminated) statement.

One important point to remember about a labeled break statement is that the label used with the break statement must be the label for the block in which that labeled break statement is used.

The following snippet of code illustrates an incorrect use of a labeled break statements:

lab1:
{
        int i = 10;
        if (i == 10)
                break lab1; // Ok. lab1 can be used here
}
 
lab2:
{
        int i = 10;
        if (i == 10)    // A compile-time error. lab1 cannot be used here because this block is not
                		// associated with lab1 label. We can use only lab2 in this block
                break lab1;
}

3. break keyword example

Java program to print the numbers from 1 to 5 and then break the loop.

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

Program output.

1
2
3
4
5

That’s all for Java break keyword and it’s usages.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.