The general form of the switch statement is:
switch (expression) {
case constant1: code block
case constant2: code block
default: code block
}
A switch's expression must evaluate to a char, byte, short, int, or an enum.
If you're not using an enum, only variables that can be promoted to an int are acceptable.
public class MainClass{
public static void main(String[] argv){
int x = 3;
switch (x) {
case 1:
System.out.println("x is equal to 1");
break;
case 2:
System.out.println("x is equal to 2");
break;
case 3:
System.out.println("x is equal to 3");
break;
default:
System.out.println("Still no idea what x is");
}
}
}
|