Switch Statement

The switch Statement

The switch statement allows you to provide a list of possible values for a variable and code to execute if each is true. In C#, however, the variable you compare in a switch statement must be either an integer or a character type and must be enclosed in parentheses:

switch ( j ) {

case 12:

System.out.println(“Noon”);

break;

case 13:

System.out.println(“1 PM”); ”

break;

default:

System.out.println(“some other time...”);

}

Note particularly that a break statement must follow each case in the switch statement. This is very important, as it says “go to the end of the switch statement.” If you leave out the break statement, the code in the next case statement is executed as well.