Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Switch statements can do much more than a single match and in this video, let's take a look at how we can match against multiple patterns.
-
0:00
Okay, so far even though what we did was pretty cool,
-
0:03
what we have here is mostly just an if statement with different keywords.
-
0:08
So each of these is an if else if else if else if else.
-
0:13
So why can't we just use an if statement?
-
0:16
Well, let's see how Switch Statements really shine by
-
0:19
adding a few more values to the array.
-
0:22
So in here, I'm going to add a comma, and I'll add LGW, add another comma,
-
0:28
then I'll add JFK, and finally ORY.
-
0:34
Now these new additions are the second international airports in London,
-
0:39
New York and Paris respectively.
-
0:43
So let's go back to our Switch Statement.
-
0:45
You'll notice now immediately that when the for loop executes and
-
0:49
once we hit LGW, JFK and ORY.
-
0:52
In addition to DXB, we don't know which cities these airports are in.
-
0:56
So now we've printed out four times, I don't know which city that airport is in,
-
1:00
because we hit the default statement four times.
-
1:03
We want to print the name of the city depending on the airport just like before,
-
1:07
but now we have some new airports.
-
1:09
If we were using an if statement, to get this to work we
-
1:12
would have to add an else clause for the new airports, an else if clause.
-
1:17
Or we could use a logical operator to create
-
1:20
one compound expression for that city.
-
1:23
For example, we do something like this,
-
1:27
if airportCodes[0] ==, double equal, so check the equality.
-
1:33
Will say LGA or airportCodes[0] == JFK,
-
1:40
then we'll print New York.
-
1:45
As you can see this is already a bit hard to read and
-
1:47
there are a couple places where we can make mistakes.
-
1:50
The string that we write here, we could forget to do double equals,
-
1:54
we could do not equals by mistake.
-
1:56
It's a bunch of things, right?
-
1:59
With a Switch Statement, it's so much easier.
-
2:01
Each case can match against multiple values and
-
2:05
that's why I said pattern earlier.
-
2:07
And to do this we simply separate each value with a comma.
-
2:11
So in this case, we'll say, if the airport that we're considering,
-
2:15
if the value that we're considering is either LGA or JFK, then print New York.
-
2:22
So now you'll see, New York is printed at the beginning when we hit LGA.
-
2:25
And then the second last value is New York again.
-
2:29
And that's all it took to make this a compound expression.
-
2:34
So let's do this for the rest.
-
2:36
So, we'll say, if this is either LHR or using a comma we'll add two values,
-
2:41
we'll say, LGW : print (London).
-
2:45
And finally, if this is CDG or ORY, then print Paris and that's it.
-
2:53
There's no limit really to how many values each case statement can match against.
-
2:58
If you look in the results area, you'll see that New York, London, and
-
3:02
Paris were matched two times each.
-
3:04
Hong Kong was matched once.
-
3:06
And we got the default result for
-
3:07
the Dubai International Airport because we didn't provide a case for it.
-
3:11
But let's take this a step further.
-
3:13
Let's go back to the other example we used when discussing ControlFlow,
-
3:17
the temperature.
-
3:18
Now this time let's use Fahrenheit to be fair.
-
3:21
For the purposes of weather, temperature in Fahrenheit goes from 32,
-
3:27
freezing point, to 120, in some extreme cases.
-
3:30
Rather than writing out a specific value for
-
3:32
the temperature, I'm going to generated it randomly.
-
3:35
I'm gonna write some code.
-
3:37
Don't worry about what's going on here.
-
3:38
So, I'll do, import GameKit.
-
3:42
I'll say, let randomTemperature
-
3:47
= GKRandomSource.sharedRandom.nextInt
-
3:54
(upperBound: 150).
-
3:59
So don't worry about what's going on here, I'm just using some of the code
-
4:04
Apple provides to generate a random number that goes from 0 to 150.
-
4:08
We'll talk more about functions in the future and
-
4:11
you can come back to this example and poke around.
-
4:14
For now it just makes the example a bit more interesting to work with.
-
4:18
You'll see that 11 was our randomly generated number.
-
4:21
And every time I hit Enter or evaluate the code again by hitting the play button,
-
4:26
we should get a new random number.
-
4:29
So earlier we used a compound if else statement to
-
4:32
print out the items of clothing we should add to our wardrobe based on the weather.
-
4:37
Let's combine two of the new concepts we've learned in this course.
-
4:41
Ranges and switch statements to make that logic easier.
-
4:45
So again we're going to start with the switch statement, and
-
4:48
the value that we're going to switch on or the value that we're going to consider.
-
4:53
In this case, the value we want to consider and
-
4:55
match against a bunch of different values, is this random temperature.
-
4:59
We'll say, randomTemperature.
-
5:03
Key statements support matching across a range that we provide.
-
5:08
So, rather than specifying an upper and lower bound for temperature values
-
5:12
using an and operator, we can use ranges to provide a range of values.
-
5:16
So here's what I mean.
-
5:18
For a temperature range of 0 degrees to 32 degrees, that's mind numbingly cold.
-
5:24
So forget clothes, just don't go out.
-
5:26
So, here we'll start with the case statement again but
-
5:29
instead of providing a single value to match against we can provide a range.
-
5:33
So we can say 0..
-
5:36
Or period period and then we will use an angle bracket of 32.
-
5:41
So if you remember what we learned in ranges, this is the half open range
-
5:45
operator which means that our values go from 0 to 1 less than 32.
-
5:49
So 32 is not included, it's the values from 0 to 31.
-
5:52
Now if the temperature falls anywhere between this range,
-
5:57
if random temperature is anywhere between 0 and 32.
-
6:01
Then we're going to print (Forget clothes,
-
6:08
you are basically a popsicle).
-
6:12
Let's add a few more cases.
-
6:14
So in here we'll say case 32, so this range starts at 32 and
-
6:19
goes all the way to 45, including the number 45.
-
6:24
Then we'll print (:It's quite cold.
-
6:29
Oops, syntax error.
-
6:32
It's quite cold.
-
6:35
You'll need a jacket).
-
6:39
In the next case, which will go from 45, all the way to 70.
-
6:46
And you'll notice we have a mistake here, over here we're including the number 45,
-
6:51
and here our range starts at 45.
-
6:52
So what if the value is 45, which one do we match?
-
6:56
We'll fix that by using a half open range operator.
-
6:59
Then we're going to print ( It's a bit chilly.
-
7:05
I recommend wearing a light sweater).
-
7:12
For the last case, let's go from 70 all the way to 100.
-
7:19
And in this case, we'll say print ( It's quite hot!
-
7:23
T-Shirt weather!).
-
7:27
And then if the temperature is higher or lower than this, we'll say default,
-
7:34
(Don't even bother to go out!).
-
7:40
Okay, so now look at that, the randomTemperature generated was 146.
-
7:45
Is 146 between 0 and 32?
-
7:47
No, it's not.
-
7:48
Is it between 32 and 45?
-
7:50
It's not.
-
7:51
We can match on every single case and will return false.
-
7:55
So finally we hit the default statement which prints out,
-
7:58
(Don't even bother to go out!).
-
8:01
Let's hit Enter again.
-
8:05
So that we generate a new temperature and
-
8:07
now the new temperature which is random is 25.
-
8:10
Okay, 25 is between 0 and 32.
-
8:13
So we hit that first case and it says, (Forget clothes,
-
8:15
you're basically a popsicle).
-
8:17
Isn't that awesome, a switch statement can match on more than just a single value.
-
8:23
Hopefully, this glimpse into the flexibility of the switch statement
-
8:27
should show you why their preferred over if statements.
-
8:31
ControlFlow helps us add logic to our code and define various paths for
-
8:35
this logic so that we can take different actions based on a set of conditions.
-
8:40
We've learned a lot of new things here, so
-
8:42
right before we wrap up, I've provided two challenges.
-
8:46
One, is somewhat straightforward so I'll let you tackle that on your own.
-
8:50
The second requires a bit of explanation, and
-
8:53
there are different ways that you can solve it.
-
8:55
So, I've provided a video to explain the problem and
-
8:58
another video with the solution.
-
9:00
In between the two, I trust that you will try your best and
-
9:03
work through the problem yourself.
-
9:05
Have fun.
You need to sign up for Treehouse in order to download course files.
Sign up