1 00:00:00,680 --> 00:00:06,320 Welcome back, round lesson nine, if else statements, so I showed you a little sneak peek of these 2 00:00:06,320 --> 00:00:12,710 in the last video, and you're actually going to be using these types of condition checks, lots and 3 00:00:12,710 --> 00:00:16,520 lots in JavaScript, and it's what's called control flow. 4 00:00:17,390 --> 00:00:24,410 So control flows when you're checking if one thing is true, then cool, execute one set of code. 5 00:00:25,250 --> 00:00:29,480 And if it's not true, then execute a different set of code. 6 00:00:30,460 --> 00:00:37,570 So you can use multiple checks, you can say if the is it just Cascade's like a waterfall, it's going 7 00:00:37,570 --> 00:00:38,440 to check this one. 8 00:00:38,440 --> 00:00:40,270 If that's false, it'll check that one. 9 00:00:40,270 --> 00:00:41,830 If that's false, it'll check that one. 10 00:00:41,830 --> 00:00:44,590 If that's false, it'll check everything else. 11 00:00:44,590 --> 00:00:50,650 Basically, it'll just go to the final set value so you can always set these up. 12 00:00:50,660 --> 00:00:56,410 So there is no code that gets run if you just choose to do an if statement or just if and else if that, 13 00:00:56,410 --> 00:00:58,830 it won't actually execute the else. 14 00:00:58,840 --> 00:01:04,030 But if you have the else in there, it will always execute this if everything else is false. 15 00:01:04,600 --> 00:01:10,240 OK, so the else is when it does not meet the condition else if is when you want to check for a second 16 00:01:10,240 --> 00:01:12,040 or third or fourth or etc condition. 17 00:01:12,550 --> 00:01:15,130 OK, so let's take a look and play with this. 18 00:01:16,270 --> 00:01:19,450 So let's just set an initial value here. 19 00:01:20,560 --> 00:01:27,310 Let's just do a pet name of Fido, OK? 20 00:01:28,810 --> 00:01:31,030 And let's give this mood. 21 00:01:32,230 --> 00:01:36,910 Let's give our Fido a mood of happy. 22 00:01:37,340 --> 00:01:37,840 Great. 23 00:01:38,530 --> 00:01:39,640 Fido was very happy. 24 00:01:40,560 --> 00:01:43,450 That is a permanent mood because we use the concept. 25 00:01:44,440 --> 00:01:48,040 So let's go ahead and check to see what the mood is. 26 00:01:51,250 --> 00:02:00,610 Is the mood exactly this string right here, these five characters in succession and nothing else? 27 00:02:01,810 --> 00:02:04,180 If so, then let's council log. 28 00:02:06,430 --> 00:02:07,600 Let's council log. 29 00:02:11,080 --> 00:02:12,070 That name 30 00:02:15,100 --> 00:02:16,480 is a 31 00:02:20,740 --> 00:02:21,400 mood. 32 00:02:22,240 --> 00:02:26,150 His pet name is totally a mood dog. 33 00:02:26,540 --> 00:02:26,980 You. 34 00:02:30,900 --> 00:02:33,040 All right, let's try that out. 35 00:02:33,870 --> 00:02:37,650 So if we just do the if by itself, then it's going to check it. 36 00:02:37,650 --> 00:02:42,900 If it's true, it'll execute this line of code so you can just use it if it's just by themselves, nothing 37 00:02:42,900 --> 00:02:43,190 else. 38 00:02:43,860 --> 00:02:48,170 Whatever you want to just say, make a certain line of code conditional, totally cool. 39 00:02:48,870 --> 00:02:50,400 And there are other ways to do that as well. 40 00:02:53,660 --> 00:03:01,850 OK, but what if we gave this and else condition the U.S., if that if this value up here wasn't true, 41 00:03:02,540 --> 00:03:04,750 then let's just say something else. 42 00:03:04,760 --> 00:03:05,510 Let's just say 43 00:03:09,590 --> 00:03:10,910 let's see. 44 00:03:15,330 --> 00:03:18,930 Will hopefully be happy soon. 45 00:03:22,080 --> 00:03:29,250 OK, so that's totally fine and obviously this is still true. 46 00:03:30,030 --> 00:03:35,540 But if we change this too sad or poor Drogo. 47 00:03:36,570 --> 00:03:38,130 Fido will hopefully be happy soon. 48 00:03:38,160 --> 00:03:40,320 Well, not with this constant one. 49 00:03:40,980 --> 00:03:44,000 Let's change that to a and maybe we can change it. 50 00:03:46,320 --> 00:03:58,020 Now, let's give this another value, let's give it a value of what is hungry, oops, is hungry, equal, 51 00:03:58,020 --> 00:03:58,470 true. 52 00:03:58,870 --> 00:04:01,600 OK, so this dog is hungry. 53 00:04:02,400 --> 00:04:03,540 Let's check if it's hungry. 54 00:04:04,020 --> 00:04:13,740 So it's just sometimes easier just to copy and paste or actually not not hungry as a as a string. 55 00:04:13,760 --> 00:04:16,710 Let's just all we have to check is he's hungry. 56 00:04:16,920 --> 00:04:18,660 Remember Truthy and falsey values. 57 00:04:19,830 --> 00:04:23,950 We don't have to do with truthiness obviously, because the value here is explicitly true. 58 00:04:25,080 --> 00:04:28,710 So if that is true, then console log. 59 00:04:30,640 --> 00:04:33,720 Let's say is hungry. 60 00:04:35,650 --> 00:04:41,350 OK, else will hopefully eat soon. 61 00:04:42,990 --> 00:04:43,430 OK. 62 00:04:44,850 --> 00:04:47,760 All right, so if it's not angry and hopefully it's harmless, you see? 63 00:04:48,740 --> 00:04:50,120 Fido's Hungaria. 64 00:04:51,500 --> 00:04:52,670 If you make that false. 65 00:04:55,410 --> 00:05:03,420 Hopefully to see so depending on the conditional expression, if this conditional expression is true, 66 00:05:03,540 --> 00:05:08,250 the one inside these parentheses, then it will execute this line of code. 67 00:05:08,940 --> 00:05:10,620 So is is Hungary true? 68 00:05:11,550 --> 00:05:12,030 No. 69 00:05:12,540 --> 00:05:13,890 Is Hungary is false. 70 00:05:15,560 --> 00:05:16,730 Is the mood happy? 71 00:05:17,880 --> 00:05:19,280 No, the mood is sad. 72 00:05:19,590 --> 00:05:26,760 OK, so if this doesn't trigger, then it will move on to the condition if one exists. 73 00:05:28,890 --> 00:05:38,010 Let's look at another example to let's to weight equals forty five. 74 00:05:39,960 --> 00:05:44,850 And let's do you know what this do to animals Fido's wait 75 00:05:50,670 --> 00:05:54,110 here and then let's do whiskers. 76 00:05:54,690 --> 00:05:55,020 We. 77 00:05:58,890 --> 00:06:10,250 It's the cat's whiskers, whiskers, and let's say, yeah, that's a good number, so we can use comparison 78 00:06:10,250 --> 00:06:17,770 operators like we had in one of the previous lessons to see if one is bigger than the other. 79 00:06:19,160 --> 00:06:23,210 So let's do Fido wait greater than. 80 00:06:27,140 --> 00:06:31,730 Keep that in is greater than Whiskas, wait. 81 00:06:35,740 --> 00:06:40,120 So if that's the case, then we can consider log. 82 00:06:45,160 --> 00:06:46,660 Fido weighs more, 83 00:06:49,180 --> 00:06:52,900 weighs more than whiskers. 84 00:06:56,920 --> 00:07:04,840 And then we could do else if then we could check for similar condition, but this time we're going to 85 00:07:04,840 --> 00:07:08,310 check for if they're exactly the same weight, OK? 86 00:07:09,970 --> 00:07:11,530 So if they have the same weight. 87 00:07:13,570 --> 00:07:17,290 Then to another value in say. 88 00:07:19,210 --> 00:07:20,230 They wear the same. 89 00:07:25,170 --> 00:07:27,620 Fido and Whiskers weigh the same amount, 90 00:07:32,550 --> 00:07:38,900 we see this fine and then we'll have an LST condition. 91 00:07:40,740 --> 00:07:48,480 So logically, if Fido doesn't weigh more than whiskers and they don't weigh the same, then what is 92 00:07:48,480 --> 00:07:51,540 the only possible other condition? 93 00:07:52,470 --> 00:08:00,240 The only other logical condition that that could happen, assuming obviously these both exist, which 94 00:08:00,240 --> 00:08:04,240 in our case they do, is that Whiskers weighs more than Fido. 95 00:08:04,790 --> 00:08:12,770 OK, so what if Fido lost a ton of weight and then Whiskers gained a ton of weight? 96 00:08:13,920 --> 00:08:19,110 If that were to happen, then whiskers would weigh more than Fido. 97 00:08:20,200 --> 00:08:25,140 If we change your values back then, Fido is more than whiskers. 98 00:08:25,990 --> 00:08:28,740 For some reason, we've got a real chunky whiskers. 99 00:08:30,080 --> 00:08:31,160 Then they would weigh the same. 100 00:08:32,010 --> 00:08:41,630 OK, so there you go, using comparison operators, if else, if and else to do comparisons and decide 101 00:08:41,630 --> 00:08:43,820 which line of code we want to. 102 00:08:44,960 --> 00:08:45,950 Well, we'll find out. 103 00:08:45,950 --> 00:08:48,770 It'll tell us, hey, this line of code is the one that's true. 104 00:08:49,550 --> 00:08:52,010 This line of code is the only one that's true, et cetera. 105 00:08:52,470 --> 00:08:57,350 OK, so that is, if else comparison operators and JavaScript control flow. 106 00:08:57,350 --> 00:09:02,720 And we're going to learn more about control flow with other ways of comparing between multiple things 107 00:09:03,110 --> 00:09:03,860 in another video. 108 00:09:04,070 --> 00:09:04,400 Thank you.