Format

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..

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){case1: System.out.println("It's the one that's just begun. But evidently it's too late.");
break;
case2: System.out.println("It's the only chance for you. It's controlled by denizens of hate.");
break;
case3: System.out.println("It's the one that no one sees. It's all too often dismissed as fate.");
break;
case4: System.out.println("The one that left you wanting more. It tantalized you with its bait.");
break;
}