Dremendo Tag Line

break and continue Statement in Java

Loops in Java

In this lesson, we will understand what is break and continue statement in Java Programming along with some example.

What is break Statement in Java

The break statement in Java is a loop control statement which is used to terminate the loop. When the break statement encountered from within a loop, the loop iterations stops there and control returns from the loop immediately to the first statement written outside the body of the loop.

The break statement is used in situations when we want to terminate the loop based on some condition.

Note: A break statement can be used in for loop, while loop and do while loop.

We will see here the usage of break statement with three different types of loops:

  • Simple for Loop
  • Nested for Loop
  • Infinite for Loop

Simple for Loop: Consider the situation where we want to exit from the loop when user enters a specific number.

Below is the example of using break with simple for loop:

public class Example
{
    public static void main(String args[])
    {
        int i;
        for(i=2; i<=50; i=i+2)
        {
            if(i==10)
            {
                break;
            }
            System.out.print(i + " ");
        }
    }
}

Output

2 4 6 8

In the above example, we want to exit from the loop when the value of i is equal to 10. So, we have used a break statement inside the body of if statement which executes when the given condition is true, and the loop terminates.


Nested for Loop: We can also use break statement while working with nested loops. If the break statement is used in the innermost loop, the control will come out only from the innermost loop.

Below is the example of using break with nested for loop:

public class Example
{
    public static void main(String args[])
    {
        int i,j;
        for(i=1; i<=5; i++)
        {
            for(j=1; j<=10; j++)
            {
                if(j>3)
                {
                    break;
                }
                else
                {
                    System.out.print(j + " ");
                }
            }
            System.out.println();
        }
    }
}

Output

1 2 3
1 2 3
1 2 3
1 2 3
1 2 3

In the above example, we can clearly see that the inner loop is programmed to execute for 10 times. But as soon as the value of j becomes greater than 3 the inner loop stops executing which restricts the inner loop to run 3 times only. However, the iteration of the outer loop remains unaffected.

Therefore, break applies only to the loop within which it is present.


Infinite for Loop: The break statement can be included in an infinite loop with a condition to terminate the execution of the infinite loop.

Below is the example of using break with infinite for loop:

public class Example
{
    public static void main(String args[])
    {
        int i=1;
        for(;;)
        {
            if(i>10)
            {
                break;
            }
            System.out.print(i + " ");
            i++;       // it means i=i+1
        }
    }
}

Output

1 2 3 4 5 6 7 8 9 10

In the above example, the for loop executes an infinite number of times. To terminate the loop, we have used a break statement which terminates the loop, when the value of i is greater than 10.

What is continue Statement in Java

The continue statement in Java is also a loop control statement just like the break statement. The continue statement is opposite to that of break statement, instead of terminating the loop, it forces to execute the next iteration of the loop.

As the name suggests, the continue statement forces the loop to continue or execute the next iteration. When the continue statement executes in the loop, the code inside the loop following the continue statement will skip and the next iteration of the loop will begin.

Note: A continue statement can be used in for loop, while loop and do while loop.

Example

Consider the situation when you need to write a program which prints the number from 1 to 10 except the number 6. It is specified that you have to do this using loop and only one loop is allowed to use.

Here comes the usage of continue statement. What we can do here is we can run a loop from 1 to 10, and every time we have to compare the value of iterator with 6. If it equals to 6, we will use the continue statement to continue to next iteration without printing anything, otherwise, we will print the value.

Below is the implementation of above idea:

public class Example
{
    public static void main(String args[])
    {
        int i;

        // we run a loop from 1 to 10
        for(i=1; i<=10; i++)
        {
            // If the value of i is equals to 6,
            // continue to next iteration
            // without printing anything
            if(i==6)
            {
                continue;
            }

            // otherwise print the value of i
            System.out.print(i + " ");
        }
    }
}

Output

1 2 3 4 5 7 8 9 10

In the above example, when the value of i becomes 6, then continue statement execute and the loop move to the next iteration without executing anything written after the continue statement.

video-poster