else if
statements are great for checking multiple conditions. However, we can find ourselves needing to check so many conditions that writing all the necessary else if
statements can feel tedious. For example:
clothingChoice := "sweater" if clothingChoice == "shirt" { fmt.Println("We have shirts in S and M only.") } else if clothingChoice == "polos" { fmt.Println("We have polos in M, L, and XL.") } else if clothingChoice == "sweater" { fmt.Println("We have sweaters in S, M, L, and XL.") } else { fmt.Println("Sorry, we don't carry that.") }
In the code above, we have a series of conditions checking for a value that matches a clothingChoice
variable. Our code works fine, but imagine if we needed to check more values! Having to write additional else if
statements sounds like a pain!
Instead, we can use a switch
statement that uses alternative syntax that is easier to read and write. Below is an example of a switch
statement:
clothingChoice := "sweater" switch clothingChoice { case "shirt": fmt.Println("We have shirts in S and M only.") case "polos": fmt.Println("We have polos in M, L, and XL.") case "sweater": fmt.Println("We have sweaters in S, M, L, and XL.") case "jackets": fmt.Println("We have jackets in all sizes.") default: fmt.Println("Sorry, we don't carry that") } // Prints: We have sweaters in S, M, L, and XL.
Let’s break down what happens in a switch
statement:
- The
switch
keyword initiates the statement and is followed by a value. In the example, the value afterswitch
is compared to the value after eachcase
, until there is a match. - Inside the
switch
block,{ ... }
, there are multiplecase
s. Thecase
keyword checks if the expression matches the specified value that comes after it. The value following the firstcase
is"shirt"
. Since the value ofclothingChoice
is not the same as"shirt"
, thatcase
‘s code does not run. - The value of
clothingChoice
is"sweater"
, so the thirdcase
‘s code runs and"We have sweaters in S, M, L, and XL."
is printed. No morecase
statements are checked. - At the end of our
switch
statement is adefault
statement. If none of thecase
s are true, then the code in thedefault
statement will run.
Instructions
Add a switch
statement that takes the condition name
.
Add one case
statement with the value "Butch"
and inside its block print the string "Head to Robbers Roost."
.
Add a second case
statement with the value "Bonnie"
and inside its block, print the string "Stay put in Joplin."
.
Add a default
statement that will print out "Just hide!"
.