Friday, April 25, 2008

Java - switch example

Java has a switch statement like most other languages. This example program demonstrates the switch statement without "break" after each case.

The output will be :This is three when x=3
This is default when x not equal to 1 or 2 or 3


class DefSwitch{
public static void main(String args[]){

int x=3;

switch(x){
case 1:
System.out.println("This is one");
default:
System.out.println("This is default");
case 2:
System.out.println("This is two");
case 3:
System.out.println("This is three");

}

}
}

No comments: