Locked lesson.
About this lesson
Exercise files
Download this lesson’s related exercise files.
Switch.docx58.9 KB Switch - Solution.docx
59.5 KB
Quick reference
Switch
Switch is another type of conditional statement.
When to use
Use Switch when you have several "cases" to test against.
Instructions
The format of a switch is this:
switch (x) {
case1:
// do stuff;
break;
case2:
// do stuff;
break;
default:
// do stuff;
}
You can have as many "cases" as you like, the more the better (otherwise just use an if/elseif statement).
Here's a switch in action:
$car = "ford";
switch ($car) {
case "ford":
echo "You like ford";
break;
case "chevy":
echo "You like chevy";
break;
default:
echo "I don't know what you like!";
}
Hints & tips
- Switch is another type of conditional statement
- Use it to test cases
- Be sure to 'break' after each case, but you don't have to for the last default
- 00:04 In the last video, we talked about if-then conditional statements.
- 00:08 In this video, we're gonna talk about switch,
- 00:09 which is another type of conditional statement.
- 00:12 And if allows you to test, if this, do this.
- 00:15 Else, do that.
- 00:16 It's sort of one or two things, right?
- 00:18 Or you can add an else if to add a few more.
- 00:20 Switch, on the other hand, you're taking a bunch of set things and
- 00:24 testing against each one.
- 00:26 So that may not make a whole lot of sense, but
- 00:29 this is, let me just highlight this stuff and have it over.
- 00:33 So this is the basic structure of a switch.
- 00:36 You have your condition that you're testing.
- 00:38 And then case1, do some stuff, and then break.
- 00:42 Case2 ,do some stuff, and then break.
- 00:45 If neither of these cases are true, slip down to the default and do stuff.
- 00:49 Basically, you're testing against cases.
- 00:52 Let's create one of these so you cans see exactly what's going on here.
- 00:55 Let's create a variable called car.
- 00:57 And, I'm gonna set it equal to ford.
- 01:00 So if we create a switch, we start out with switch, i-t-c-h.
- 01:05 And then, let's pass it in our car, okay?
- 01:09 Now let's create some cases.
- 01:12 Create the case of ford.
- 01:16 Colon.
- 01:17 Now let's echo out, You like Fords, semicolon, break.
- 01:23 Now if any of these are true, this break will stop this from continuing on.
- 01:28 So let's go, case, Chevy, colon, echo,
- 01:34 You like Chevy's, I don't know break.
- 01:39 And then finally, our default echo,
- 01:44 You don't like anything!
- 01:48 Now we don't need a break because this is the last thing here, so
- 01:52 there's nothing to break from.
- 01:54 It already is gonna break.
- 01:56 You can put your default up at the top if you want.
- 01:58 If you do, you need to put a break.
- 02:00 Let's save this, come back here, and hit reload.
- 02:03 You like Fords.
- 02:04 Let's put, I don't know, GMC.
- 02:07 That's a type of car, right?
- 02:09 Hit reload.
- 02:09 You don't like anything!
- 02:10 Cuz our variable is GMC, so that's our condition right here, and is GMC a Ford?
- 02:17 No. Is it a Chevy?
- 02:18 No. Well actually, we should lowercase that.
- 02:21 So since neither of these two cases is true, it echoes out our default,
- 02:25 You don't like anything!
- 02:26 Most of the time, to be perfectly honest with you, I'll use an if-else statement.
- 02:31 Because if you think about it, you could do this in an if-else statement
- 02:34 pretty easily, and if-else statements are just really easy to use.
- 02:38 Switch is a little bit more complicated, a little bit more work to do the same thing.
- 02:42 The reason why you might use a switch,
- 02:44 is if you had 50 different cases you wanted to test against, then it might make
- 02:49 sense to use a switch just to break it up and make it easier to read or whatever.
- 02:53 But I find, after 20 years of doing this, I hardly ever use a switch.
- 02:57 I'll just use an if statement, or
- 02:59 an if-else statement, or an if-else-if statement.
- 03:02 But there you have it.
- 03:03 That's the sort of the last main conditional statement we wanna talk about.
- 03:06 In the next video, we're gonna talk about loops.
- 03:09 Loops are pretty important to programming and a lot of fun to play with.
- 03:12 And that's all for this video
Lesson notes are only available for subscribers.