knowledge-kitchen

Branching - Controlling the Flow of a Program (in Java)

Two roads diverged in a yellow wood,

And sorry I could not travel both

-Robert Frost, The Road Not Taken

  1. Overview
  2. Flow Charts
  3. If/Else if/Else
  4. Switch/Case
  5. The Ternary Operator
  6. Conclusions

Overview

Concept

The flow of a program can be controlled such that it deviates from its ‘normal’ path.

Flow Charts

Concept

Understanding the flow of a program’s control can be visualized easily with diagrams, irrespective of the programming language and code.

Example

A virtual card game program randomly picks two cards, from 1 to 11.

Example

Blackjack example diagram

UML Activity Diagram Rules

UML Activity Diagrams follow a few basic rules:

If/Else if/Else

Isolated ifs

if statements on their own either execute a particular branch or not.

int card1 = (int) (Math.random() * 11) + 1; // number from 1 to 11
int card2 = (int) (Math.random() * 11) + 1; // number  from 1 to 11

if (card1 + card2 == 21) {
    // if the cards add up to 21...
    System.out.println("Blackjack!");
}

Binary choice

if statements followed by else blocks can have control flow into one of two branches, depending on a single condition being met or not.

int card1 = (int) (Math.random() * 11) + 1; // number from 1 to 11
int card2 = (int) (Math.random() * 11) + 1; // number  from 1 to 11

if (card1 + card2 == 21) {
    // if the cards add up to 21...
    System.out.println("Blackjack!");
}
else {
    // otherwise...
    System.out.println("Maybe next time!");
}

n-ary

if statements followed by one or more else if blocks can have control flow into one of several different branches, depending on multiple conditions.

int card1 = (int) (Math.random() * 11) + 1; // number from 1 to 11
int card2 = (int) (Math.random() * 11) + 1; // number  from 1 to 11

if (card1 + card2 == 21) {
    // if the cards add up to 21...
    System.out.println("Blackjack!");
}
else if (card1 == 1 && card2 == 1) {
    // otherwise if the cards are both ones...
    System.out.println("Snake eyes!");
}
else {
    // otherwise...
    System.out.println("Maybe next time!");
}

UML Activity Diagram

Blackjack example diagram

Switch/Case

Concept

In Java, an alternative to if/else if/else statements is switch/case statements.

Example

Imagine a program that recommends how to dress.

UML activity diagram for weather program

Not using switch / case

This could be handled by if/else if/else statements with some boolean operators thrown in.

Scanner scn = new Scanner(System.in);
String weather = scn.nextLine();

if ( weather.equals("very sunny") || weather.equals("raining") ) {
    // if it's raining or very sunny...
	System.out.println("Take an umbrella");
}
else if (weather.equals("snowing") ) {
    // otherwise, if it's snowing...
	System.out.println("Wear a hat!");
}
else {
    // otherwise...
	System.out.println("Good luck!");
}

Using switch / case

Using switch/case would remove the need for boolean operations and provide more readable code.

Scanner scn = new Scanner(System.in);
String weather = scn.nextLine();

switch (weather) {
    case "raining":
    case "very sunny":
        // if it's raining or very sunny...
        System.out.println("Take an umbrella!");
        break;
    case "snowing":
        // otherwise, if it's snowing...
        System.out.println("Wear a hat!");
        break;
    default:
        // otherwise...
        System.out.println("Good luck!");
}

The Ternary Operator

Concept

It is often the case that we use if/else statements to control setting the value of a variable.

if (breakfastWasServed && breakfastWasEaten) {
  satiationLevel = "full"
} else {
  satiationLevel = "hungry"
}

The ternary operator, ?, thankfully allows us to simplify such syntax:

satiationLevel = breakfastWasServed && breakfastWasEaten ? "full" : "hungry"

Conclusions

You now have a basic understanding of UML activity diagrams, if/else if/else statements, and switch/case statements.