Hey, guys, it's Angela again. 1
Welcome to another episode on Swift Deep Dive. 2
And in this Deep Dive, we're going to cover Swift Switch Statements. 3
Now, if you were a train driver and you were pulling into a large station, say, Paddington or King's Cross 4
and you had to manually check if every single platform was empty or if it was occupied, that would take 5
you a long time. 6
And that's kind of like trying to write an "if else" statement for a whole bunch of conditions. 7
What's way more efficient and much easier is if you could just ask the station manager which platform 8
should I pull into and they give you a number, 9
1, 2, 3 or 4, and you just go to that platform. 10
So this is kind of like how Switch Statements work. 11
We can have a variable, say, if we think about our Egg Timer, we have this variable called hardness which, 12
of course, depends on the button that the user presses on, and we can check to see what the value of that 13
variable is equal to. If it's equal to "Soft," then we could print (5) or the Soft Time, if it's equal to 14
"Medium," then print (7) "Hard," print 12. 15
So, essentially, what's happening here is we're matching the value of this variable to each of these cases 16
and depending on which one matches, then we're going to perform different actions. 17
And finally, at the end, there is a default statement in case none of our cases match the current value, 18
so usually that's where you might print "Error," where you might say, "None of the values match," or "Hey, you 19
might want to check and see what's going on here," or you could use it like an "else" statement, like a catch 20
all, do this in all other cases. 21
Now, the reason why Switch Statements are so great is because not only do they reduce the amount of code 22
you have to type. Instead of having to type if hardness is equal to "Soft," open brackets, close brackets, 23
if hardness is equal to medium etc.. 24
But underlying how a Switch Statement versus how an "if" and "else" statement works, if you have more than 5 25
cases, it's usually more efficient to use a Switch Statement because it performs a search rather than 26
having to manually go through each of the conditions and check them. If you have less than 5 conditions, 27
then it's often quicker and easier to write just some "if" statements. 28
Now, what if you can't match the precise value of a variable? 29
What if you wanted to match a range, say, if hardness was actually a number and we wanted to check if 30
it was between, say, 3 and 5, and then 5 and 7? 31
What would you do in that case? 32
Well, here's where the Swift range operator comes in again. And we saw this already before, we saw that 33
you could use three dots to create a closed range where you're creating a range between "a" and "b." 34
So, for example, we could create a whole bunch of random numbers between 1 and 100, including 1 and 100, 35
then we would use the close range, and then we have the half-open range where we have a less than sign 36
at the end. 37
And this means that we have a range between "a" and "b," but not including "b." 38
And finally, one that you haven't seen yet is something called a one-sided range operator where you leave 39
one of the sides either the "a" side or the "b" side empty. 40
So this means that we're creating a range that is up to and including "b," but it can be any number that's 41
less than "b." 42
So bearing in mind what we've just learned, 43
I want you to challenge yourself and play around with these range operators as well as the Switch Statement 44
by changing our existing loveCalculator that you created using an "if," 45
"else if," "else" statement into one that uses a Switch Statement only. So have a think about what you need 46
to do, maybe rewind and take a look at the range 47
operators 48
if you need to, or Google for the range operator in the Swift Programming Language book and have a read 49
over there. 50
But essentially, the challenge is to simply adapt your loveCalculator 51
that's using an "if" statement into one that uses a Switch Statement. 52
So pause the video and try and give that a go. 53
So here I've got the code from before where we're using our "if," "else if," and "else" statements to check if each 54
of these conditions are true and perform the correct actions. 55
Now, in this case, we're going to change it to a Switch Statement. 56
And, again, I'm going to use the capability of Xcode to provide code snippet by just starting out with 57
writing the keyword which is, of course, "switch," and I'm gonna hit enter, and then insert a code snippet with 58
these placeholders for me to replace. 59
So, in this case, the value that we're checking or we're switching on is, of course, our loveScore. 60
So that goes there. 61
And then the case or the pattern that we're matching is whether if loveScore is greater than 80. 62
So in this case, I'm going to check if the loveScore is greater than 80. 63
So to do that using the close range operator, we would write 81...100. 64
And in that case, the code that should get executed is, of course, this line, printing 65
"You love each other like Kanye loves Kanye." 66
Now, in order to add another case, I'm simply just going to write the word "case," and then I'm going to 67
provide the case pattern that I'm trying to match. 68
So in this case, we'll need to check if the loveScore is greater than 40, and less than or equal to 80. 69
Let's use the half-open range operator this time, so that 70
I'll write 71
41.. 72
less than 81. 73
So in this case, what we should do is to print "You go together like Coke and Mentos." And the last case 74
I'm going to add is if it's between 0 and 40. Let's use the one-sided range operator this time by writing 75
...40. 76
And finally, we're going to print "You'll be forever alone." 77
Now, the default statement should catch if loveScore was ever outside of this range. 78
And because we're creating a random number here between 0 and 100, it's really unlikely that it's ever 79
going to go outside of our three Switch Statements. And if it really doesn't, then we should print that 80
there's an error and love score is out of range. 81
And that's my sample solution to the challenge. 82
Now, your code might be slightly different and you might have used slightly different range operators, 83
but as long as you print the same output as with the "if else" block, then your solution is correct. So 84
here we've created our Switch Statement and it replaces our 85
"if" and "else" statement. Now, in this case, it might seem like we're actually writing a bit more code than before. 86
But if you had a lot of cases, say, 5 or 6 or 7 or more, then it would be a lot more efficient 87
writing it like this, and your code will also run much faster than if you just had a whole bunch of conditions 88
which each need to be checked and confirmed. Instead, 89
this case, we are pattern matching the value of this against one of these cases and it's more performant 90
when there are a lot more conditions. 91
. 92
.