Learn
Booleans and Comparison Operators
Switch Statement

We often want to compare a value, expression, or variable against many different possible values and run different code depending on which it matches. We can use a series of if/elseif statements which use the identical operator (===) or we can use a switch statement—an alternate syntax.

We wrote some code with if/elseif statements to print a string based on a student’s letter grade:

if ($letter_grade === "A"){ echo "Terrific"; } elseif ($letter_grade === "B") { echo "Good"; } elseif ($letter_grade === "C") { echo "Fair"; } elseif ($letter_grade === "D") { echo "Needs Improvement"; } elseif ($letter_grade === "F") { echo "See me!"; } else { echo "Invalid grade"; }

Since this code involves a series of comparisons, it’s ripe for a switch statement! Let’s see it refactored with switch:

switch ($letter_grade){ case "A": echo "Terrific"; break; case "B": echo "Good"; break; case "C": echo "Fair"; break; case "D": echo "Needs Improvement"; break; case "F": echo "See me!"; break; default: echo "Invalid grade"; }

We begin the keyword switch followed by the value (or expression) we’ll be comparing—in this case, $letter_grade. We provide possible matches for the expression with the keyword case, the potential matching value, and the colon. For each case, we provide code that should run if that case matches. After each case, we include the keyword break to break out of the switch statement. We can provide a default that should run if none of the provided cases match.

A switch statement is a good example of code that might be preferable not because it’s shorter, but rather because it clearly indicates the purpose of the code; when looking at a switch statement we can quickly identify the important aspects of the code; this makes it easier to understand, extend, and debug.

Instructions

1.

The U.S. government has multiple classifications of air quality each symbolized by a color:

  • The color "green" indicates the air quality is "good".
  • The color "yellow" indicates the air quality is "moderate".
  • The color "orange" indicates the air quality is "unhealthy for sensitive groups".
  • The color "red" indicates the air quality is "unhealthy".
  • The color "purple" indicates the air quality is "very unhealthy".
  • And the color "maroon" indicates the air quality is "hazardous".

Write a function, airQuality(), that takes in a color as a string, and prints the corresponding air quality.

Your function should use a switch statement. You should provide a default string of "invalid color" for any input aside from the six colors listed.

2.

Test your function with several inputs to make sure it’s working as expected.

Folder Icon

Take this course for free

Already have an account?