Most of you have probably seen switch statements before.
They're used in Java, C based languages, Ruby.
But they are arguably less intuitive than if else statements.
You'll be seeing a lot of them in Swift though, and
hopefully come to appreciate their conciseness.
Switch statements are great when you're trying to assess multiple conditions.
Especially when you're conditioning on multiple values of a given variable.
Here's an example of an if else statement,
that might be better written as a switch statement.
It's a statement about the years of the Chinese Zodiac.
You can see that even though I'm just looking at a few values that
match the years of the monkey and the years of the goat,
my statement is already getting a bit cumbersome.
If we write this as a switch statement it's easier to manage.
Switch statements take the form switch, then a type name, the type whose
value you're conditioning on, case, the first value possible for this type,
colon, then the statement that'll be executed if this case is true.
Note that you can combine two or
more values into the same case, as in this third case.
At the end of the statement comes a default case.
This last statement is executed if none of the other cases are true.
It's important to include this default case, because a switch statement must
account for every possible value of a given type.
Every value must match at least one case.
So for our Chinese zodiac example,
you can see that the statement switches on the value of the variable birth year.
If birth year has a value of 1992, 1980, or 1968,
this print statement is executed.
You were born in the year of the monkey.
If birth year has a value of 1991, 1979, or 1967,
the second print statement is executed.
So let's change birth year to 1991.
And we can see you were born in the year of the goat.
And if the value of birth year doesn't match any of these six values,
the default statement is executed.
So if this was 1993, the default would be executed.
Let's scroll down and look at another example.
Here's a program that depicts the visible spectrum.
It sets a variable, called color,
based on the value of a variable representing light wavelength.
Here we can see the ellipsis again.
Remember that in Swift this is the range operator.
So these values represent ranges of wavelength within which
certain colors fall.
If we alter the value of the wavelength variable, we can see
different values being assigned to the color variable in the sidebar.
Notice that the value of wavelength doesn't have to match the specified
range, it just has to fall within it.
One aspect that distinguishes switch statements in Swift,
from switch statements in C for example,
is that the flow of control does not fall through from one case to the next.
What do I mean by fall through?
Well, imagine moving through the switch statement, and
checking each case in turn.
What should happen when a condition is met?
Should execution of the statement stop, or
move on to evaluate the rest of the cases?
To fall through is to continue to evaluate the remaining cases.
In Swift, if the conditions of one case are met, the statement is done.
Let's look a little closer at what happens when switch statements don't
fall through.
This example switch is on temperature.
I live in California, so I'm pretty much a wimp when it comes to the cold.
Here you can see that 55 degrees appears in both the first and the second case.
So what will be the output?
When the temperature's 55 the console prints out brrr, and
then it's finished evaluating the statement.
It's done.
The second case is never evaluated, the console never prints out comfortable.
Even though 55 does match that second case.
So what if I left a statement out?
What would happen?
Let's find out.
What if I left out this statement?
Okay, we get an error.
What does it say?
Case label in a switch should have at least one executable statement.
Every case needs an accompanying statement, because with no fall through,
if a case matches, the program needs to be able to execute a statement and
then be done.
That's all for now on control flow.
Be sure to complete the exercises to get lots of practice with for in loops and
switch statements.
And stay tuned for the next lesson on functions.