Learning Java
Custom Search

Previous Next

switch..case Statement

The "switch..case" construction allows us to test multiple values of an expression or a variable and perform different tasks depending on that value. This can be accomplished using the "if..else if" construction but can become a bit cumbersome. The format is similar to the following. The "default" keyword is used to point out what should be done if the above conditions are not met. The "default" is not required. The "break" keyword stops execution from falling through to the next statement.

	switch (expression) {
	    case value1:
		execute this step 1;
		execute this step 2;
		execute this step 3;
	 	break;
	    case value2:
		execute this step 4;
		execute this step 5;
		execute this step 6;
	 	break;
	    case value3:
		execute this step 7;
		execute this step 8;
		execute this step 9;
	 	break;
	    default:
		execute this step 10;
		execute this step 11;
		execute this step 12;
	}

The following "if..else" construction and "switch..case" construction do the same thing.

	int quarter = 2;

        if (quarter == 1) { 
	    System.out.println("Jan start project A"); 
	    System.out.println("Feb & Mar complete phase I");
	} else if (quarter == 2) { 
	    System.out.println("Apr & May get financing"); 
	    System.out.println("Jun start phase II"); 
	} else if (quarter == 3) { 
	    System.out.println("Jul & Aug finish phase II"); 
	    System.out.println("Sep start phase III"); 
	} else if (quarter == 4) { 
	    System.out.println("Oct & Nov finish phase III"); 
	    System.out.println("Dec start plan for next year");
	} else {  
	    System.out.println("Invalid quarter.");
        }


	int quarter = 2;

        switch (quarter) {
            case 1:  
		System.out.println("Jan start project A"); 
		System.out.println("Feb & Mar complete phase I");
		break;
            case 2:  
		System.out.println("Apr & May get financing"); 
		System.out.println("Jun start phase II"); 
		break;
            case 3:  
		System.out.println("Jul & Aug finish phase II"); 
		System.out.println("Sep start phase III"); 
		break;
            case 4:  
		System.out.println("Oct & Nov finish phase III"); 
		System.out.println("Dec start plan for next year");
		break;
            default: 
		System.out.println("Invalid quarter.");
        }

Notice in the following execution when the "break" keywords are commented out, all the statements below the true condition are executed.

class SwitchTest {

    public static void main(String args[]) {

	int quarter = 2;

        switch (quarter) {
            case 1:  
		System.out.println("Jan start project A"); 
		System.out.println("Feb & Mar complete phase I");
		//break;
            case 2:  
		System.out.println("Apr & May get financing"); 
		System.out.println("Jun start phase II"); 
		//break;
            case 3:  
		System.out.println("Jul & Aug finish phase II"); 
		System.out.println("Sep start phase III"); 
		//break;
            case 4:  
		System.out.println("Oct & Nov finish phase III"); 
		System.out.println("Dec start plan for next year");
		//break;
            default: 
		System.out.println("Invalid quarter.");
        }
    }
}

Previous Next

Steps In Learning

Contact us

Copyright © 2008      N. Nelson      All Rights Reserved