Wednesday, June 30, 2010

How to write switch statements



class SwitchExample {

public static void main(String[] args) {
int a = 2;
switch (a) {
case 1:
System.out.println("a is equal to 1");
break;
case 2:
System.out.println("a is equal to 2");
break;
case 3:
System.out.println("a is equal to 3");
break;
default:
System.out.println("value of a is not 1,2,or 3");
}
}
}

The output will be: a is equal to 2
Note: only char, byte, short, int can be used in switch variable

No comments:

Post a Comment