Classnotes | JAVA01 | RecentChanges | Preferences The if and else statements are good for situations with only a small handful of possible conditions. However, there will be times when you will need something which can provide you more options, that is where the switch statement comes in.
The basic structure of a switch statement is to provide some variable to test, and give several possible answers with a block of code associated with each of them. Here's an example:
switch(grade) {
case 'A' :
System.out.println("You got an A. Great job!");
break;
case 'B' :
System.out.println("You got a B Good work!");
break;
case 'C' :
System.out.println("You got a C. Eh.... it's okay");
break;
case 'D' :
System.out.println("You got a D. Woah, that's not good.");
break;
default :
System.out.println("You got an F. Dude, go flip burgers!");
break;