Java Break and Continue

Learners can benefit from this post to better understand Java break and Continue with examples.

A break statement skips some statements, while a continue statement terminates the loop immediately without checking the test expression.

For, while, or do-while loops can be used with these statements.

Java Break

To exit the loop immediately, a Java break statement can be applied.

The loop is stopped at the break statement, and control is immediately returned to the first statement following the loop.

A break statement is generally called for in cases where we aren’t sure how many iterations the loop will have. This is because we wish to terminate the loop based on some condition.

A break statement was already used in an earlier chapter of this tutorial. This statement was used to exit a switch statement.

Syntax :

break;

You can also use the break statement to jump out of a loop.

A loop is terminated when mrx is equal to 6 in the following example:

Example: 

public class Main { public static void main(String[] args) { for (int mrx = 1; mrx <= 8; mrx++) { if (mrx == 6) { break; // break keyword exits the loop when value of mrx becomes 6 } System.out.println(mrx); } } }

Example: 

public class Main { public static void main(String[] args) { for (int ample = 10; ample >= 1; ample–) { if (ample == 3) { break; // break keyword exits the loop when value of ample becomes 3 } System.out.println(ample); } } }

Java break example



Java Continue

If a specified condition occurs, the continue statement breaks one iteration (in the loop), and continues with the next.

The value 3 has been skipped in this example:

 

Example: 

public class Main { public static void main(String[] args) { for (int mrx = 1; mrx <= 6; mrx++) { if (mrx == 3) { continue; // By using the Continue keyword, we skip the iteration when mrx reaches 3 and continue the loop back from there } System.out.println(mrx); } } }

Similarly, the value “5” is not printed:

Example: 

public class Main { public static void main(String[] args) { for (int ample = 1; ample <= 7; ample++) { if (ample == 5) { continue; // By using the Continue keyword, the iteration when ample reaches 5 is skipped and the looping continues again } System.out.println(ample); } } }

java break-continue example


Break and Continue in While Loop

In while loops, you can also use break and continue:

Example:  break

public class Main { public static void main(String[] args) {int mrx=1;while(mrx <= 6){System.out.println(mrx); mrx++; if(mrx==4){ break; // break keyword exits the loop when value of mrx becomes 4 } } } }

Example: 

public class Main { public static void main(String[] args) {int ample=2;while(ample <= 16){System.out.println(ample); ample+=2; if(ample==10){ break; // As soon as ample reaches 10, the loop is exited with the break keyword } } } }

Example:  Continue

public class Main { public static void main(String[] args) { int mrx=1;while(mrx<6){ if(mrx==2){ mrx++; continue; // By using the Continue keyword, we skip the iteration when mrx reaches 2 and continue the loop back from there} System.out.println(mrx); mrx++; } } }

Example: 

public class Main { public static void main(String[] args) { int ample=1;while(ample<16){ if(ample==7){ ample+=2; continue; // When ample reaches 7, we skip the iteration and continue from there using the Continue keyword} System.out.println(ample); ample+=2; } } }

Break vs continue:

Break
Continue
Break statements terminate loops immediately.It is used to skip the current loop iteration.
A break statement in Java is indicated by the break keyword.A continue statement in Java is indicated by the continue keyword.
The switch statement can be broken with a break.Continue with the switch statement is not possible.
Breaking the loop early terminates it.A continue statement triggers the next iteration at the beginning.
This will stop the loop from running.The loop will not be stopped by using continue statement.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

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