Break and continue


In this tutorial, we will cover concepts and keywords related to looping in general. You will learn how you can terminate a loop even if its condition is still true and you will also learn how to skip some steps in the body of the loop instead of making it terminate. These two issues are handles by two Java keywords which are the break and continue.

The break Keyword

Simply, break breaks the loop. Therefore, if you have a break statement in your loop, whenever the program reaches that statement, it will stop the loop and exit from it. Let's look at the following program for illustration.

public class Break
{
   public static void main(String args[])
   {
      for(int i = 0; i < 10; i++)
      {
         System.out.println(i);

         break;
      }
   }
]

Output:

0

The reason why the for loop printed only a single zero is that i was zero in the first iteration and after the printing statement the loop encountered a break statement which made the loop terminate.

A More Useful Use of break

The codevious program was only an example of what break can do. Let's look at a more useful example. In the following program, we will have an infinite loop that will keep reading numbers from the user and will stop when the user enters an odd number. Let's look at the code.

import java.util.Scanner; 

public class OddBreak
{
   public static void main(String args[])
   {
      Scanner input = new Scanner(System.in);

      for(;;)
      {
         int n = input.nextInt();

         if(n%2 != 0)
         {
            System.out.println("Odd number I will stop now.");

            break;
         }

         System.out.println("Still even, I will keep on looping");
      }
   }
}

Output:

2
Still even, I will keep on looping
6
Still even, I will keep on looping
3
Odd number I will stop now.

To clear some things up, for(;;) recodesents an infinite loop which need to be controlled in its body. Now, the program will read numbers from the user. If the user enters an even number, the program will print Still even I will keep on looping. If not, the program will print Odd number I will stop now. Then, it will encounter a break statement and terminate the loop. Therefore, break in this program was used in a more useful way.

Note: Try remembering this sort of infinite loop because you will likely use it when solving programs on the Judge and any other programming contest.

The continue Keyword

Now it's time to move on to the continue statement. Now, if we have a break statement in a loop, anything written below the break will not be executed because the loop will stop when it reaches that statement. On the other hand, anything written below a continue keyword will not get executed but the loop will not terminate. The loop will move to the next iteration. Let's take a look at the following program. This program will sum the even numbers from one to ten using a for loop.

public class SumEven
{
   public static void main(String args[])
   {
      int sum = 0;

      for(int i = 1; i <= 10; i++)
      {
         if(i%2 != 0)
            continue;

         sum += i;
      }

      System.out.println("The sum is: "+sum);
   }
}

Output:

30

In the codevious program, continue made the loop not add the odd numbers to sum and just move on to the next iteration.

break and continue Labels

Let's take a program where we have two nested loops. Assume that if we encounter a certain event in the inner loop, we have to break or continue both loops. However, break and continue are only related to the loop they are found in and not the outer loop. To solve this problem, Java provides a technique called labels which will allow us to do so. Let's look at the following program to illustrate how to use a label. This program should break when both i and j are five.

public class Labels
{
   public static void main(String args[])
   {
      outer:      //this is a label
      for(int i = 0; i < 10; i++)
      {
         for(int j = 0; j < 10; j++)
         {
            if(i==5 && j==5)
               break outer;
         }
      }
   }
}

The codevious program will break the outer loop because the break statement specifies that it is breaking the label outer:.

Congratulations

You now know how to use break and continue. Again, remember that the infinite loop example is useful when solving problems on the judge. Move on to the next tutorial but first check the following important notes.

Important Notes

Label Syntax

When writing a label make sure that you don't mistake the colon ( : ) for a semi-colon. This will result in a compilation error.