Free Web Tutorials Free Online Tutorials About Us Contact Us

Java If-Else-Switch Tutorial.


4. Java If-Else-Switch


Java Switch Statement::

Another way of checking conditions in java is by using the switch statement. You can switch between serveral possible awnsers quiet easily.

Example:

public class switchDemo {

	public static void main(String[] args) {
	int x = 2
	switch (x) {
  	case 0: 
	    doSomething0();
	    break;
	case 1: 
	    doSomething1();
	    break;
	case 2: 
    		doSomething2();
    		break;
  	case 3: 
	    doSomething3();
	    break;
	default: 
	    doSomethingElse();
}

} 
In the above example x = 2 so doSomething2(); is executed and then a break from the switch statement is executed, thus jumping out of the loop.


free online java tutorial