Selection
Home - About Us
Introduction To Selection More Selection Switch

The Switch Statement

With the switch statement the flow of execution is based on the value of a discrete value, such as the int, byte, short or char built-in data types. The switch statement cannot branch its flow of execuation via a range of data values, nor can the decision be base on f loat or doubles value. The general structure of the switch statement is:

	switch (expression)
	{
		case label: 
				statement1;
				statement2;
				...
		case label2:
				statement3;
				statement4;
					 
		case label3:
				statement5;
				statement6;
			 
		default:
				statement7;
				statement8;
	}

When the program's flow of execution reaches the switch statement the expression is evaluated. The value of the expression is compared to the labels, one-at-a-time, from top to bottom. The first label that matches the value is the point at which the flow of execution begins again -- the statement associated with the matching label, and all statements from there to the end of the switch statement are performed. If the value does not match any of the labels then the flow of execution picks up with the "default" statement(s).

For example

	 char ch = 'c';
	 ...
	 switch (ch)
	 {
		case 'a':  System.out.println("aardvark");
		
		case 'c':  System.out.println("cat");
		
		case 'd':  System.out.println("dog");
		
		case 'e':  System.out.println("elephant");
		
		default:   System.out.println("none");
	}

In the above example the variable ch resolves to the character 'c'. This matches the second label, so the statement associated with that label, System.out.println("cat"), is performed, as well as all of the statements from there to the end of the switch statement. Therefore, the output of the above code fragment would be:

	cat
	dog
	elephant
	none

Usually a programmer would like the program to only perform the statement, or series of statements, associated with the matching label. This can be accomplished with break statements. When a break statement is encountered then the flow of execution skips to the end of the switch statement. The preceding code has been modified to include some break statements:

	 switch (ch)
	 {
		case 'a':  
			System.out.println("aardvark");
			break;
		
		case 'c':  
			System.out.println("cat");
			System.out.println(" - a great pet!");
			break;
		
		case 'd':  
			System.out.println("dog");
			break;
		
		case 'e':  
			System.out.println("elephant");
			break;
		
		default:   System.out.println("none");
	}

Now the output when ch is a 'c' is:

	cat
	 - a great pet!

If ch is a 'd' then the output would be "dog". If ch is an 'e' the output would be "elephant". If ch is a value that does not match any of the labels, such as a 'g', then the statement associated with default is executed -- in this case the output would be "none".

More than one label can be stacked up on a statement:

	 switch (ch)
	 {
		case 'a': case z: 
		case 'v':  
			System.out.println("aardvark");
			break;
		
		case 'c':  
			System.out.println("cat");
			System.out.println(" - a great pet!");
			break;
		
		case 'd':  
			System.out.println("dog");
			break;
		
		case 'e':  
			System.out.println("elephant");
			break;
		
		default:   System.out.println("none");
	}

Three labels have been placed on the "aardvark" statement. If ch is an 'a', a 'z', or a 'v', then the "aardvark" statement is performed.

Determine the output of the following code if val is 16:

	 int val;
	 val = 16;
	 
	 switch (val)
	 {
		case 5: 
		case 50:   
			System.out.println("Five or fifty");
			break;
		
		case 7:    
			System.out.println("Seven");
			val--;
		
		case 16:
		case 32:   
			System.out.println("Val is: " + val);
			break;
		
		default:   
			System.out.println("Not one of the above.");
	}

What is the output of the above code if val is 5? If val is 7? If val is 100? Note that not all of the groups of statements end with a break statement.

Home - About Us
Copyright © 2006 by Kiowok, Ann Arbor, Michigan, USA