switch

Definition

A switch statement is used as an alternative to if-then-else statements to chose one (sometimes more) of several possibilities.

Format

switch (expression) {
    case constant:  statement1;
    case constant:  statement2; 
    ...
    default: statment3;
}

The expression of the switch must be a int, short, or byte that is primitive or wrapped, or an enum type. Once the expression is evaluated execution will skip to the case that matches the evaluation or the default case (if there is one) if the evaluation does not match any of the cases.

Fall-through

When using a switch we often employ break's to avoid fall-through. Fall-through is when..

Example

Here is an example code that uses the switch statement.

int day = 6;
 
switch (day) {
    case 1:  System.out.println("Sunday"); 
        break;
    case 2:  System.out.println("Monday"); 
        break;
    case 3:  System.out.println("Tuesday"); 
        break;
    case 4:  System.out.println("Wednesday"); 
        break;
    case 5:  System.out.println("Thursday"); 
        break;
    case 6:  System.out.println("Friday"); 
        break;
    case 7:  System.out.println("Saturday"); 
        break;
    default: System.out.println("Invalid day");
}

In the example the output will be:
Friday

If-then-else

This if-then-else code would output the same line as the switch code above.

int day = 6;
 
if (day == 1) {
    System.out.println("Sunday");
} 
else if (day == 2) {
    System.out.println("Monday");
}
else if (day == 3) {
    System.out.println("Tuesday");
}
else if (day == 4) {
    System.out.println("Wednesday");
}
else if (day == 5) {
    System.out.println("Thursday");
}
else if (day == 6) {
    System.out.println("Friday");
}
else if (day == 7) {
    System.out.println("Saturday");
}
else {
    System.out.println("Invalid day");
}

When to use

Switch statements are usually used when the output is based on a single integer.

If-then-else statements are usually used when decisions are based on ranges of values or conditions.

Situations by Jack Johnson

Here is a switch based on the song Situations by Jack Johnson.

Lyrics

Situation number one:
It's the one that's just begun,
But evidently it's too late.
Situation number two:
It's the only chance for you;
It's controlled by denizens of hate.
Situation number three:
It's the one that no one sees;
It's all too often dismissed as fate.
Situation number four:
The one that left you wanting more,
It tantalized you with its bait.

Code

switch(situation){
    case 1: System.out.println("It's the one that's just begun. But evidently it's too late.");
        break;
    case 2: System.out.println("It's the only chance for you. It's controlled by denizens of hate.");
        break;
    case 3: System.out.println("It's the one that no one sees. It's all too often dismissed as fate.");
        break;
    case 4: System.out.println("The one that left you wanting more. It tantalized you with its bait.");
        break;
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License