Hey, guys. 1
Welcome to another episode of Swift Deep Dive. 2
And in this lesson, we're going to talk about IF and ELSE statements, 3
also known as conditionals. 4
So the idea here is what if you could check for a condition, and based on whether if that condition is 5
true or false, then you would perform different actions. 6
So in this case, if the green light was on, then the car should go. But if the red light was on, then it 7
should stop. 8
So we can also express this in code 9
like so. 10
So here we're checking if the traffic light is equal to green, then we will perform the action of go, 11
otherwise, namely if this is false, 12
namely if the trafficLight is not green, then we will perform the action of stop. 13
Now, similar to what you saw with functions, curly braces often contain more than one line of code. So 14
more frequently, you'll see this formatted like so. Now, that's all very well and good, but in a lot of 15
cases, especially in code, it's not always just true or false, because here our conditions, whetherif the 16
trafficLight is equal to "green," it can only be true or false, it's either green or not green. 17
So we only have our "if" and our "else" blocks. But in real life as with code, there's often other possibilities. 18
So what do you do, for example, if the traffic light was amber? Then we might have to adjust our code a 19
little bit to take into account this third condition. 20
So I'm not sure about the "Highway Code," but at least when I drive, if it's amber, I just press the gas 21
pedal. And just as long as there's nobody on the road, I just go. Now, don't trust my word, read the "Highway 22
Code." 23
But if we convert this condition to code, then we're saying that if it's green, then definitely go. 24
Else if the traffic load is equal to amber, 25
so we're tagging on another condition here, 26
well, then in that case, use your judgment. And then the final "else" is if all of those are not true, then 27
we're going to stop. In programming, 28
we call these conditional statements because we're performing different actions based on different conditions. 29
So here's a challenge for you. 30
I want you to either open up your previous eyes with playground and delete everything inside or simply 31
just create a new playground. In that playground, 32
I want you to create a function called loveCalculator. Inside the function, generate a random number 33
between 0 and 100, and then store that number inside a constant called loveScore. 34
Now, if the love score happens to be equal to 100, then I want you to print "You love each other like Kanye 35
loves Kanye." 36
Otherwise, I want your code to print "You'll be forever alone." 37
Then finally, call the function so that you get a message showing up in the console. And 99 out of 100, 38
it's probably going to say, "You'll be forever alone." 39
So feel free to modify my messages as you wish. 40
So basically, you'll have two messages and depending on the condition of the love score, it will trigger 41
either one of those messages. 42
So pause the video and try to complete this challenge. 43
All right. 44
So the first thing we're going to do is create a new function which we're going to call loveCalculator. 45
And then inside this function, we're going to create a random number generator. 46
So we're going to generate whole numbers, 47
so they're going to be integers, and then we're going to use that random function that we saw before 48
in order to generate those numbers. 49
Now, if you forgot any of these parts, 50
be sure to just simply do a quick Google, do a quick search on StackOverflow, and you should be able to find 51
these bits of code relatively easily. 52
You don't have to memorize them. 53
And inside are random number generator. 54
We're going to provide a range. 55
So I want to generate numbers between 0 and 100. 56
So now we've got our random number, we have to hold it within a container, 57
right? 58
So we're going to store it inside a constant and we create constants with the "let" keyword 59
and then we're going to name it 60
loveScore, 61
and then we going to set it equal to the right-hand side which is, of course, going to be a random number 62
between 0 and 100. 63
Now that we have our random number loveScore, we're going to use our If statement that we just learned 64
about. 65
Now, if you want to get a starting structure, you can actually simply type "if." And on the left, you've got 66
this little code snippet symbol. 67
Then if we hit Enter, it'll insert our code snippet. 68
So it shows a placeholder for our condition that we're checking, 69
and then finally, for our code that needs to execute if that condition was true. 70
So our condition, in this case, is whether if the loveScore that we generated is equal to 100. 71
Now, notice that there's a big difference between two equal signs and one equals sign. Two equal signs 72
means that we're checking if the left-hand side is equal to the right-hand side. 73
So this entirety is going to be a condition that's going to be true or false. 74
And if you want to see what this is actually going to be equal to, then feel free to try and print it, 75
then you can see that this will be true or false. 76
So this double equals means checking if left-hand side equals right-hand side. A single equals means 77
make the left-hand side equal to the right-hand side. 78
So it's assignment versus checking. 79
Now, we've checked if the loveScores equal to 100. Well, then, in this case, the code that should execute is 80
a print statement that says, "You love each other like Kanye loves Kanye," which is a lot. 81
Now, if they get 100, that's what's going to print. 82
But then we're going to add an "else" statement to trigger 83
if that was not true. 84
So if the loveScore was not equal to 100, then in this condition, we're going to print "You'll be forever 85
alone." 86
Cool. 87
So now the final thing to do is to actually call our function loveCalculator, so that we actually trigger 88
the running of this function. And if we hit play, it will say "You'll be forever alone." because the score 89
that got generated was 23. And you can click play a number of times to see some different scores. 90
Now, it's going to be pretty unlikely that I'll ever be able to reach 100. So that, I'm not going to try. 91
But this is essentially the solution to our challenge. So we've seen the "Is equal to" check, 92
but there's actually a whole bunch of other checks we can make as well, whether if something Is not equal 93
to is an exclamation mark and equal sign, whether something is greater than, or lesser than, greater than 94
or equal, lesser than or equal to. 95
So you can use all of these different comparison operators to compare the left-hand side and the 96
right-hand side. And then the end result is a condition that will still be true or false which determines whether 97
if your "if" statement gets run or if your "else" statement gets run. 98
Here's the second part of our challenge. 99
I want you to modify the loveCalculator, so we'll get our "You love each other like Kanye loves Kanye" message 100
trigger more frequently. And it should print that message 101
if the love score is greater than 80. Otherwise, if the love scores between 40 and 80, then it should print 102
"You go together like Coke and Mentos." 103
And finally, if the loveScore is less than 40, you should print "You'll be forever alone." 104
So go ahead and try to make those modifications to your loveCalculator and complete the challenge 105
now. 106
All right. 107
So all we need to do here is to change our condition here from checking whether if loveScore is equal 108
to 100, to whether if it's greater than 80. And then we're going to add that "else if" which checks for 109
the next condition. 110
So else if loveScore is greater than 40, 111
and in this condition, we're going to print "You go together like Coke and Mentos." Cool. 112
So now finally, there is the final "else" statement which will catch everything else and including if the 113
loveScore was not greater than 40, and not greater than 80. So this is the solution to our challenge. 114
Now, if you're wondering how our code actually flows, then let's think about a couple of example numbers 115
that we might generate. 116
Let's say that we generate 55. 117
Well, the first condition checks if loveScore is greater than 80, 118
so that's going to be false. 119
So we're going to skip that one. And then the next condition to check is going to be if the loveScore 120
is greater than 40, which is true, 55 is greater than 40. 121
So it's going to do this block here. And then once it's done, it's going to complete, 122
and that's going to be the end of the running of our function. 123
Now, let's say that we had a higher score that we generated. Say, we generated an 85. 124
Well, in this case, loveScore is indeed greater than 80. 125
So "A," this block will trigger, and then it's going to skip checking all of the other conditions because 126
these conditions have an "else" in front of it. 127
So it reads kind of like English. Well, if loveScore is greater than 80, then do A, else, 128
so namely if this was false, then check this, else, do this. 129
So none of these "else" statements will trigger because the original "if" statement is already true and has 130
already been triggered. 131
So here's a bit of a brain teaser. 132
What if instead of having an "else if" here, I replaced it with an "if"? 133
What do you think would happen in this case? 134
Pause the video and have a think about it. 135
And if you want to see the solution, just change your code in your playground, and you'll be able to check 136
whether if you managed to predict the outcome correctly. The difference between the "else if" and the "if" 137
statement is that, in this case, we're going to check if the loveScore is greater than 80, which it is, 138
so it's going to do "A." But then because there is no "else" that's coming up, it's going to check the next 139
condition which is whether if loveScore is greater than 40, and 85 is indeed greater than 40. 140
So it's gonna do "B" as well before it retires. 141
So in this case, because we don't have the "else if," it means that it's going to check all of the conditions 142
with an "if." 143
But if you have an "else if," it means it'll skip it as long as the previous one or one of the previous ones 144
were true. 145
Now, the final thing that I want to show you is that you can also use these combinations where you can 146
combine these checks so that you can check if two conditions are both true or if one of the conditions 147
are true or, finally, if something is not true. 148
For example, in terms of our loveCalculator, we could also write the code like so, right? 149
We could say that if loveScore is greater than 80, then do A. 150
And then we could use an "if" statement that checks if loveScore is greater than 40 and it's less than 151
or equal to 80. 152
So between 40 and 80, well, in this case, it's going to do B. 153
So now even though we still have two "if" statements like before, this time it's actually only going to 154
execute one of these blocks, because even if the first one was true and it's going to do "A," it's going 155
to continue and check the next one. But the next one is actually not true anymore because loveScore 156
85 is indeed greater than 40. But this condition also requires that loveScore must be less 157
than or equal to 80. Because one of these is not true, 158
therefore, this condition overall is going to be false. And you can have a go at changing the conditions 159
in your playground to use the "and" or the "or," and the "not" to see how each of these work. 160
So now that you've learned all about the Swift conditional statement, the "if," the "else," and "else if," then 161
it's time to complete the IF/ELSE assignment. 162
.