Integrating else if conditions
The else if condition enables you to have a follow-up condition if your first condition didn't match. You can have as many else if conditions as you please. The code executes in a linear fashion the first condition that is matched is then the only one that is executed.
Video Transcript
00:00I could see how some people would really hate these
00:03intros and some would love it.
00:05Let's see, more complex if conditions.
00:08Let's continue where we left off and we left off
00:11when we had an if condition and an else condition.
00:14And what I wanna do, I'm just gonna break a line here
00:15after the if here for a second.
00:17Because I wanna add another if,
00:20but I don't wanna just add another if.
00:22Because I don't want this to happen.
00:24And then another something to happen.
00:26I want another something to happen only if
00:29it didn't come true.
00:31So to do that, what I'm gonna do is I'm gonna type an if,
00:34else, and I'm gonna put here a new rule.
00:37So I'm gonna say if my hour is smaller or equal to seven,
00:41and if my hour is smaller or equal to seven,
00:43then I'm gonna execute whatever's here.
00:45And notice how I'm chaining it together.
00:47I don't have to have an else.
00:49I don't have to have an else if,
00:51but I could chain it basically saying I have if.
00:53So okay, so the time now is actually,
00:55if we put here the real time,
00:56the time now is 6.40 in the morning.
00:58So in 6.40 in the morning,
01:00that means that our hour, this wouldn't be true.
01:03We're gonna move into the next one.
01:05We see that our hour is less or equal to seven,
01:07which it's true.
01:08And then I could set here, okay,
01:09I wanna change my color.
01:11And by the way, I could have done here
01:12a lot more than just changing a color.
01:14This is just for the sake of this example.
01:16So we're keeping it a little bit tighter.
01:19Let me just push that back,
01:20because it's always fighting back.
01:22And I'm just gonna put here that other color,
01:24which is we're gonna be a lighter blue,
01:26which is also a dark blue.
01:28But a bit lighter.
01:29So if I just test this out really quickly,
01:31and let me just show you also,
01:32if it was five in the morning,
01:34I'm gonna save this.
01:36Go back into my application here,
01:39click on refresh.
01:41Oh, and I have your little error.
01:43So let me just jump right back in there.
01:45I made a huge mistake.
01:46It's not if, else, it's else, if.
01:51Silly, silly me.
01:53All right, so it's else, if.
01:54And else, if, we'll go back there,
01:57save it, and refresh.
01:58And notice, these mistakes just happen.
02:00And when they happen, you just jump right in there
02:02and see what you did wrong.
02:04And now you'll see that now we're starting
02:05from a really dark blue, to be honest.
02:09That's quite dark.
02:10So let me just go here, and I'm just gonna check,
02:14are we, in which color are we in right now?
02:17So I'm just gonna go ahead and just put here
02:18a really light blue.
02:19So I'm just gonna copy this,
02:21put a really light blue in here for seven in the morning,
02:25and just gonna refresh this one more time.
02:27So we're saying that we're not getting
02:29what we wanted to get, which basically is telling me that,
02:32oh, because we're still stuck in five in the morning.
02:34That's the reason.
02:35All right, so let me just save that.
02:37We'll go right back in there, click on refresh.
02:41And now we're starting from a really light blue.
02:43But really, what I wanted to do is I wanted
02:45for seven to be this color.
02:47And if we're already at it, let me just go ahead
02:49and just copy this else, if, again,
02:51and have another else, if, but have this one
02:53if it's earlier than 10 in the morning,
02:56or 10 or earlier, I'll set this color.
03:00And if our time now is, I'm just gonna copy this,
03:03paste it in again, if our time now is smaller than,
03:07let's say two, which will be 14,
03:11then I'm gonna put the lightest color before we move
03:13to white, which would be everything else.
03:15Now, obviously, in a real world scenario,
03:17we probably wouldn't want everything else to be white.
03:20So how about let's make it more real.
03:22So let's say until daytime is until, let's say,
03:27six in the evening.
03:27So I'm gonna set it to be until six in the evening
03:30where we get completely white.
03:32And one last else will set our starting color
03:37to be completely black.
03:39So I'm gonna go ahead here and just set your 0, 0, 0, 0, 0,
03:431, 2, 3, 4, 5, 6.
04:10And there you go.
04:11Now, what I wanna do before even we move onwards,
04:13what I wanna do is I'm gonna create really quickly
04:15a small little function.
04:19We're gonna call it fade in
04:22and we're gonna put your time.
04:24And what I wanna do, I'm just gonna take this function
04:26and move it into a function because what I really wanna do,
04:29and I'm just gonna copy all this
04:32and just paste it right in here.
04:36And instead of checking to see if our,
04:38or you know what, let me just set here
04:41or call this variable O to hour,
04:43that way we don't have to change anything inside here.
04:46Or we could have just created another variable,
04:48but there's just no reason for us to do that.
04:50So what I'm gonna do now is I'm just gonna fade in hour.
04:56Now, the reason why I'm doing this
04:57is because I want us to test this out in our browser
05:00without needing to go back and forth each time.
05:02So now that it's inside of a function,
05:05I could go ahead and basically just call the fade in
05:09function each time to test it out.
05:11So let's say that we're gonna,
05:12oh, and let me just set it to be the real hour,
05:14oh, it is the real hour, okay.
05:16So I'm gonna go right here into my editor
05:18and I'm just gonna call fade in.
05:20And let's say it's midnight, so I'm gonna send zero.
05:23And in zero, we're gonna get a nice dark color,
05:24which is beautiful.
05:26And if the time now is five in the morning,
05:28we're still gonna be in the dark.
05:30But if the time is seven in the morning,
05:32we're gonna get a bit lighter.
05:34And if it's nine in the morning,
05:36then it's gonna get a little bit lighter.
05:38And if it's 11, we're gonna get even lighter.
05:41And if it's one o'clock, even lighter.
05:45And if it's four p.m.,
05:47we're gonna get a nice white fade in.
05:50All right, so that was just our example
05:51to showcase the power and capabilities of if conditions.
05:57All right, so we're gonna see you in the next lecture
06:00because I wanna talk a little bit more
06:01about if conditions.
06:02I wanna talk with you about something
06:04that is called nested if conditions
06:07or double if conditions.
06:08You don't really, it doesn't have a name necessarily,
06:10but if conditions don't have to be singular,
06:13singular, but they could be actually
06:15something that is nested one inch side of each other.
06:18So see you in the next lecture
06:19when we dive a little bit deeper into if conditions.
