02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Switches

We are done talking about operators and if statements its time for us to explore switches. Switches are in many ways more primitive if statements we can not add operators to them but instead we list out detailed of one possible comparison of one variable. The switch statement can be replaced by performing many if-then-else-if conditions, but for the most part its easier to organize when we are only comparing the value of one closed list amount of data. for more on this let's explore the switch options in this title.

Video Transcript

00:00All right, back to our sample, we're
00:01done talking about if statements for this exercise,
00:05and we're done talking about operators.
00:07And I wanted to bring in one more topic, which
00:09is called switches.
00:10Now, what are switches?
00:11Switches, in a nutshell, are if statements
00:15where you have only one variable and you have only one value
00:18that you're trying to test it to.
00:19So let's say, for example, in our scenario,
00:21we just want to add another switch for the case inside of
00:24here that we're using inside of this if statement.
00:28Basically, I'm not going to use this internal if that's
00:32inside of the first if we're not going
00:33to use the nested if statement.
00:36Instead of using a nested, it's like an airport here.
00:41Another plane is passing, so sorry about that.
00:43It's like I live next to the water, next to an airport,
00:46and next to the summer just started,
00:48so a lot of people are jumping around in the water.
00:50So I'm not going to stop recording each time.
00:52So let's just pretend that you don't hear airplanes
00:55while I'm speaking.
00:56All right, so back in to what we're talking about.
00:59So we're talking about when we want
01:01to compare between the possibilities of one variable.
01:04Now, to do that is very simple.
01:05Basically, we create a new switch.
01:08Now, switches are really like if statements,
01:10only we compare one value at a time.
01:12They have a few magical features to them
01:14that are worth exploring into.
01:16They're not as used as if statements,
01:18but they're definitely a tool belt
01:20that you want to know about and you want to use.
01:22All right, so in my switch condition,
01:24I'm going to put inside here my username.
01:27Username, and now I'm going to open a curly bracket,
01:30close a curly bracket,
01:31and all of my different conditions inside of my switch
01:34are going to be between that opening
01:35and closing curly bracket.
01:37All right, my first case is going to be Ben.
01:41Oops, my case, Ben, and then the case information.
01:45So basically, I put inside the value
01:47that I want to use for this case and the colon.
01:50Now, I could put inside here,
01:51let's say I'm going to put inside the case of Ben,
01:53I'm going to put exactly the same thing as we had earlier,
01:57and I'm going to put here a break.
01:59Now, the break is very similar in like in the if statements
02:02where you had a closing curly bracket each time
02:05in cases we don't, and really what the break says
02:07is leave the switch and terminate it.
02:10You're done with the switch.
02:12Let's do the same thing now for the case.
02:14Let's say another person will call it Bell,
02:18and we're going to do the exact same thing,
02:20and we'll create one more case,
02:23and let's create a case for John.
02:28All right, so John now has a case as well.
02:29We're going to create the same exact message again,
02:32and again, we want to break out of it when we're done.
02:35Now, so we created now three different if statements.
02:38Now, the beauty of this is that it's a lot more organized
02:40than if statements because it's really easy,
02:42much easier to list them out in this way.
02:46And that's really one of their biggest advantages
02:48is that it's really easy to list them out.
02:49They're very visually pleasing, you could say.
02:52It's very easy to see that we're working with this case.
02:55We're using with this variable,
02:56and these are all the different cases of that variable.
02:58There's no way to do end,
02:59or there's no way to do bigger or smaller than,
03:02so obviously this does not fit every single condition.
03:05It's really ideal when you have a closed list of options,
03:08and you want to do a variation of a few different things
03:12depending on that scenario.
03:14Last but not least, we want to create also a default state,
03:17and that default state will capture
03:19all those different scenarios
03:20where we just didn't have any choice, any selection.
03:24So far, so good, and I just want to walk down
03:26through the sample one more time
03:28to just make sure that we really get it.
03:30So in our case, this will only happen
03:32inside of our 19 or 20,
03:34so let's just make sure that our age is 20.
03:37Now, in this case, because my name is Ben,
03:39so my username in this switch
03:41will first capture this case, Ben.
03:43It will execute everything inside
03:45until it meets the next break.
03:47When it meets that break,
03:48it's going to terminate that switch and leave it.
03:51All right?
03:52Now, in the case of Ben, it was really easy
03:53because as soon as we got here, we terminated it,
03:56which basically means that we're going to find
03:57that next curly bracket,
03:59and we're going to exit it in line 32.
04:01It's at line 23.
04:02So if we run our application, again,
04:04we should be getting Ben, you are an adult,
04:06and your name is, and so on and so on.
04:08Let's just make sure that we're tracing our age,
04:11not our age, but that message,
04:13and we'll refresh our screen
04:15and we'll see that Ben, you are an adult,
04:16and your age is 20.
04:17Perfect, which is exactly what we were expecting.
04:20Now, the same is true if we change our name to be John,
04:23then in the case of John, John,
04:25because it all has to be uppercase in our scenario,
04:28to match up because it's an exact match.
04:29So John with a lowercase j will just not work.
04:33It would actually go to our default.
04:34All right, so our John equals this John,
04:36and we could see that our message equals
04:38so on and so forth, and we should be getting this John.
04:41So basically, we'll check the case Ben, no.
04:44Bell, no.
04:44John, yes, and then we'll execute everything
04:47inside of John and then breakout
04:48because we have a break right after John.
04:51All right, so let's refresh our screen
04:54and make sure that we're really getting John
04:55and the age and we surely do.
04:57So again, if we set it to be a lowercase j
05:00and seven uppercase j, we'll discover that we have an issue
05:03and our issue is gonna be that actually John
05:05in lowercase does not equal John uppercase.
05:08And what will happen in that scenario,
05:10our default message will come true
05:12because we couldn't find any case that matches John.
05:15So if we refresh our screen again,
05:17we're gonna see our default message
05:18because we don't really know the name of our individual
05:21because it didn't match up in our cases.
05:24All right, so that really covers the basics
05:26of a switch condition and now it's time
05:27for us to play it up a bit.
05:29Now obviously, all right, so you probably noticed
05:32that we're very repetitive.
05:35Line 12, we repeat ourselves.
05:37Line 15, line 18, we repeat ourselves again.
05:40And it seems kind of counterintuitive
05:42that this is the better way of doing it.
05:44And it actually is because really in real cases,
05:47you'd probably have different scenarios.
05:48But what if in your real scenario,
05:51you really wanted to do the same thing
05:53for Ben, Bell, and John?
05:55Now, if we were using an if statement,
05:57it would be really easy.
05:58We would just do if the age equals 19 or 20 or 21.
06:04Now, the way it's done with the way it's done
06:06in a switch is very, very similar.
06:10What we do is instead of creating a break,
06:12we just delete that break.
06:13In our case, we're gonna delete everything
06:15inside of Ben and Bell.
06:17Now, even if Ben is caught or Bell is caught,
06:20the case itself just continues until there's a break.
06:23So in this case, no matter if Ben, Bell, or John
06:26are selected, they will all trigger line 14
06:29and then in line 15 terminate.
06:32Now, there's a big consequence to this.
06:34There's a lot of things you could do
06:35that you couldn't do with an if statement,
06:38such as maybe doing something specific for Ben.
06:40And let's say because Ben is the CEO of the company,
06:43I'm going to switch Ben's name from username Ben, name.
06:50And I'm gonna change that value from Ben
06:52to Ben the CEO of zero2geek.com.
07:02All right, so now if I save this out,
07:03what would actually happen?
07:04Even though the switch already met its condition.
07:07So line 11 was already executed.
07:09Now that line 11 was executed,
07:11it's not gonna go back again and change it.
07:13But we're changing our name for Ben.
07:15After changing the name for Ben,
07:17now we're gonna continue everything
07:18the same way as we did before.
07:20So if I go here and I set it up for Ben
07:23and I go into my training and I just refresh it,
07:25you'll see that Ben, the CEO of zero2geek,
07:27you are an adult and your age is 20.
07:30Although my age isn't 20, it would be nice.
07:32And I just wanna show one more thing now.
07:34If we choose Bell, if we change our username to be Bell,
07:38Bell would then trigger line 13,
07:40which would then continue on until it matches the break.
07:43And because there is no break between Bell and John,
07:45it would actually execute line 15 and then break.
07:51All right, now one thing that is also really important
07:53to note is that if you don't put a break at all,
07:55it will continue and actually trigger your default as well.
07:59So although you said, John, look what would happen
08:02if we call Bell again,
08:03this time without a break in line 16,
08:06you'll see that you are an adult
08:07and your age is 20 now is triggered,
08:09which is our default message because we didn't break.
08:12Now in some scenarios, that's exactly what we wanna do.
08:15And this scenario is absolutely not what we wanna do.
08:18So we've seen now with the switches,
08:19the different variations of ways of working with it.
08:22And it comes to be really, really useful,
08:24especially when you have layered if conditions.
08:28In many ways, what if you wanna do something
08:30only if Ben is there,
08:32but even if Ben or others, you wanna add features
08:34such as for example,
08:35maybe you have an admin tool on a site
08:39and you wanna add features based on seniority.
08:41So if you're in level three,
08:42you get a certain set of features
08:44plus all the stuff that are in level two,
08:46plus all the stuff that are in level one.
08:49If you're in level two,
08:50you get all the rights of level two and level one.
08:52If you're level one, you only get what's of level one.
08:55This way you could do it very, very easily
08:57without duplicating your code
08:58using a very simple switch condition.
09:01So that's all I have to say about switches.
09:03There's a lot more that we could learn about switches.
09:05And again, if you wanna dig deeper into switches,
09:08I really recommend you strongly to go into our courses,
09:11into zero to geek,
09:12and check out our title conditions to switches
09:15which go in depth into the details
09:18that we ran through them more quickly
09:21such as going through examples.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!