Using switch statements

Let's say you're programming a traffic light. There are three possible conditions for the traffic light—red, yellow, or green—and you want something different to happen based on the color of the light. To do this, you can chain multiple if statements together:

  1. Type in and run the following code:
// Implementing a traffic light program using multiple if statements
var trafficLight = "Yellow"
// Try changing the value of trafficLight to get different results
if trafficLight == "Red" {
print("Stop")
} else if trafficLight == "Yellow" {
print("Caution")
} else if trafficLight == "Green" {
print("Go")
} else {
print("Invalid Color")
}

The first if condition, trafficLight == "Red", returns false, so the statement after else is executed. The second if condition, trafficLight == "Yellow", returns true, so Caution is printed in the Debug area and no more if conditions are evaluated. Try changing the value of trafficLight to see different results. This works, but it's a little hard to read.

In this case, a switch statement works better. A basic switch statement looks like this:

switch value {
case firstValue:
code
case secondValue:
code
default:
code
}

The value is checked and matched to a case, and code for that case is executed. If none of the cases match, the code in default is executed.

  1. Here's how to write the if statement shown earlier as a switch statement. Type in and run the following code:
// the same traffic light program implemented using a switch statement
// Note: you can't fall-through to the next case once a case is matched
// Note: switch statements must cover all possible cases
trafficLight = "Yellow"
switch trafficLight {
case "Red":
print("Stop")
case "Yellow":
print("Caution")
case "Green":
print("Go")
default:
print("Invalid color")
}

The code here is much easier to read and understand when compared to the prior implementation, which used the if statement. The value in trafficLight is "Yellow", so case "Yellow": is matched and "Caution" is printed in the Debug area. Try changing the value of trafficLight to see different results.

There are two things to remember about switch statements:

  • First, switch statements in Swift do not fall through the bottom of each case and into the next one by default. In the example shown previously, once case "Red": is matched, case "Yellow":, case "Green": , and default: will not execute.
  • Second, a switch must cover all possible cases. So in the example shown previously, any trafficLight color other than "Red", "Yellow", or "Green" will be matched to default: and Invalid color will be printed in the Debug area.

This concludes the section on if and switch statements. In the next section, you'll learn about optionals, which allow you to create variables without initial values, and optional binding, which allows instructions to be executed if an optional has a value.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.145.114