how2examples.com
Home - Introduction to Java Programming

Java Loop Constructs

Java supports three loop constructs - while(), do and for(). All of which will be familiar to people from a C or C++ background.

The while() loop

A while() loop allows a block of code to be executed zero, once or many times. A boolean condition must be specified in the parentheses of the while() statement. The code directly following the while() statement, the body of the loop, will be repeatedly executed as long as the boolean condition evaluates to true. If the first time the boolean condition is evaluated it evaluates to false then the body will not be executed.

while (boolean_condition) {
   // body of the loop
}
Contents of WhileExample.java:
public class WhileExample {
   public static void main(String[] args) {
      int i = 0;
      while (i++ < 9) {
         System.out.print(i);
      }
   }
}
Command to compile WhileExample.java:
javac WhileExample.java
Command to run WhileExample:
java WhileExample
Output from running WhileExample:
123456789

The do loop

A do loop allows a block of code to be executed once or many times. As with the while() loop, the do loop is terminated when the boolean condition evaluates to false. Unlike the while() loop, the do loop is always executed at least once.

do {
   // body of the loop
} while (boolean_condition);
Contents of DoExample.java:
public class DoExample {
   public static void main(String[] args) {
      int i = 0;
      do {
         System.out.print(i);
      } while (i++ < 9);
   }
}
Command to compile DoExample.java:
javac DoExample.java
Command to run DoExample:
java DoExample
Output from running DoExample:
0123456789

The for() loop

The for() loop is normally used when it is already known how many times the body of the loop should be executed. The contents of the for() loop parentheses has three sections:

for (statement; condition; expression) {
   // body of the loop
}
Contents of ForLoopExample.java:
public class ForLoopExample {
   public static void main(String[] args) {
      char[] chars = {'a','b','c','d','e','f'};
      for (int i=0; i<chars.length; i+=2) {
         System.out.print(chars[i]);
      }
   }
}
Command to compile ForLoopExample.java:
javac ForLoopExample.java
Command to run ForLoopExample:
java ForLoopExample
Output from running ForLoopExample:
ace

The enhanched for() loop

The enhanched for() loop, introduced in Java 5, is a variation of the standard for() loop that simplifies looping through the elements of an array or collection. The contents of the enhanced for() loop parentheses has two sections:

for (declaration : expression) {
   // body of the loop
}
Contents of EnhancedForLoopExample.java:
public class EnhancedForLoopExample {
   public static void main(String[] args) {
      char[] chars = {'a','b','c','d','e','f'};
      for (char c : chars) {
         System.out.print(c);
      }
   }
}
Command to compile EnhancedForLoopExample.java:
javac EnhancedForLoopExample.java
Command to run EnhancedForLoopExample:
java EnhancedForLoopExample
Output from running EnhancedForLoopExample:
abcdef

The break and continue statements

The break and continue statements are used to abandon the execution of a loop.

Both break and continue statements can take a label that allows the flow of execution to skip multiple nested loops. The label must be specified at the start of a loop.

Contents of LoopLabelExample.java:
public class LoopLabelExample {
   public static void main(String[] args) {
      outerloop:
      for (int i=0; i<9; i++) {
         System.out.print("i = " + i);
         if (i % 2 == 1) {
            System.out.println(" continue");
            continue;
         }
         for (int n=0; n<9; n++) {
            System.out.print(" n = " + n);
            if (i+n>5) {
               System.out.println(" continue outerloop");
               continue outerloop;
            }
            if (i==4 && n==1) {
               System.out.println(" break outerloop");
               break outerloop;
            }
         }
         System.out.println();
      }
   }
}
Command to compile LoopLabelExample.java:
javac LoopLabelExample.java
Command to run LoopLabelExample:
java LoopLabelExample
Output from running LoopLabelExample:
i = 0 n = 0 n = 1 n = 2 n = 3 n = 4 n = 5 n = 6 continue outerloop
i = 1 continue
i = 2 n = 0 n = 1 n = 2 n = 3 n = 4 continue outerloop
i = 3 continue
i = 4 n = 0 n = 1 break outerloop