how2examples.com
Home - Introduction to Java Programming

Java Selection Constructs

Java supports if()/else and switch() selection constructs. Both of which will be familiar to people from a C or C++ background.

The if()/else construct

The parentheses of the if() construct must contain a boolean condition. If, and only if, the boolean condition evaluates to true then the block of code belonging to the if() is executed. The if() can optionally be followed by an else statement. If, and only if, the boolean condition of an if() evaluates to false then the block of code belonging to the else is executed.

Contents of IfElseExample.java:
public class IfElseExample {
   public static void main(String[] args) {
      testIfElse(0);
      testIfElse(1);
      testIfElse(2);
      testIfElse(3);
      testIfElse(4);
      testIfElse(5);
   }
   
   static void testIfElse(int i) {
      final char c;
      if (i<2) {
         c = 'A';
      } else if (i==3) {
         c = 'B';
      } else {
         c = 'C';
      }
      System.out.print(" " + i + " = " + c);
   }
}
Command to compile IfElseExample.java:
javac IfElseExample.java
Command to run IfElseExample:
java IfElseExample
Output from running IfElseExample:
0 = A 1 = A 2 = C 3 = B 4 = C 5 = C

The ?: conditional operator

The conditional operator provides a convenient way to combine a simple if()/else into a single expression. The conditional operator has three sections - a boolean expression before the ? and two expressions of the same result rype either side of the :.

boolean_condition ? value1 : value2

If the boolean expression to the left of the ? evaluates to true then the value of the whole expression is the value of the expression to the left of the : else it is the value of the expression to the right of the :.

Contents of ConditionalOperatorExample.java:
public class ConditionalOperatorExample {
   public static void main(String[] args) {
      System.out.print( true ? "A" : "B" );
      System.out.print( false ? 1 : 2 );
   }
}
Command to compile ConditionalOperatorExample.java:
javac ConditionalOperatorExample.java
Command to run ConditionalOperatorExample:
java ConditionalOperatorExample
Output from running ConditionalOperatorExample:
A2

The switch() construct

The switch() construct is used when there is a requirement to make a choice between multiple execution paths. The rules for using a switch() construct are:

The expression specified in the parentheses of the switch() is compared to the arguments associated with the case labels. When the expression is matched to a case label execution continues downward from that point until it reaches a break statement. If the expression is not matched to any of the case labels then, if present, execution continues from the default label.

Contents of SwitchExample.java:
public class SwitchExample {
   public static void main(String[] args) {
      testSwitch(0);
      testSwitch(1);
      testSwitch(2);
      testSwitch(3);
      testSwitch(4);
      testSwitch(5);
   }
   
   static void testSwitch(int i) {
      System.out.print(" " + i + " = ");
      switch (i) {
         case 3:
            System.out.print("A");
         case 1:
            System.out.print("B");
            break;
         case 2:
            System.out.print("C");
         default:
            System.out.print("D");
         case 4:
            System.out.print("E");
      }
   }
}
Command to compile SwitchExample.java:
javac SwitchExample.java
Command to run SwitchExample:
java SwitchExample
Output from running SwitchExample:
0 = DE 1 = B 2 = CDE 3 = AB 4 = E 5 = DE