Break and Continue in Java

Break and Continue in Java

7 mins read480 Views Comment
Updated on Aug 3, 2023 16:37 IST

In Java programming, there are two helpful tools called ‘break’ and ‘continue.’ They make managing loops easier. With ‘break,’ you can stop a loop before it finishes, based on certain conditions. On the other hand, ‘continue’ lets you skip some parts of the loop. In this guide, we’ll explore these tools, show you how they’re used in real situations, and share tips to get the most out of them. By mastering these tricks, you’ll become better at Java programming and make your code more efficient.

2023_04_MicrosoftTeams-image-8.jpg

Break and continue keywords are used as a jump statement, which is used when the programmer wants to skip or jump out some line of code. Break and continue are used inside the for, while, and do-while loops or with the switch statements. This article will dive deep into the break and continue keyword in Java. 

Like any other programming language use of break and continue keyword is the same in the case of Java programming. 

What is a break statement in Java?

Break keyword exit or terminates the loop like for, goto, do-while or while in Java. If the user wants to terminate the loop after getting desired conditions, then the break keyword is used to terminate the loop. 

Whether it is, a loop or a switch statement break keyword is used to jump out some line of code in Java. By the use of the break keyword execution of a loop is stopped and moved to the other lines of codes, which makes the program readable and more convenient to the user. 

Syntax to declare break keyword in Java inside a loop 

for(Expression1;Expression2;Expression3){  

        if(condition){  

            //breaking the loop  

            break;  

        }  

Recommended online courses

Best-suited Java courses for you

Learn Java with these high-rated online courses

– / –
6 weeks
15 K
3 months
2.05 K
3 months
– / –
170 hours
5.4 K
1 month
– / –
6 months
– / –
2 months
17 K
3 months
5.5 K
1 month

What is continue statement in Java?

 The continue keyword is used when a user wants to skip a loop’s specific Condition or iteration. Continue keyword: skip the specific iteration in loops like for,do-while, while or goto. 

In other words, the continue keyword is used where users wish to execute the loop but do not wish to execute a specific condition or iteration. 

Syntax to declare continue keyword in Java 

for(Expression1;Expression2;Expression3){  

        if(condition){   

            continue; //it will skip the current given Condition and 

          } //executes the whole program

 

What is Java JDK?
How to Check if Two Java Strings are Anagrams
Understanding Java Scanner Class

Explore Java Courses

Use of break and continue in Java 

Let’s move further on their usage to get a handle on break and continue keywords in Java.

The break keyword is used for terminating the execution of a loop in Java. This jump statement is used in the following cases in a Java programming language. 

  • for loop 
  • while loop 
  • do-while loop
  • switch case

Break keyword with for loop

Program to demonstrate the use of break keyword inside the for loop in Java 


 
public class example {
public static void main(String[] args)
{
// for loop that will print numbers from 0 to 14
for (int i = 0; i < 15; i++) {
if (i == 8) //specifying the condition
break; //loop is terminated by using the break keyword
System.out.println("i= " + i);
}
System.out.println("loop terminated");
}
}
Copy code

Output

i=1

i=2

i=3

i=4

i=5

i=6

i=7

loop terminated 

Explanation 

  • This program uses a for loop to print numbers from 0 to 14.
  • In each iteration value of i is increased by one using the increment operator. 
  • Break is written in the Condition of if inside the for loop, which instructs to break the loop when the value of i becomes 8.
  • And break keyword stops or terminate the whole loop when the value of i becomes 13.

Break keyword with while loop 

Program to demonstrate the use of break keyword inside the while loop in Java 


 
public class Example {
public static void main(String[] args)
{
int n = 20; //initialising and creating the variable
//using a while loop to print the numbers between 20 to 10 in descending order
while (n >= 10) {
if (n == 13) { //specifying the if condition
//loop will terminate when the value of n becomes 13
break;
}
// Print statement to show the value of n
System.out.print(n + " ");
n--; //using the decrement operator to decrease the value of n by 1 in each iteration
}
}
}
Copy code

Output

20 19 18 17 16 15 14 

Explanation 

  • This program uses a while loop to print numbers from 20 to 10 in descending order. 
  • In each iteration value of n is decreased by one using the decrement operator. 
  • Break is written in the Condition of if inside the while loop, which instructs the compiler to break the loop when the value of n becomes 13.
  • And break keyword stops or terminate the whole loop when the value of n becomes 13.

Break keyword with a do-while loop 

Program to demonstrate the use of break keyword inside the do-while loop in Java 


 
public class Example {
// Main method
public static void main(String[] args)
{
int n = 0; //intiallising variable n
// do-while loop to print multiples of five
do {
if (n == 65) {
// Incrementing value of n by 5
n += 5;
// when the value of n will become 65 loops will terminate
//by using the break keyword
break;
}
System.out.print(n + " ");
// Print statement to show multiples of five on the screen
n += 5;
}
// condition written in while to be checked
while (i <= 100);
}
}
Copy code

Output-

0 5 10 15 20 25 30 35 40 45 50 55 60

Explanation 

  • This program uses a do-while loop to print multiples of five.
  • In each iteration, the value of n is increased by one using the increment operator. 
  • Do-while loop is similar to the while loop, but the do-while loop statement executes first and after that Condition is checked. 
  • Break is written in the Condition of if inside the do block, which instructs the compiler to terminate the loop when the value of n becomes 65.
  • Using the break keyword, this program prints the multiple of five up to 60.

Break keyword with switch case 

Program to demonstrate the use of break keyword inside the switch case in Java 


 
public class Example {
// Main method
public static void main(String[] args)
{
// initialising and declaring a variable
int n = 3;
// Switch statement that will take arguments
switch (n) {
// Case statements
case 1:
System.out.println("this is case1");
//break keyword will terminate the Switch
//case if the given condition is satisfied
break;
case 2:
System.out.println("this is case2");
break;
case 3:
System.out.println("this is case3");
break;
// Default case
default:
System.out.println("this is default case");
}
}
}
Copy code

Output-

This is case3

Explanation –

  •  In the above program, we have initialised the value of n as 3. 
  • The switch keyword takes the value of n as the arguments and checks for similar cases if the cases match the value of n, then the code written inside that specific case gets executed. 
  • Writing break keywords after each case is essential as it ensures to termination of the execution of other cases.
  • If we don’t use the break keyword after each case, then the execution of the program will not stop, and the code written inside each case will get executed until the execution reaches to program’s end. 

Continue keyword in Java 

The continue keyword is used when the user wants to execute the loop but does not wish to execute a specific condition or iteration. This jump statement jumps out a specific condition or iteration. The continue keyword is used in the following cases in Java. 

  • for loop 
  • while loop 
  • do-while loop 

Continue keyword with for loop 

Program to demonstrate the use of the continue keyword inside the for loop in Java 

 


 
public class Example {
public static void main(String args[]) //main method
{ //initialising variable
// printing numbers from 0 to 14 using for loop
for (int n = 0; n < 15; n++) {
// If the number is 8
// continue keyword will skip the number 8 and continue the loop
if (n == 8)
continue;
System.out.print(n + " ");
}
}
}
Copy code

Output

0 1 2 3 4 5 6 7 9 10 11 12 13 14 

Explanation-

  • Above program prints numbers from 0 to 14 using for loop. 
  • In each iteration, the Condition is checked and incremented by 1.
  • Continue is written in the Condition of if inside the for loop, which instructs the compiler to skip the iteration or number the loop when the value of n becomes 8.
  • When the value of n becomes 8, continue keyword skip the number 8 and print numbers according to the loop. 
  • The continue keyword does not stop the execution of the loop like the break keyword; it simply skips the Condition or iteration. 

Continue keyword with while loop 

Program to demonstrate the use of the continue keyword inside the while loop in Java 


 
public class Example {
public static void main(String[] args) {
//printing numbers from 1 to 17 using a while loop
int n=1; //initialising and creating variable
while(n<=17){
if(n==11){
//using continue statement
n++; //incrementing value of n by 1 in each statement
continue;//it will skip the condition written in if
}
System.out.println(n);
n++; //incrementing value of n by 1 in each statement
}
}
}
Copy code

Output

1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17

Explanation 

  •  The above program prints numbers from 1 to 17 using a while loop. 
  • In each iteration, the Condition is checked and incremented by 1.
  • Continue is written in the Condition of if inside the while block, which instructs the compiler to skip the iteration or number the loop when the value of n becomes 11.
  • When the value of n becomes 11, continue keyword skip the number 11 and print numbers according to the loop up to 17.

Continue keyword with a do-while loop 

Program to demonstrate the use of the continue keyword inside the do-while loop in Java 


 
public class Example {
public static void main(String[] args) {
//initialising and creating variable
int n=10;
//printing numbers from 10 to 25 using do- while loop
do{
if(n==18){
//using continue statement
n++; //incrementing value of n by 1 in each statement
continue;//it will skip the condition written in if
}
System.out.println(n);
n++; //incrementing value of n by 1 in each statement
}while(n<=25); //condition written in while
}
}
Copy code

Output

10 12 13 14 15 16 17 19 20 21 22 23 24 25 

Explanation 

  • Above program prints numbers from 10 to 25 using the do-while loop.
  • In each iteration, the Condition is checked and incremented by 1.
  • continue is written in the Condition of if inside the do block, which instructs the compiler to skip the iteration or number the loop when the value of n becomes 18.
  • When the value of n becomes 18, continue keyword skip the number 18 and print numbers according to the loop up to 25.

Wrapping up 

By practicing and analyzing all these programs, one can easily understand the use of break and continue keywords in Java. This article will help you to have a clear understanding of the jump statements in Java. 

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio