An Overview of Break and Continue Statements in Java


By NIIT Editorial

Published on 10/07/2023

Important in programming, loop control statements determine how iterations of a loop are executed. Java, a widely used programming language, provides programmers with two such instructions, Break and Continue, to alter the operation of loops.

This blog post will go through the syntax of the Java Break and Continue statements, as well as their utility in controlling loops. This blog's premise is that these statements are potent ways to improve the usefulness and efficiency of Java's loop-based programming paradigm.
 

 

Table of Contents:

  • Understanding Loops in Java 
  • The Break Statement in Java 
  • The Continue Statement in Java 
  • Difference Between Break and Continue Statements 
  • Conclusion

Understanding Loops in Java

Java's looping construct is used to continually run a section of code until a given condition is fulfilled. Java's loops go into one of three categories: while, do-while, and for.

The condition is evaluated by the while loop before the code block is carried out. If the condition holds, the programme will run until the condition no longer holds, at which point the loop will stop.

The difference between the while loop and the do-while loop is that the latter performs its conditional check at the loop's conclusion. This guarantees that the loop's code will run at least once.

If you know ahead of time how many times you want to run the loop, you should use the for loop. It consists of three sub-parts: setting up a condition, incrementing, and decrementing. The loop counter is incremented or decremented after each iteration and the initial value is set during startup.

Altering a loop's behaviour may be done with the use of loop control statements like Break and Continue. The Break statement exits the loop and executes the next sentence. When you need to skip out of a loop early, this is the way to do it. "break;" is the correct syntax for the Break statement.

Instead of continuing with the current iteration of the loop, the Continue statement advances to the next iteration. You may use this to bypass certain values or circumstances inside the loop. The Continue statement's syntax is "continue;".

 


The Break Statement in Java
 

Java's Break statement is a loop control statement that abruptly exits a loop. If the Break statement is executed within a loop, execution will jump to the statement that comes after the loop.

Java's Break statement operates as follows:

  • Inside the loop, you’ll find the Break statement.
  • The loop instantly finishes when it reaches a Break statement.
  • The statement that comes after the loop executes now.

To see how the Break statement may be used to abort a for loop, consider the following code.

css

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

  if (i == 5) {

    break;

  }

  System.out.println(i);

}

The for loop will run ten times in this case. However, the loop is broken when i == 5, when the Break command is executed. What will come out of the computer is:

0

1

2

3

4

When specific circumstances are satisfied, the Break statement is used to force the programme to quit the loop immediately. The Break statement may be used to exit a loop after a condition is met, as as when looking for a particular value in an array.

There are a few blunders that are often made with the Break statement that should be avoided. For instance, you will get a compile-time error if you try to use the Break command outside of a loop. Overuse of the Break statement may also make your code more complicated and less readable. Therefore, the Break statement should be used sparingly and only when absolutely required.
 

 

The Continue Statement in Java

To skip an iteration of a loop and continue to the next one, you may use the Continue statement, which is a loop control statement in Java. Any leftover code in the current iteration of the loop is skipped when the Continue statement is met inside the loop, and execution jumps to the next iteration.

The Java Continue statement operates as follows:

  • The loop now includes a Continue statement.
  • When a loop runs into a Continue statement, it immediately moves on to the next iteration.
  • The following cycle takes over from here.

 

An application of the Continue statement inside a for loop, skipping even iterations, is seen here:

css

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

  if (i % 2 == 0) {

    continue;

  }

  System.out.println(i);

}

The for loop will run ten times in this case. When i is an even number, however, we jump over the rest of that iteration's code since we've reached the Continue statement. What will come out of the computer is:

1

3

5

7

9

In Java, you may skip over specific loop repetitions depending on the results of a conditional expression called Continue. When iterating through an array, the Continue statement may be used to bypass elements that don't satisfy a criterion, for instance.

Mistakes in utilising the Continue statement are widespread and should be avoided. An endless loop may be created, for instance, if the Continue statement is used in the improper context. Overuse of the Continue statement also makes code more cryptic and less readable. That's why you should only utilise the Continue statement when absolutely required.
 


Difference Between Break and Continue Statements

Java's Break and Continue statements both serve the purpose of controlling loops, but in different ways.

In order to go forward to the next line after the loop, you may use the Break statement to exit the loop early. To bypass this loop iteration and go to the next one, however, use the Continue statement.

 

It is important to think about your end goal when selecting whether Break or Continue should be used. Use the Break statement to immediately exit the loop once the condition is fulfilled. However, the Continue statement should be used to skip iterations of the loop depending on a condition.

Break and Continue statements should be used sparingly and only when absolutely essential. When used excessively, these statements may make your code cryptic and hard to follow.

It's possible that you'll need to utilise both Break and Continue statements in tandem at some point. For instance, you could wish to skip certain iterations of the loop depending on one condition, while prematurely terminating the loop when another condition is fulfilled. To accomplish the desired result, you may combine the two statements.

Here's how you may combine a Break statement with a Continue one:

css

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

  if (i == 5) {

    break;

  }

  if (i % 2 == 0) {

    continue;

  }

  System.out.println(i);

}

The for loop will run ten times in this case. However, the loop is broken when i == 5, when the Break command is executed. When iterating through a set of instructions, the Continue statement is skipped whenever i is an even integer. The result of the code is:

1

3

 

Conclusion

In conclusion, this blog post discussed the Break and Continue statements in Java and their role in managing programmatic loops. We covered why you'd want to use Java's loop control statements, how they work, and the fundamentals of Break and Continue.

We also covered the similarities and distinctions between the Break and Continue statements, as well as when to use each one and how to pair them effectively.

Learning how to effectively use the Java Break and Continue commands is crucial for optimising your code and maintaining control over the execution of your loops. When used properly, they improve code efficiency, readability, and maintainability.

We suggest enrolling in a Java developer course if you're keen in learning Java development or enhancing your existing expertise in the language. To stand out in today's competitive employment market and become a successful Java developer, it is recommended that you enroll in a Java developer course.


 



Top