Java break statement example

Break statement is one of the different control statements which we use very often.If break statement is found in loop, it will exit the loop and execute the statement following the loop.
Let’s take very simple example:

Java break statement example:

You want to search for the element in an array and if element is found, exit the loop.

When you run above program, you will get below output:

53 is present in the array
Executing statments following the loop

Labeled break statement:

Labeled break statement is used when you want to break labeled for loop.

For example:

You want to find an element in two-dimensional array. Once you got the element in the array, exit the outer loop.

When you run above program, you will get below output:

65 is present in the array
Executing statements following the outer loop

Switch Case:

You can also use break statement in switch case. Once case condition is met, you will exit the switch case.
For example:

When you run above program, you will get below output:

Car
Your vehicle type is C

That’s all about Java break statement example.

Add Comment