Java Arena
Step 1 of 5

The switch statement

This module is an extra: the MOOC doesn't cover it. It shows three pieces of Java that the course never needed, but that you'll meet in a lot of Java code written today: switch, which picks one of several branches by a value; var, which lets the compiler work out a local variable's type; and text blocks with String.format, for text of several lines and for lining up columns. None of them lets a program do something it couldn't do before. They make some code shorter, and often easier to read.

One value, many branches

A snack machine has numbered buttons. With the tools of the course, a program that names the snack behind a button is a chain of if and else if:

int button = 3;
if (button == 1) {
    System.out.println("Chips");
} else if (button == 2) {
    System.out.println("Chocolate bar");
} else if (button == 3) {
    System.out.println("Apple");
} else {
    System.out.println("No such button");
}
Output
Apple

Every condition compares the same variable with a different value. The switch statement says exactly that:

int button = 3;
switch (button) {
    case 1:
        System.out.println("Chips");
        break;
    case 2:
        System.out.println("Chocolate bar");
        break;
    case 3:
        System.out.println("Apple");
        break;
    default:
        System.out.println("No such button");
}
Output
Apple
  • switch (button) works out the value in the parentheses once. Then the program jumps to the case label with the same value, here case 3:, and runs the statements after it.
  • break ends the switch: the program goes on after its closing brace.
  • default: is where the program jumps when no label matches. It's optional: without it, a value that matches no label does nothing.
  • A label is a constant: a literal such as 3, 'x' or "tea", or a final variable given a literal. Two labels of one switch can't have the same value; javac stops with "duplicate case label".
  • A switch on constants works on an int (and on the smaller whole-number types and char), on a String (since Java 7, step 3) and on an enum (step 3). It doesn't work on a double, a long or a boolean: switch (price) with a double price gives javac's error "selector type double is not allowed".

Falling through

Here's what happens when a break is missing:

int button = 1;
switch (button) {
    case 1:
        System.out.println("Chips");
    case 2:
        System.out.println("Chocolate bar");
        break;
    case 3:
        System.out.println("Apple");
        break;
}
Output
Chips
Chocolate bar

A case label only marks where the program starts. Once it's running the statements of a case, it keeps going, past the next label and into the next case's statements, until a break (or the end of the switch). This is called falling through. A forgotten break is the classic switch bug: javac says nothing, and the program does too much. The arrow labels of step 2 don't fall through, which is one reason they're preferred today.

Several values, one branch

Falling through has one honest use: several labels in a row share the statements after the last of them. Buttons 4 and 5 are both water, still and sparkling:

int button = 5;
switch (button) {
    case 1:
        System.out.println("Chips");
        break;
    case 4:
    case 5:
        System.out.println("Water");
        break;
    default:
        System.out.println("Something else");
}
Output
Water

case 4: has no statements of its own, so it falls straight through to case 5:. Since Java 14, the same can be written on one line, with the values separated by commas: case 4, 5:.

Your task

Challenge 1 of 3

A weekly pill organizer numbers its boxes from 1 (Monday) to 7 (Sunday). Read a day's number, a whole number on one line, and print the day's name with a switch: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday or Sunday. For any other number, print No such day. With the input 3, the program prints:

Wednesday

Challenge 2

In the board game Ladder Race, a roll of the die decides what you do. The program reads a roll, a whole number on one line, and prints what it means. With a roll of 2 it should print Pick a card, but it prints two lines. Find the bug and fix it, keeping the switch. With the input 2, the program prints:

Pick a card

Challenge 3

The pill organizer's reminder now only needs to know the kind of day. Read a day's number (1 is Monday, 7 is Sunday), a whole number on one line, and print weekday for 1 to 5, weekend for 6 and 7, and No such day for any other number. Use a switch where several labels share one branch, so that the program has only three println lines. With the input 6, the program prints:

weekend