Switch Statement

The finale control statement that we are going to look at is the switch statement. This statement works a little like the if / else, except it allows a multi-way branch rather than just a two way branch.  The basic form is:

swtich (control)
{
case cond1:
   ...
   break;

case cond2:
   ...
   break;

case cond3:
   ...
   break;

default:
   ...
   break;
}

Here control is some variable. It can be any of the integer data types as well and a defined enum type (more on enums later). If the control value is equal to cond1 then all of the code that follows the case cond1: will be executed until it reaches the break. Similarly, if the control value is equal to cond2, then all of the code that follows the ‘case cond2:’ will be executed until it reaches it’s break. You may have as many cases in a switch that you like. If control does not match any of the cases, then the code that follows the default line will be executed until it reaches it’s break. Note that the default case is optional, and if it is missing and the control does not match any of the cases, then nothing will be done.

Let’s now write a program which will read one character at a time from the console and print out Apple if the character is an ‘A’Banana if the character is a ‘B’ and Cherry if the character is a ‘C’.

import java.io.IOException;

public class HelloWorld 
{
	public static void main(String[] args) throws IOException 
	{
		char ch;
		
		System.out.println("Type a letter");
		while (true)
		{
			ch = (char) System.in.read();
			
			switch (ch)
			{
			case 'A':
				System.out.println("Apple");
				break;
				
			case 'B':
				System.out.println("Banana");
				break;
				
			case 'C':
				System.out.println("Cherry");
				break;
			}
		}
	}
}

Here we are using System.in.read() to read individual characters from the console. Note also the addition of ‘throws IOException’ to the declaration of our main() function. I don’t want to get into the details of Java exceptions except to say that we need this in order to be able to call System.in.read().

Now run your program and type in some letters and see how it behaves. Note that since we are using an infinite while loop here, you will need to stop the program manually.

When we specify the case statements, we can list multiple items for each block of code and if the condition matches any of those values, the associated block will be executed. To illustrate this let’s now change our program so that it will print the appropriate fruit if the user types either the capital letter or the lower case letter.

import java.io.IOException;

public class HelloWorld 
{
	public static void main(String[] args) throws IOException 
	{
		char ch;
		
		System.out.println("Type a letter");
		while (true)
		{
			ch = (char) System.in.read();
			
			switch (ch)
			{
			case 'A':
			case 'a':
				System.out.println("Apple");
				break;
				
			case 'B':
			case 'b':
				System.out.println("Banana");
				break;
				
			case 'C':
			case 'c':
				System.out.println("Cherry");
				break;
			}
		}
	}
}

Now I would like you to add two more cases, print Date for the letter ‘D’ and Elderberry for the letter ‘E’. After you are done, you can check your solution below:

.

.

.

.

import java.io.IOException;

public class HelloWorld 
{
	public static void main(String[] args) throws IOException 
	{
		char ch;
		
		System.out.println("Type a letter");
		while (true)
		{
			ch = (char) System.in.read();
			
			switch (ch)
			{
			case 'A':
			case 'a':
				System.out.println("Apple");
				break;
				
			case 'B':
			case 'b':
				System.out.println("Banana");
				break;
				
			case 'C':
			case 'c':
				System.out.println("Cherry");
				break;
				
			case 'D':
			case 'd':
				System.out.println("Date");
				break;
				
			case 'E':
			case 'e':
				System.out.println("Elderberry");
				break;
			}
		}
	}
}

Finally, it would be nice if we could have a character that will cause the program to quit. See if you can change your program so if the user types a ‘Q’ or ‘q’, the program exits the while loop, prints ‘Done!’ and then terminates. Check your solution to the one below.

.

.

.

.

import java.io.IOException;

public class HelloWorld 
{
	public static void main(String[] args) throws IOException 
	{
		char ch;
		boolean	run = true;
		
		System.out.println("Type a letter");
		while (run)
		{
			ch = (char) System.in.read();
			
			switch (ch)
			{
			case 'A':
			case 'a':
				System.out.println("Apple");
				break;
				
			case 'B':
			case 'b':
				System.out.println("Banana");
				break;
				
			case 'C':
			case 'c':
				System.out.println("Cherry");
				break;
				
			case 'D':
			case 'd':
				System.out.println("Date");
				break;
				
			case 'E':
			case 'e':
				System.out.println("Elderberry");
				break;
				
			case 'Q':
			case 'q':
				run = false;
				break;
			}
		}
		System.out.println("Done!");
	}
}

So what we did is create a boolean variable named run which we initialize to true. Then rather than doing while(true), we do while(run) so the loop will only execute only as long as run is true. Then all we need to do is set run to false when the user types either a ‘Q’ or ‘q’.

How did this compare to your solution?

Next: Classes