Lesson 8

Switch Statements

Author: Brandon Fajardo (9/29/17). Updated (8/6/20)

Introduction

Review: Over the past few past lessons, we have focused on the traditional types of logical statements that can be found in any programming language. These included if statements, else if statements, and else statements.

Lesson topic: Yet, there are so many more types of statements that we can use in more unique circumstances. As such, we will teach one of these unique statements in this lesson, the switch statement.

Topic 1 - Switch Statement

Switch statement: a statement that tests an input against multiple possible conditions.

This kind of statement acts as a way to better organize your code in a specific circumstance. In particular, the switch statement is useful when working with larger lists of a particular type of condition.

Here is the notation:

int casenumber;
data type var;

switch (casenumber) {
 case 1: var = val1;
 break;
 case 2: var = val2;
 break;
 ...
 default: var = defaultval;
 break; (optional)
}

The chart below shows the flow of this type of statement.

Image: https://www.geeksforgeeks.org/switch-statement-in-java/

Topic 2 - Composition of a Switch Statement

As we can see, the switch statement is composed of many new parts that follow a syntax unique to anything we’ve seen so far.

Despite this, each component of a switch statement is simple enough to understand. In fact, much of the same logic is used in a switch statement when compared to a chain of else-if conditions.

The logic of a switch statement follows:

  1. The switch statement takes in a variable input. In our example, this is “casenumber”.

  2. The switch statement is composed of multiple cases (possible outputs). Using our variable, we go through each case, starting from the top. For each case, we check if our casenumber variable matches the number of the case. EX: If casenumber is 10, we would want to continue until we found case 10.

  3. Once we have found the case, we set the value of a variable to whatever value is appropriate for that case. EX: In our example, if we ran case 2, our “var” variable would then be equated to “val2”.

  4. Finally, if no case is satisfied, we run the default case.

Note:

  • Any amount of cases can be used. Only one default case can be used.

  • All cases and defaults end in colons : (which is a new symbol to us in Java).

  • All assignment statement ends in semicolons ;.

Subtopic 1 - Break

break; 

This indicates the end of each case. If no break is used, the code will continue to run through cases until it hits a break or reaches the end of the switch statement. This is called a fall-through.

The break after default is optional because at that point the switch statement has already concluded.

Subtopic 2 - Default

default:

This is a special type of case that runs if no case is found which matches the value entered. It is essential to a switch statement. If no default is used, the switch can never conclude and the code after it can’t run.

Subtopic 3 - Using other data types for cases

So far, you know that you can check if a given variable matches a certain case if they are equal integers. In addition, it is possible to use non-integer cases.

EX:

switch (...) {
 case ‘A’: ...;
 break;
 case ‘B’: …;
 break;
 ...}

Example - This switch statement takes the number of a given month and returns the month’s full name as a String.

int monthnum;
String month;
switch (monthnum) {
 case 1:  month = "January";
 break;
 case 2:  month = "February";
 break;
 case 3:  month = "March";
 break;
 case 4:  month = "April";
 break;
 case 5:  month = "May";
 break;
 case 6:  month = "June";
 break;
 case 7:  month = "July";
 break;
 case 8:  month = "August";
 break;
 case 9:  month = "September";
 break;
 case 10: month = "October";
 break;
 case 11: month = "November";
 break;
 case 12: month = "December";
 break;
 default: month = "Invalid month";
 break;
}
System.out.println(month);

Problem #1:

Situation: You’re coding a magical game in Java. For this game, you have found it convenient to represent each magical status effect as an integer. Problem: You want a convenient way to convert each integer to the corresponding message that prints when an effect has affected a character/enemy. Instead of writing a long chain of else-if statements, you want to use a switch statement to organize your code better!

Conclusion

Recap: In this lesson, you learned about all the components of a switch statement, including case, break, and default.

Review: In the next lesson, you will cover very different types of functions in Java which allow you to run code in loops. These include the while, do-while, and for loops.

Solutions

Problem #1:

int statuseffect = 1;
int health = 100;
String effect;

switch (statuseffect) {
case 1: effect = "Fire Ailment: Minus 5 health";
break;
case 2: effect = "Ice Ailment: Minus 3 health";
break;
case 3: effect = "Heartbreak Ailment: Instant Death";
break;
default: effect = "Error. Report to game developer";
break; (optional)
}
System.out.println(effect);



if (statuseffect == 1) {
health-=5;
System.out.println(health + " hitpoints left");
}
if (statuseffect == 2) {
health-=3;
System.out.println(health + " hitpoints left");
}
if (statuseffect == 3) {
health-=100;
System.out.println(health + " hitpoints left");
}
if (health <= 0) {
System.out.println("GAME OVER");
}

This idea and example given by: Anthony Nuon-Prak (2/10/18)

Last updated