Skip to content

5.3 if-else Statements

Key terms: conditional operator, if-else chain

5.3.1 Syntax and Semantics

An if-else statement ensures that exactly one of two possible courses of action is taken depending on a specified condition. The general syntax is:

if (E) { 
    // executed if E is true
} else { 
    // executed if E is false
}

Listing 5.3.1 uses an if-else statement to simulate a coin flip. A random decision is made by calling nextBoolean from the ThreadLocalRandom class, which returns true or false with equal probability. The second coin flip uses Java's ternary if-else operator, discussed below.

Listing 5.3.1 - CoinFlip.java

CoinFlip.java
package chap05.sect3;

import java.util.concurrent.ThreadLocalRandom;

/**
 * Flips two coins and outputs the results.
 *
 * @author Drue Coles
 */
public class CoinFlip {

   public static void main(String[] args) {
      ThreadLocalRandom rand = ThreadLocalRandom.current();

      // first flip: using if-else statement
      System.out.print("Coin flip 1: ");
      if (rand.nextBoolean()) {
         System.out.println("HEADS");
      } else {
         System.out.println("TAILS");
      }

      // second flip: using if-else operator
      System.out.println("Coin flip 2: " + (rand.nextBoolean() ? "HEADS" : "TAILS"));
   }
}
Output 5.3.1
Coin flip 1: HEADS 
Coin flip 2: TAILS

Another common way to simulate Boolean events such as coin flips is to generate a random double between 0 and 1 and compare it to a threshold probability. This technique can be used to simulate events with any desired probability, not just 50-50 outcomes. The following code fragment shows how to simulate a biased coin flip, one that has a 75% chance of coming up heads.

if (rand.nextDouble() < bias) { 
    System.out.println("HEADS");
} else { 
    System.out.println("TAILS"); 
}

5.3.2 Conditional Operator

Java has a single ternary operator, written ?: and called the conditional operator. It is a compact way of writing a simple if-else expression. The syntax is of the form:

E ? E1 : E2

If E is true, the entire expression evaluates to E1; otherwise it evaluates to E2. The next code fragment shows two concrete examples of the conditional operator in action.

System.out.println(x > y ? x : y); 
String coin = rand.nextBoolean() ? "HEADS": "TAILS";

In the first line, the greater of x and y is written to the output window. In the second, coin is initialized to "HEADS" or "TAILS" depending on whether nextBoolean returns true or false, respectively.

5.3.3 if-else Chains

An if-else chain is a sequence of nested if-else statements used for multi-way selection. Listing 5.3.3 is a revised version of Listing 5.2.1 (ComeOutRoll) that uses an if-else chain to simplify the code.

Listing 5.3.3 - ComeOutRoll.java

ComeOutRoll.java
package chap05.sect3;

import java.util.concurrent.ThreadLocalRandom;

/**
 * Simulates the first roll in the game of Craps. The player rolls two dice and the outcome depends
 * on the sum: natural (7 or 11) - player wins; craps (2, 3, or 12) - player loses; otherwise, the
 * game continues according to a different rule.
 *
 * @author Drue Coles
 * @version 2.0 - uses if-else chain
 */
public class ComeOutRoll {

   public static void main(String[] args) {
      ThreadLocalRandom rand = ThreadLocalRandom.current();
      int die1 = rand.nextInt(1, 7);
      int die2 = rand.nextInt(1, 7);
      final int comeOutRoll = die1 + die2;

      System.out.printf("You rolled %d + %d = %d. %n", die1, die2, comeOutRoll);

      final String winMessage = "Natural. You win.";
      final String loseMessage = "Craps. You lose.";
      final String continueMessage = "The game continues";

      if (comeOutRoll == 7) {
         System.out.println(winMessage);
      } else if (comeOutRoll == 11) {
         System.out.println(winMessage);
      } else if (comeOutRoll == 2) {
         System.out.println(loseMessage);
      } else if (comeOutRoll == 3) {
         System.out.println(loseMessage);
      } else if (comeOutRoll == 12) {
         System.out.println(loseMessage);
      } else {
         System.out.println(continueMessage);
      }
   }
}

The if-else chain enables multi-way selection. By convention, such chains are written in a flat alignment style to improve readability, but the following equivalent form uses indentation to make the nested logic explicit. If comeOutRoll is 7, the program prints winMessage; otherwise, execution continues with the nested if-else structure to check for the other possible sums.

if (comeOutRoll == 7) {
   System.out.println(winMessage); 
} else {
   if (comeOutRoll == 11) {
      System.out.println(winMessage);
   } else {
      if (comeOutRoll == 2) {
         System.out.println(loseMessage);
      } else {
         if (comeOutRoll == 3) {
            System.out.println(loseMessage);
         } else {
            if (comeOutRoll == 12) {
               System.out.println(loseMessage);
            } else {
               System.out.println(continueMessage);
            }
         }
      }
   }
}

The fully nested version is shown here only as a learning aid — it precisely mirrors the execution logic. But once this has been seen, the behavior of a conventionally formatted if-else chain is easy to understand.