Difference Between break and continue in Java

October 2022 · 5 minute read

Key Difference – break vs continue in Java
 

In programming, sometimes it is required to repeat a statement or a set of statements multiple times. Loops are used to iterate a number of times the same set of instructions. Some examples of loops are the while loop, do while loop and for loop. In the while loop, the test expression is evaluated first. If it is true, the statements inside the while loop execute. In the end, the test expression is evaluated again. If it is true, the statements will execute again. When the test expression becomes false, the loop terminates. The do while loop is similar to the while loop. But the statements execute once before the test expression is checked. The for loop is used when the number of iterations is known at the beginning. The initialization happens first. Then the test expression is checked. If it is true, the loop executes. Then the update expression is evaluated. Again, the test expression is checked. If it is true, the loop executes. This process repeats until the test expression becomes false.  It is sometimes required to skip some statements inside the loop or to terminate the loop immediately without checking the test expression. The break and continue statements can be used to achieve this task. The break is used to terminate the loop immediately and to pass the program control to the next statement after the loop. The continue is used to skip the current iteration of the loop. That is the key difference between break and continue in Java.

CONTENTS

1. Overview and Key Difference
2. What is break in Java
3. What is continue in Java
4. Similarities Between break and continue in Java
5. Side by Side Comparison – break vs continue in Java in Tabular Form
6. Summary

What is break in Java?

The break is used to terminate from the loop immediately. When there is a break statement, the control is passed to the statement after the loop. The ‘break’ keyword is used to indicate the break statement. Even though the program is executing the loop, if a break occurs, the execution of the loop terminates. Therefore, if the programmer wants to stop execution when a specific condition is met, then he can use the break statement.

Difference Between break and continue in Java

Figure 01: Java program with break statement

According to the above program, the for loop iterates from 1 to 10. When the i value becomes 6, the test expression becomes true. So, the break statement executes, and the loop terminates. So, the value after 6 will not print. Only the value from 1 to 5 prints.

What is continue in Java?

The continue is used to used to skip the current iteration of the loop. The keyword ‘continue’ is used to indicate the continue statement. When continue executes, the control of the program reaches the end of the loop. Then the test expression is checked. In a for loop, the update statement is checked before the test expression is evaluated.

Key Difference Between break and continue in Java

Figure 02: Java program with continue statement

According to the above program, the for loop iterates from 1 to 10. When i is 1, the remainder after dividing by two is 1. So, the if condition is true. Therefore, the continue statement executes and the iteration skips to the next. Then i comes 2. When dividing 2 by 2, the remainder is 0. The condition is false. So, continue does not execute. Therefore, the value 2 gets printed. In the next iteration, i is 3. When dividing it by 2, the remainder is 1. The condition is true. So, continue executes and the iteration jump to the next and i becomes 4. This process repeats until i becomes 10.  If the remainder is one, the iteration skips to the next because of the continue statement. Only the even numbers get printed.

What is the Similarity Between break and continue in Java?

What is the Difference Between break and continue in Java?

break vs continue in Java

The break is a loop control structure that causes the loop to terminate and pass the program control to the next statement flowing the loop.The continue is a loop control structure that causes the loop to jump to the next iteration of the loop immediately.
 Main Purpose
The break is used to terminate the loop.The continue is used to skip statements inside the loop.

Summary – break vs continue in Java

In programming, it is required to repeat a statement of a group of statements multiple times. The loops are used for that tasks. Sometimes it is required to skip some statements inside the loop or to terminate the loop immediately. The break and continue can be used to achieve that task. The break is used to terminate the loop immediately and to pass the program control to the next statement after the loop. The continue is used to skip the current iteration of the loop. That is the difference between break and continue in Java.

Reference:

1.“Continue Statement in Java.”, Tutorials Point, 27 Oct. 2017. Available here
2.“Break Statement in Java.”, Tutorials Point, 27 Oct. 2017. Available here

ncG1vNJzZmivp6x7pbXFn5yrnZ6YsqOx07CcnqZemLyue8OinZ%2Bdopq7pLGMm5ytr5Wau26u0Z6YpGWRo7FuwtJmmqimpJ67trGMoqVmopGrrnA%3D