CodingBison

Java provides methods for an application to be more selective when running inside a loop. There are two specific tasks that an application might wish to do when present in a loop: skip some of the records and break out of the loop. Java provides two keywords, "continue" and "break" that allows a program to skip a record or to break out of the loop, respectively.

Here is a representative flow-chart that highlights the behavior of these statements. When condition for continue is met, then we skip the rest of the tasks in that iteration of the loop and continue the next iteration of the loop. On the contrary, when the condition for break is met, then we skip the loop altogether and break out of it.



Figure: Continue/Break for a while Loop

For cases where we have multiple levels of loops (meaning, a loop running inside another loop), the "break" statement merely exits from the current loop level. The immediate outer loop surrounding the current loop continues to run. Similar behavior holds true for the continue statement as well, it applies only to the current loop.

With that, let us go through an example that uses these two keywords. In the interests of simplicity, we use only a "for" loop. But, these keywords would apply equally well to the remaining loop variants of while and do-while.

Example program

Let us write a sample program, using all the concepts we have seen so far in this section.

 public class ContinueBreakExample {
     public static void main(String[] args) {
         int arrayVar[] = new int[4];
         int arrayValue = 880;

         System.out.println ("\n Filling the array using For Loop. Demonstrating BREAK.....");
         // Fill the array using for loop.
         for (int count = 0; count < arrayVar.length; count++) {
             if (count == 2) {
                 System.out.println(" Exiting the loop, nothing inside the loop will be executed after this");
                 break;
             }
             arrayVar[count] = arrayValue + count;
             System.out.println (" Count: " + count + ", Array Val: " + arrayVar[count]);
         }

         System.out.println("\n Filling the array using For Loop. Demonstrating CONTINUE.....");
         // Fill the array using for loop.
         for (int count = 0; count < arrayVar.length; count++) {
             if (count == 2) {
                 System.out.println(" Continue in the loop, nothing"
                                 + " inside the loop will be executed for this iteration alone");
                 System.out.println(" Control jumps to the loop increment block directly......");
                 continue;
             }
             arrayVar[count] = arrayValue + count;
             System.out.println (" Count: " + count + ", Array Val: " + arrayVar[count]);
         }

         System.out.println ("\n Printing the array using While Loop.....");
         /* 
          * Declaring count again, since the one declared above is
          * not visible here. Its scope is confined only to that loop.
          */
         int count = 0;
         // Print the array values using While loop.
         while (count < arrayVar.length) {
             System.out.println (" Array Index: " + count + ", Value: "+ arrayVar[count]);
             count++;
         }
     }
 }

Compile/Run this program and Here is the output:

  Filling the array using For Loop. Demonstrating BREAK.....
  Count: 0, Array Val: 880
  Count: 1, Array Val: 881
  Exiting the loop, nothing inside the loop will be executed after this

  Filling the array using For Loop. Demonstrating CONTINUE.....
  Count: 0, Array Val: 880
  Count: 1, Array Val: 881
  Continue in the loop, nothing inside the loop will be executed for this iteration alone
  Control jumps to the loop increment block directly......
  Count: 3, Array Val: 883

  Printing the array using While Loop.....
  Array Index: 0, Value: 880
  Array Index: 1, Value: 881
  Array Index: 2, Value: 0
  Array Index: 3, Value: 883




comments powered by Disqus