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:

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’.

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.

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:

.

.

.

.

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.

.

.

.

.

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

Pierre Pierre, who replica handbag 185cm tall, gucci replica handbags the "Diamond King" in his hermes replica handbags . He has been selected as one of the handbag replica most beautiful 50 people in the "People" magazine. It is also known as replica handbags most elegant and quiet in the world. Unforgettable prince.