How to break out of nested loops in Java

Break out of nested loop in java

In this post, we will see how to break out of nested loops in Java.

Using break (will break inner loop)

It is very important to understand how nested loops work to ensure that applying break will output the desired result.

If you are a novice with nested loops, I am going to make the concept as easy as possible for you to understand.

When you apply break in the inner loop, it results in breaking the loop when a specific condition is met, but the outer loop continues to execute.

The following nested loop outputs the result of multiplying the two loops until the outer loop reaches position 3.

The loop will skip position 3 and continue executing for the remaining positions.

Output:

1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16

Using named loop

In the first example we discussed, the nested loop continued to execute the outer loop after breaking position 3.

To prevent the outer loop from continuing to execute, we can use a named loop which is simply a loop with a label.

When the condition is met, instead of just breaking the nested loop, we want to add the named loop to the break statement to ensure that it breaks the outer loop.

For this approach, the nested loop will print out the product of the two loops until the outer loop reaches position 3.

The break statement will break the outer loop preventing it from iterating further.

Output:

1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8

Using named block

Block is a sequence of statements containing local classes and variables within braces and can be used to break out of nested loops.

The block executes the statements one by one until the last line of code. If the code executes successfully, the block terminates successfully, and the same case applies if the code terminates abnormally.

To break out of a nested loop using this approach, we will print out the product of the nested loop until a condition is fulfilled.

Once the condition is fulfilled, we will break the execution of the block, which will, in effect, break the execution of the nested loop.

The block will break the nested loop when the product is equal to 4, and you can also add some statements to be executed if the condition was not valid before the closing braces of the block.

Output:

1 * 1 = 1
1 * 2 = 2
1 * 3 = 3

Using return

The return keyword works the same way as the break keyword by terminating the nested loop after a condition is met but returns a value for the loop to break.

To break from nested loops using the return approach, create a method and initialize a value that will be returned by the method once a condition is met.

The following example initializes the variable named multiply with 1 and tracks the multiplication of the nested loops until the result of the multiplication is 8.

When the product is 8, the method will return the value, and the nested loop will break immediately.

The for loop will also be printing out the product of the nested loops until the condition is met.

Output:

1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
8

Conclusion

In this tutorial, you have learned four ways that you can use to break out of nested loops in java. The methods you covered include using break, return, named loop, and named block.

Was this post helpful?

Leave a Reply

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