1 00:00:00,210 --> 00:00:06,689 Welcome back in this video, we are going to use if statements and we are going to finally make use 2 00:00:06,689 --> 00:00:10,950 of what we've learned so far, for example, the comparison operators, which are very useful when it 3 00:00:10,950 --> 00:00:12,540 comes to if statements. 4 00:00:12,750 --> 00:00:17,130 So we at this comparison operator, we said five, equal, equal three. 5 00:00:17,370 --> 00:00:21,150 And then if that was true, we can now go ahead and execute something. 6 00:00:21,570 --> 00:00:25,440 So now let's go ahead and create an if statement here. 7 00:00:25,800 --> 00:00:31,650 And before we do so, I'm going to create a little variable called height person one. 8 00:00:32,189 --> 00:00:36,390 And it's going to be something like 170. 9 00:00:36,990 --> 00:00:37,560 And then. 10 00:00:38,560 --> 00:00:42,900 Maybe I should be written correctly, and then I have another one height. 11 00:00:43,180 --> 00:00:47,710 Person two should be something like one hundred and. 12 00:00:48,760 --> 00:00:49,430 Eighty nine. 13 00:00:49,710 --> 00:00:51,360 All right, so these are centimeters. 14 00:00:51,750 --> 00:00:57,780 For those of you who aren't aware of what I'm talking about, so one hundred and seventy centimeters 15 00:00:57,780 --> 00:01:01,950 and one hundred eighty nine centimeters, I think that's something like five. 16 00:01:03,270 --> 00:01:05,760 Seven or five, eight or something like that. 17 00:01:05,940 --> 00:01:11,580 And then this should be, um, six three in terms of feet and inches. 18 00:01:12,060 --> 00:01:12,360 All right. 19 00:01:12,360 --> 00:01:16,120 So these are the two height person, height person one and height person two. 20 00:01:16,140 --> 00:01:22,560 So we have two people and we want to execute something based on a condition. 21 00:01:22,560 --> 00:01:26,370 So if person one is taller than person to. 22 00:01:27,290 --> 00:01:30,590 Then we're going to use raw force in order to beat it. 23 00:01:30,650 --> 00:01:36,620 Let's say we have a fight between two people and now we have to decide what kind of technique we want 24 00:01:36,650 --> 00:01:37,300 him to use. 25 00:01:37,310 --> 00:01:43,460 So let's say our fighter is 170 and the opponent is 189. 26 00:01:43,460 --> 00:01:44,570 So what shall we do? 27 00:01:44,930 --> 00:01:49,820 Shall we either use raw force or shall we use technique? 28 00:01:50,510 --> 00:01:57,710 Of course, a combination of both will be useful, but then we're going to check if height person one 29 00:01:58,280 --> 00:02:01,070 is greater than height. 30 00:02:01,220 --> 00:02:09,289 Person two, I simply want to print something like use raw force. 31 00:02:11,000 --> 00:02:18,140 All right, so let's say let's assume that if someone is taller than he's also going to be stronger 32 00:02:18,140 --> 00:02:24,020 because he's going to have more weight and that is going to be very useful in a one on one fight. 33 00:02:24,770 --> 00:02:25,370 But now. 34 00:02:26,430 --> 00:02:30,480 If we execute this line of Coke, we will see that pretty much nothing will happen because the condition 35 00:02:30,480 --> 00:02:33,060 isn't met, so we have this condition of hide person. 36 00:02:33,060 --> 00:02:34,930 One shall be created in high person two. 37 00:02:34,950 --> 00:02:41,490 But if you look at the values, we will see that that is not true because 170 isn't higher than 189. 38 00:02:41,490 --> 00:02:43,950 So we can see we get nothing here printed. 39 00:02:44,490 --> 00:02:47,280 So now let's say the other way around. 40 00:02:47,700 --> 00:02:52,800 What shall happen if I'd person one is less than high person two? 41 00:02:53,460 --> 00:02:57,150 Well, that's something that we can use with an else statement. 42 00:02:57,640 --> 00:02:58,080 OK. 43 00:02:58,110 --> 00:03:01,060 So in this case, it's either if or else. 44 00:03:01,350 --> 00:03:06,990 So if this condition is met, then execute the code that is within those curly brackets. 45 00:03:07,560 --> 00:03:13,050 And if the condition is not met, then run the else block, which means run the cult, which is within 46 00:03:13,050 --> 00:03:14,310 those curly brackets. 47 00:03:14,640 --> 00:03:19,170 So here I'm just going to say use technique. 48 00:03:20,070 --> 00:03:20,520 All right. 49 00:03:20,550 --> 00:03:25,560 So now let's run this again and see if anything will be printed whatsoever. 50 00:03:26,560 --> 00:03:28,400 And we can see it says use technique. 51 00:03:29,080 --> 00:03:35,920 So because our personal one was smaller than the other one, we advise him to use his best techniques 52 00:03:35,920 --> 00:03:42,610 instead of trying to apply real force when fighting one on one because we're one hell of a coach and 53 00:03:42,610 --> 00:03:44,650 want only the best for our fighter. 54 00:03:45,900 --> 00:03:49,530 But what shall happen if they are both the same size? 55 00:03:49,560 --> 00:03:53,730 So let's say we have the situation where both are actually the same size. 56 00:03:54,000 --> 00:03:59,850 Is there any special advice that we can give our fighter, even though in this example, it's rather 57 00:04:00,270 --> 00:04:02,220 weird to have such a situation? 58 00:04:02,490 --> 00:04:05,130 But let's just say we have this other. 59 00:04:06,710 --> 00:04:07,310 Condition. 60 00:04:07,580 --> 00:04:14,380 So what you can do is you can have an else, if so, there are three different types of checks. 61 00:04:14,390 --> 00:04:16,790 So the if check, then the elusive check. 62 00:04:16,790 --> 00:04:22,790 And then finally the LS, which is not really a check, but which is a code that will be executed as 63 00:04:22,790 --> 00:04:25,040 a default if none of the above are true. 64 00:04:25,340 --> 00:04:26,870 So what does that even mean? 65 00:04:27,170 --> 00:04:31,360 Well, let's say we have an LS if in between here. 66 00:04:31,670 --> 00:04:34,220 So me show you. 67 00:04:34,370 --> 00:04:37,610 So we have an else, if again, with a condition. 68 00:04:37,940 --> 00:04:41,990 So here I'm going to say that those two have the same height. 69 00:04:42,230 --> 00:04:44,240 So equal, equal. 70 00:04:45,020 --> 00:04:45,500 All right. 71 00:04:45,920 --> 00:04:50,630 So we had this first condition here, which was high person, one greater than hot person, too. 72 00:04:50,900 --> 00:04:54,740 Then we had the second condition here, which was high person. 73 00:04:54,740 --> 00:05:02,960 One is equal to high person two when it comes to his size, and then we can execute some code if that 74 00:05:02,960 --> 00:05:04,670 condition is met in here. 75 00:05:05,180 --> 00:05:16,370 So let's just say something like use your power technique one three three seven, something like that. 76 00:05:16,370 --> 00:05:22,010 So the fighter has this power technique, and it's very useful against people who are the same height 77 00:05:22,010 --> 00:05:22,550 as he is. 78 00:05:23,090 --> 00:05:24,800 So you should use that technique. 79 00:05:25,580 --> 00:05:29,530 But then we have this else block, which will be executed if none of the above are true. 80 00:05:29,540 --> 00:05:36,290 So if neither this if statement or this condition here is true nor this condition here is true, then 81 00:05:36,500 --> 00:05:38,330 else block will be executed. 82 00:05:38,720 --> 00:05:43,940 So then the text use technique will be executed instead. 83 00:05:45,500 --> 00:05:53,360 Now, the cool thing is you can have as many lives as you want, so we could go ahead and check if the 84 00:05:53,360 --> 00:06:00,590 height of person one is greater than 150 and execute some coat, then check if what what should happen 85 00:06:00,590 --> 00:06:08,720 if the person is smaller than 140 or all kinds of combinations that we can think of, even though we 86 00:06:08,720 --> 00:06:14,870 have to be careful that certain conditions don't overlap with each other because if they overlap, then 87 00:06:14,870 --> 00:06:16,130 one of them will be useless. 88 00:06:16,850 --> 00:06:23,690 OK, so what we have here, if we execute this, let's say when the person's more personal one in person 89 00:06:23,690 --> 00:06:29,540 two are both 170, then it should display use your power technique one three three seven. 90 00:06:30,320 --> 00:06:33,290 So that we are, it says use your power technique one three three seven. 91 00:06:33,560 --> 00:06:41,270 So let's say want the person to is smaller than the person one, then it should say use. 92 00:06:43,310 --> 00:06:43,730 Air Force. 93 00:06:46,100 --> 00:06:48,440 So that we are use your force. 94 00:06:50,560 --> 00:06:51,130 All right. 95 00:06:51,190 --> 00:06:52,310 And what you see here. 96 00:06:52,330 --> 00:06:59,800 So this whole, if elusive of stuff is a control flow and we're using if statements here now a little 97 00:06:59,800 --> 00:07:00,670 challenge for you. 98 00:07:00,970 --> 00:07:08,830 Please go ahead and create an if else, if else, if else block where the first age check will be at 99 00:07:08,830 --> 00:07:09,940 greater equal. 100 00:07:09,940 --> 00:07:10,620 21. 101 00:07:11,080 --> 00:07:16,660 And if that's the case, then it should write something like or print something like you may drink in 102 00:07:16,660 --> 00:07:19,280 the US, then LS. 103 00:07:19,330 --> 00:07:25,120 If the age is greater equal than 18, then you may vote. 104 00:07:26,110 --> 00:07:30,340 And if somebody is older than 16, you may drive. 105 00:07:30,840 --> 00:07:36,200 And then finally, if none of the above is true, then it should print something like you were too young. 106 00:07:36,370 --> 00:07:43,330 So try to use this kind of structure to print those different statements based on a variable called 107 00:07:43,330 --> 00:07:45,520 age that you, of course, also need to create. 108 00:07:46,270 --> 00:07:47,620 So go ahead and try that. 109 00:07:49,440 --> 00:07:49,790 All right. 110 00:07:49,860 --> 00:07:53,280 So I hope you pass the video, so I'm going to create this variable called H. 111 00:07:53,670 --> 00:07:57,510 And actually, I'm not going to change it so I could go ahead and use a vowel here. 112 00:07:57,960 --> 00:08:02,940 So the variable will be h and then I use an if statement here. 113 00:08:03,990 --> 00:08:07,560 If age was greater equal 21. 114 00:08:08,830 --> 00:08:18,610 Then I want to print something saying, now you may drink in the US because in other countries, you 115 00:08:18,610 --> 00:08:19,870 may drink before that, right? 116 00:08:20,470 --> 00:08:26,380 If you do that, then in our case, of course, this will not be printed because age is not greater 117 00:08:26,380 --> 00:08:27,550 than 21. 118 00:08:28,190 --> 00:08:30,670 Now let's have another check. 119 00:08:30,820 --> 00:08:35,289 So else, if the age is greater, equal. 120 00:08:37,059 --> 00:08:37,630 18. 121 00:08:38,600 --> 00:08:40,850 So what should be printed if that's the case? 122 00:08:41,570 --> 00:08:45,160 Well, let's print something like you may vote now. 123 00:08:46,650 --> 00:08:55,170 All right, Prindle, and you may vote no, and then we have another check. 124 00:08:55,460 --> 00:09:05,070 Age creator equal 16 And then in the US, you may drive so poor and Dylan, you may drive now. 125 00:09:05,520 --> 00:09:10,350 Of course, he knew the license for that, but at least you can make the license or do the license at 126 00:09:10,350 --> 00:09:12,840 this point and then you can use it. 127 00:09:13,320 --> 00:09:17,970 And then finally, we have an LS block, which will be executed if none of the above is true, which 128 00:09:17,970 --> 00:09:19,740 means if the person is. 129 00:09:21,060 --> 00:09:24,660 15 or younger than it should say something like? 130 00:09:25,990 --> 00:09:30,280 You're too young to do whatever. 131 00:09:30,400 --> 00:09:30,760 All right. 132 00:09:31,300 --> 00:09:35,020 So if we run this, you probably already guessed it, it will print. 133 00:09:35,020 --> 00:09:38,050 You may drive now because our age is 17. 134 00:09:39,330 --> 00:09:42,850 All right, so let's run this, and there we are. 135 00:09:42,870 --> 00:09:45,330 So it says you may drive now. 136 00:09:47,030 --> 00:09:50,540 Now, if the age is, let's say, 31 as I am. 137 00:09:51,870 --> 00:09:55,680 You can see it says now you may drink in the US. 138 00:09:57,040 --> 00:10:02,370 All right, so of course, I can do all of the others information as well, so I also may vote. 139 00:10:02,380 --> 00:10:04,270 I also may drive now and so forth. 140 00:10:04,630 --> 00:10:08,110 Of course, I could adjust the print statements here accordingly. 141 00:10:08,470 --> 00:10:13,930 But this is the latest thing, so to speak, that I can do due to my age. 142 00:10:13,960 --> 00:10:14,350 All right. 143 00:10:14,950 --> 00:10:22,090 So we have this, if else, if else blocked, that we can use in order to execute stuff when certain 144 00:10:22,090 --> 00:10:23,140 conditions are met. 145 00:10:23,740 --> 00:10:29,020 Now, by the way, a thing that you can also do if you have a very basic if statement. 146 00:10:29,020 --> 00:10:37,990 So let's say you only have one line of code that follows a condition, so age is greater equal. 147 00:10:37,990 --> 00:10:38,500 30. 148 00:10:40,630 --> 00:10:47,470 Then you can just go ahead and do or add the text in the next line, you're over 30. 149 00:10:49,410 --> 00:10:53,790 All right, so that's something that you can, of course, also do directly. 150 00:10:54,600 --> 00:10:59,310 So just one line of code without having these extra curly brackets around it. 151 00:10:59,580 --> 00:11:02,460 So if you see anything like that, that's what it does. 152 00:11:02,790 --> 00:11:05,340 But you have to be careful because in the next line. 153 00:11:05,790 --> 00:11:10,080 So if I add something like this in the next line? 154 00:11:11,220 --> 00:11:15,720 This line will not be part of this if statement, so it will be executed either way. 155 00:11:16,830 --> 00:11:18,210 So be careful here. 156 00:11:19,700 --> 00:11:20,060 All right. 157 00:11:20,120 --> 00:11:25,730 So these are the if statements and in the next video, we're going to look at when. 158 00:11:26,120 --> 00:11:28,940 Because you might see already that it's complaining. 159 00:11:28,940 --> 00:11:32,630 So it says Cascade, if should be replaced with when. 160 00:11:33,170 --> 00:11:34,830 So let's do that in the next video. 161 00:11:34,850 --> 00:11:35,510 So see you there.