Switching cases
Throughout our course, we talked about Boolean values and how they're treated as flags. We've approached if conditions and seen them in action. In this lecture, we're going to dive into the world of switches. Switches are very similar to if conditions - they just have different rules. Contrary to if conditions that each condition is set separately, switches only look at one variable, one subject. They're not used as commonly as if conditions, yet within they a lot of useful features.
Video Transcript
00:00This isn't exactly something new but it's a new way of doing the same thing in the last lecture
00:05We saw how to work with if conditions to create this kind of like
00:10sophisticated
00:11Fade in based on the time of the day. We're gonna recreate the same thing only using saying it's called switches
00:17So let's see what that means to switch gears and see how to do the same thing using the switch command
00:22Switch command instead of this. So what I'm gonna do is I'm just gonna copy this fade in
00:27Function and I'm just gonna have an empty one here and I'll just call it fade in switch
00:32I don't even have to tell you that you're now allowed to have the same function with the same name because that's something that we
00:36Already know. All right
00:38So we have your fade and switch the way we create switches is a little bit different than if conditions and if conditions we had
00:44our condition inside of our if in
00:47Switches we basically only have one variable that we're working with one expression. So the way we use it
00:53We type the word switch and then we put an open around bracket close around bracket and we put our variable in
01:01Right in there open a curly bracket close a pearly grant curly bracket. All of our conditions are gonna be set to this
01:07We're basically doing a equal equal to what an hour by creating cases. So here we go. Let's do case zero
01:15All right, and in case zero and case and case one
01:21case two case three
01:25Case four we all want to treat all those cases the same way
01:28so literally all we have to do is just type all those cases and now we could just copy our
01:33We also have case five and then I could just paste in the same value that we change there to that variable
01:43All I have to do now is break out of it
01:47Now once I break out of break out of the cases
01:50It's not going to continue doing other cases if you don't use break contrary to if conditions once
01:56Once your rule is matched. It will continue running all the code from that point onwards
02:01So literally if hours equals five it's going to jump to this line of code and just to execute everything until it sees a break
02:08So in our case, we want to say break because we don't want it to continue further and let's do case for six
02:46Let's see. It was like six
02:48We had a exactly six, right?
02:50so I'm gonna do a case for exactly six and just set here the value right here and
02:57Notice I'm not putting a break on six because I want to continue I
03:01Want to continue down to seven because the changed color I'm doing if it's six or seven
03:06So I'm gonna go here on case seven and then seven
03:09I'm just gonna put inside the changed color that was the lighter blue and
03:14Then I'm gonna put a break
03:16Now this is a perfect example is because if it was six in the morning, it's gonna first trigger this animation
03:22Then it's gonna trigger the of seven because it's not gonna jump out
03:26Once it found a value that it was equal to is going to continue until it sees a break
03:30Okay, so that's gonna solve six and seven. Let's see what's in the next one. The next one is gonna be
03:37Eight nine ten. So let's do eight nine ten right here. I'm just gonna copy this really quickly
03:48Well, when I say quickly, I mean really slowly
03:51Okay, perfect. So that was eight
03:54Nine ten and for all of them we want to do the same type of thing only with a different color
04:01Let me just find that color where was it right here
04:05We're gonna copy that change that. All right, let's see what's next. So we were at 10 now we have your
04:1211
04:1411 12 13 14 and
04:1712 now this is gonna be our problem right because we have your this we won't be able to do in the same structure because
04:24What we want to do and again, I'm just gonna put all those in
04:28This is a really great example because it showcases the diversity and the limits of each one of these methods of doing things
04:35So first of all, we have your 10 through 14 and let's see then how do we get it to work?
04:41Where only if it's 12 o'clock, we're doing something specific and if not, we're not
04:46So again, we're gonna go right in your first of all
04:48We'll change the color because we change the color and all them in that time anyways
04:52And that was this color we go right in there set that color
04:56Okay, and now our problem is we need to set this tween to happen only if it's 12 o'clock
05:01But our problem is is that if we set it right here, then oh, so if it's 10 or 11
05:07It's gonna do it anyways, so we can't put it here. What we could do is
05:12Reverse our order we could literally instead of that instead of creating an if here. We're basically gonna say hey if it's 12
05:20We're gonna put 12 up as an earlier case
05:24Because if we're 12 we're gonna do this tween and if it's after 12 or any other value
05:31It's gonna go through the regulars hoops. So if it's 10 11 13 or 14
05:36it's only gonna change the color, but if it's 12, it's gonna first change that color and
05:41It's gonna change the opacity and then it's gonna change the colors. So let's just finish this one up and we're getting close
05:47We have your uh, if it's uh until 6 o'clock, so let me just
05:51Create the cases for until 6 o'clock
05:57Okay, so we have your 15
06:0016 17 and
06:02Last but not least is say team. This was really fun to copy paste so much. All right, we're gonna copy this
06:10paste that there and
06:11All that's left is to change the actual color which we're gonna find right here, which was completely white and
06:18We're almost almost done. We just have one more case left
06:21Which is if it's anything but all the stuff that we already mentioned and in that case, we're gonna create here a default
06:28sorry for the garbage truck in the background if you could hear it and
06:35Sorry, I hear someone making noise another one which drives me crazy when people make noise
06:40okay, so now we could set the default value here as
06:44this and basically what if we just run through this really quickly is
06:47We have a switch
06:49We put our variable in and then it's gonna match to whatever the time is at that point basically finding the value in the cases
06:56Once it finds it it's gonna run forever until it hits a break
06:59So if it's two in the morning, it's gonna start from here and it's gonna do everything that's gonna happen from here all the way
07:05Until the break that way we saw that in the case with six and seven where we put our six where if it was six
07:11It also did our tween and only then changed our color now same was true
07:16We saw in the case with our twelve where we pushed it up earlier in the queue because we wanted that to happen
07:21But we didn't want anything else to have that so we put it before everything else
07:26Last but not least was the default case and this is gonna happen no matter what always if there was no
07:33Value or if there was no break
07:34so if I didn't have your a break and it was six o'clock then would do it would do everything from
07:3915 it would do anything that was 15 16 17 18 it would do this and it would do this as well
07:45So it would override it. So that wouldn't be too good for us
07:48All that's left for us to do is to call that tween that we had on the bottom here and just put that up here
07:53Underneath here as well, and that's our switch case. All right, so i'm not going to talk much further about switches
07:59It's very similar to if statements a little bit different the construct of it
08:04But do it the biggest difference is we're only doing one variable if conditions are a lot more rich
08:10But these become really really useful in certain situations
08:14So without further ado in the next lesson
08:17We're going to go back into if conditions because we want to also talk about what if we want to compare
08:21More than one thing but make sure that you look into the source files and play around with switches and if cases
08:27You know exactly the difference between the two. Oh, by the way
08:30And if I just save this and let's just test it out to see that it actually works
08:34And we go back in there save that go into our browser all that work without actually seeing it
08:39We'll see that everything is working
08:41And if I go right in there
08:43And i'll try to do nine in the morning this time around we'll see that the colors are getting lighter
08:48If I do two we'll see that now it's going to be even lighter if I do 12
08:53Which I think was the color where our item will animate a little bit later in which is exactly what's happening
08:58and if it's let's say seven o'clock, then we should have a dark color, but if it's
09:06Four four four four o'clock then we're going to start from white. So there we go in the next lesson
09:10We're going to go back to if statements because we want to see more complicated if structures
09:15And really the main topic of next lecture is going to be operate operators in general. So see you in the next lesson
