Skip to content

Switch / Case#

You already know if and else (and else if) — but sometimes you wish to choose between a whole lot of options. In those cases, switch and case come into play. Let's assume you want to use four different keys to control a dot on the screen:

float x = 0;
float y = 0;

void setup() {
    size(100, 300);
}

void draw() {
    background(0);
    if (keyPressed) {
        if (keyCode == LEFT) {
            x -= 10;
        } else if (keyCode == RIGHT) {
            x += 10;
        } else if (keyCode == UP) {
            y -= 10;
        } else if (keyCode == DOWN) {
            y += 10;
        }
    }
    circle(x, y, 10);
}
float x = 0;
float y = 0;

void setup() {
  size(100, 300);
}

void draw() {
    background(0);
    if (keyPressed) {
        switch (keyCode) {
            case LEFT:
                x -= 10;
                break;
            case RIGHT:
                x += 10;
                break;
            case UP:
                y -= 10;
                break;
            case DOWN:
                y += 10;
                break;
        }
    }
    circle(x, y, 10);
}

The switch/case is slightly longer than the if/else version. It has one thing going for it, in my opinion: In an if/else statement, you can mix and match whatever you want. Each else if could be completely unrelated to the other things — one could check for the mouse instead, for all we know. switch/case on the other hand has one single variable you put into its parentheses. No operators. No boolean logic. Just one variable. This is the only thing it does: compare a variable against a number of possible options, one in each case statement. So, case is followed by some value like 15 (or a variable leading to an int as in this case) and then a : colon. All following instructions will be run (we'll come back to this one). A break; then marks the end of this specific set of instructions.

Copy and Experiment

You should try extending the switch example with other special keys. Try these: SHIFT, CONTROL, ALT. (some of these are exclusive to Windows, MacOS or Linux, just pick anything you want).

Default#

switch/case can have a default: case. When no case: matches, that default will be chosen instead. It could be used like this:

// maybe you asked the user to enter their
// favorite fruit. Here is their answer.
String userInput = "coconut";

switch (userInput) {
    case "apple":
        println("Apples make great pie!");
        break;
    case "banana":
        println("Bananas are the most produced fruit in the world by weight!");
        break;
    case "grape":
        println("Sure, you could eat it. But you could also make wine.");
        break;
    case "orange":
        println("They come in any color you want, as long as it's orange!");
        break;
    case "strawberry":
        println("Strawberries are delicious.");
        break;
    case "watermelon":
        println("It's a fruit and a thirst-quencher.");
        break;
    default:
        println("I don't know this. Are you sure this is a fruit?");
}

Here, we would take some kind of user input (well, simulated user input) and write something about them to the output window. The user chose "coconut", and that is not a thing we recognize. In those cases, the default will be run instead! You can think of it a little bit like else in an if-statement.

Falling Through#

Ever wonder what all those break lines are about? Let's take them out and see what happens!

// maybe you asked the user to enter their
// favorite fruit. Her is their answer.
String userInput = "grape";

switch (userInput) {
    case "apple":
        println("Apples make great pie!");
    case "banana":
        println("Bananas are the most produced fruit in the world by weight!");
    case "grape":
        println("Sure, you could eat it. But you could also make wine.");
    case "orange":
        println("They come in any color you want, as long as it's orange!");
    case "strawberry":
        println("Strawberries are delicious.");
    case "watermelon":
        println("It's a fruit and a thirst-quencher.");
    default:
        println("I don't know this. Are you sure this is a fruit?");
}
What happens if you run the code above?

Processing IDE with the code above and a bunch of output, starting at the "grape" output

Whoa, that's a lot of output! Where did that come from? Somehow, we're jumping straight into the "grape" output, but then we also print everything after that, too! This is called "falling through", and it's an odd and sometimes useful thing in switch/case.

Let's come up with a real-world example for this. Let's print a smiley based on a grade someone enters. Yes, this could also be done with if/else. This is about the behavior of break or its lack of behavior.

int grade = 100;

switch (grade) {
    case 100:
    case 130:
        println("🤩 excellent!");
        break;
    case 170:
    case 200:
    case 230:
        println("😃 good job!");
        break;
    case 270:
    case 300:
    case 330:
        println("🫡 OK.");
        break;
    case 370:
    case 400:
        println("😕 fine, I guess?");
        break;
    case 500:
        println("😵 inadequate.");
        break;
    default:
        println("👾 this does not appear to be a grade.");
}

Rare but good to know

switch/case does not come up that often, but it is a good construct whenever you have many options that you want to choose from. This includes things like handling keyboard input or switching between screens.