Break Statement with Example in Java

In this class, We discuss Break Statement with Example in Java.

The reader should have prior knowledge of loops in Java. Click Here.

Break Statement:

We use break statements to come out of the loop.

Example:

for(i=0; i<=10; i++)

{

Code

if(i==5)

{

break;

}

Code

}

First, we understand the execution of the above Code.

The body contains Code, if statement, and Code for each iteration of the loop.

For the 5th iteration, the if condition is true, so the break statement will execute.

Once the break statement executes, the execution comes out of the loop.

The execution continues executing the remaining Code after the loop.

The below-nested loop example helps to understand the break statement.

Example:

We need to display 2 to 10 tables.

The table value should be less than 20.

Example:

6 * 4 = 24 Here 24 is > 20 so stop displaying 6th table from 4 onward.

The below diagram shows the program and output.

In the inner loop, we wrote an if condition to break the loop.

if(i*j >20)

break;

Point to understand:

The beak break statement is written inside the inner loop. So execution comes out of the inner loop.

After coming out of the inner loop, the outer loop starts execution.

Please elaborate on the execution flow step by step.

The elaboration helps a lot to understand how to write Code.