Locked lesson.
About this lesson
Understanding conditional statements using switch.
Exercise files
Download this lesson’s related exercise files.
Switch.docx59 KB Switch - Solution.docx
59.5 KB
Quick reference
Switch
A switch is a type of a conditional, similar to an If statement.
When to use
Use switches when you have lots of different cases to test against.
Instructions
Switches act sort of like if statements. The general format is:
switch(expression){
case a:
//code block;
break;
case b:
//code block;
break;
default:
//code block;
}
Hints & tips
- Switches are similar to If statements
- Use a switch when you have lots of cases to test against
- 00:04 So in the last video, we looked at If statements.
- 00:07 In this video, I wanna talk about switch.
- 00:09 And switch is similar to an If statement, but a little bit different.
- 00:12 So switch is a conditional, just like If and
- 00:14 else, but it allows you to select a bunch of different little blocks of code.
- 00:18 So, this is basically what a switch looks like.
- 00:21 We have, we call switch, and then we have an expression, and
- 00:24 then it's gonna test against each of these cases.
- 00:28 So, this may look a little bit weird, but it's actually pretty easy.
- 00:31 It starts out, like I said, it evaluates this expression.
- 00:35 And it compares each of these cases to that expression.
- 00:39 As soon as it matches to one, it executes whatever code is in here, and
- 00:43 then it ends.
- 00:44 This break is basically an end.
- 00:46 If it doesn't match, it goes down to the next one, and it tries to match it.
- 00:50 If it doesn't work, it goes through all the different cases.
- 00:52 And then finally,
- 00:53 if it doesn't match any of them, it spits some default code that you designate.
- 00:58 So let's just make one of these.
- 00:59 I'm gonna go, I'm gonna call var first name set that = to John.
- 01:05 So it's just creating a variable.
- 01:06 So now let's do a switch, and we just go switch, and then here's our expression.
- 01:11 I'm just gonna say firstName.
- 01:14 So it's just gonna pop in that variable right there.
- 01:17 So then we do that.
- 01:21 And now, we created our cases.
- 01:23 I'm just gonna do a couple of cases.
- 01:25 Let's go, John, and you put ;.
- 01:28 And then, the code that I wanna write,
- 01:32 the block of code, is just right here, so that's our first case.
- 01:37 Our next case will go, let's say, Mary, right?
- 01:41 And the same thing, so let's change this to Mary.
- 01:49 And then finally, let's do a default.
- 01:52 And, I don't know, let's go, document write, sorry I don't know you, first name.
- 01:58 So if we save this, and come up to our page, and hit reload,
- 02:02 we see nothing at all.
- 02:03 What have we done here?
- 02:05 I misspelled switch, S-W-I-T-C-H, all right.
- 02:11 And I'm just gonna pop these in, just to make it a little more readable.
- 02:15 So if we save this, now we come back, boom, Hello John!
- 02:18 If we change our variable from John to Mary, come up here, Hello Mary!
- 02:23 If we change it to Bob, we save this, sorry, I don't know you Bob.
- 02:30 So what it's doing here is,
- 02:31 it's taking our expression first name and it's testing each case.
- 02:35 It's saying, is our first name John?
- 02:37 If so, do this, else test to see if it's Mary.
- 02:42 Is our first name Mary?
- 02:43 If so, output this.
- 02:44 If not, no.
- 02:45 If you go through all of our different cases,
- 02:47 and we can have as many cases as we want.
- 02:49 And if none of them match, then we output this line of code right there.
- 02:53 So that's a basic switch.
- 02:54 Now notice, I put brakes under each one of these.
- 02:58 In an If - Else statement, we dont have to break out.
- 03:00 If a statement is true, the line gets executed and then the statements ends,
- 03:04 not so in a switch.
- 03:05 You have to explicitly tell a thing to break.
- 03:08 Except for you'll notice down here, I didn't, and
- 03:10 that's because this is the last one listed.
- 03:12 We could put our default anywhere we want.
- 03:13 We could list default right up here at the top.
- 03:16 And if we do, then we need to put a break there.
- 03:18 But since it's the last one, I can just leave it the way it is.
- 03:21 So, that's switch.
- 03:21 You can see it's sort of like an If - Else statement just formatted a bit
- 03:25 differently.
- 03:25 To tell you the truth, I rarely use switch.
- 03:28 I find that If - Else statements pretty much handle most of what I need them to do, but
- 03:32 every once in a while, there's a weird thing where this makes sense to use.
- 03:35 So anyway, that's switch.
- 03:37 In the next video, we'll start to look at for loops.
Lesson notes are only available for subscribers.