Tuesday, March 15, 2016

Intro To Java: Section 3.3 - Using Switch Statements

Topic 3.3 Overview

In this section we will cover how to use another control structure known as a Switch.

Lecture Video 3.3

Switch Statements

Switch statements in Java allow you to easily define certain paths in which code can be executed based on specific values. Switches are composed of two parts - the switch, which takes the value, and the case, which corresponds to a particular value. For example, you could create a switch that takes a number representing a particular day of the week, and then each case could print out what day it is based on that value. Although you could achieve the same thing using multiple if/else statements. it is much cleaner in this scenario to use a single switch. The end of each code section should end with a break; to end the execution. If you do not include a break, once the case is satisfied, Java will continue executing the following cases regardless of whether or not the value matches the particular case.

public class DayOfWeek{
 public static void main(String[] args) {
  //Day of the week
  int day = 5;
  switch (day) {
   case 1: System.out.println("It is Sunday"); break;
   case 2: System.out.println("It is Monday"); break;
   case 3: System.out.println("It is Tuesday"); break;
   case 4: System.out.println("It is Wednesday"); break;
   case 5: System.out.println("It is Thursday"); break;
   case 6: System.out.println("It is Friday"); break;
   case 7: System.out.println("It is Saturday"); break;
   //Handle all other cases
   default: break;
It is Thursday

The above code prints out "It is Thursday" because it begins executing code at the 5th case. If the break statements were not there the blocks would fall through - Cases 5, 6, and 7 would all be executed without a break statement. The default case handles all of the other possible cases - in this case they're all just set to break.

Combining Cases

In addition to having a single case with a single set of actions, it is possible to have multiple cases perform one set of actions. You do this simply by adding multiple cases to a set of actions. The example below demonstrates this.

public class Test {

 public static void main(String[] args) {
  int someValue = 5;
  //Create Switch
  //Operations for cases 1, 2, and 3
  case 1: case 2: case 3: 
   System.out.println("Case 1, 2, or 3 has been triggered.");
  //Operations for cases 4, 5, and 6
  case 4: case 5: case 6:
   System.out.println("Case 4, 5, or 6 has been triggered.");
  //Default case for all other cases not caught in 1,2,3,4,5,6
  default: break;

Case 4, 5, or 6 has been triggered.

The system prints out that case 4, 5, or 6 has been triggered because someValue is equal to 5. It would also trigger if someValue was equal to 4 or 6. The first block would trigger if the value was 1, 2 or 3, and nothing would be printed if any value less than 1 or greater than 6 is given.

Review Exercise 3.3 - Menu Calculator

Instructions: Create a calculator which prompts the user to enter two values. It should then display a menu with 4 options - 1 to find the sum, 2 to find the difference, 3 to find the product, and 4 to find the quotient. The program should determine which number the user entered using a scanner and then calculate the result within the context of a switch and print out the result to the screen.

Sample Output:

Welcome to Calculator.
Please enter a first value:
Please enter a second value:
Would you like to:
1) Find the sum
2) Find the difference
3) Find the product
4) Find the quotient
Calculated Sum: 15.00


Post a Comment