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