JAVA BREAK AND CONTINUE

Java break and continue statements are also known as transfer statements that are used to transfer the control from one place to another.

Table of Contents

[hide]
  • Java Break
  • Example :A Simple Java Break Program
  • Java Continue
  • Example :A Simple Java Continue Program

Java Break

We hava already seen the use of Java break in Switch statements. As soon as break statement is encountered inside the loop, the loop is terminated and the transfer of control follows the next statement outside loop.

We have to decide a condition as per our requirement that when do we need to break a loop or switch statement. The break statement is used to transfer control hence only works at loop level and cannot be directly used with if statements.

Java Break Syntax

break;

Working :

The break statement is used as a flow control hence is used within loops. If the condition within a loop is true, then it enters the if block and based on some conditional check break statement terminates the loop. Java break can be used with all types of loops i.e for loop, while loop and do while loop. Lets see the working of do while loop with the help of flow diagram below.

Flow Diagram

java do while image

From the above diagram it is very easy to understand the working of Java break statement. kindly learn all the flow diagrams given in this Java tutorial. School and College students can use these in their exams to get more marks.

A Simple Java Break Program

Lets take an example of printing a table of 2 till 50 iterations. We will break the loop after 10th iterations i.e at number 20.

copyimage
// Java program to illustrate for Java Break example
public class JavaBreakExample{
    public static void main(String args[]){  
       int num = 2;
       for(int i = 1 ; i <= 50 ; i++ ) {
          System.out.print(i * num + "	");
          if(i * num == 20) {
          break;
          }
       }
   }
}

OUTPUT :

2 4 6 8 10 12 14 16 18 20

Just try removing the if block, you could see that table of 2 is printed till 50th iteration i.e till value 100. Also try the same problem with while and do-while just for better understanding purpose.

kindly dry run this program by yourself. If facing any difficulty kindly watch our Java Control Statements Loops modules in which we have shown how to dry run the given programs on paper with the help of tabular representation.

Java Continue

Java Continue Statement is used to break only one iteration in the loop and continues with rest of the iterations. Unline break statement that terminates the entire loop once the break condition is fulfilled, continue statement just skips the particular iteration once the condition is met and thus executes the rest of the iterations.

Java Continue Syntax

continue;

Flow Diagram

java do while image

Working :

In the above diagram, first the loop body gets executed where it checks for the condition. If the condition is true it will enter the loop else if its false it will exit the loop as shown in the diagram.

In the next step based on some condition, continue statement is checked. If its present, then statement after that in the loop body will not be printed just for that particular iteration.

If continue statement is not present then all the statements will get executed inside the loop body. Lets see a simple continue statement example in the next module.

A Simple Java Continue Program

In this example we will print the values from 1 to 7, and will neglect the value 5 using continue statement.

copyimage
// Java program to illustrate for Java Continue example
public class JavaContinueExample{
    public static void main(String args[]){  
        for(int i = 1 ; i <= 7 ; i++ ) {
           if(i == 5) {
           continue;
           }
        System.out.print(i + " ");
        }
    }
}

OUTPUT :

1 2 3 4 6 7

Just click on the copy icon on the right top corner of the program and paste the code in your code editor, you would get the same output. Just change the values and try to implement something new if your concepts are clear.

Unless you practice these programs on your own, you would not be able to learn java quickly. This tutorial is designed in such a way that if you follow from starting your core concepts would be strong that would help in the later modules where the level of difficulty would be high

In next tutorial we will learn about Java OOP's Concepts .