Locked lesson.
About this lesson
We look at how to use a Switch Statement within our program.
Exercise files
Download this lesson’s related exercise files.
35 - Switch Statement.docx57.6 KB 35 - Switch Statement SOLUTION.docx
59 KB
Quick reference
Switch Statement
Switch allows you to make decisions based on specific cases.
When to use
Use Switch when you have very specific cases to test against.
Instructions
string day = "Monday";
switch(day)
{
case "Monday":
Console.WriteLine("I've got a bad case of the Mondays!");
break;
case "Tuesday":
Console.WriteLine("Tuesday is better than Monday!");
break;
default:
Console.WriteLine("Is it the weekend yet?");
break;
}
Hints & tips
- Switch lets you test against specific cases
- 00:04 All right, we've been talking about if-else logic.
- 00:06 And there's another logic thing that is a little unique, and it's similar
- 00:10 to the if-else statement, but it's very specific, and it's called the switch.
- 00:15 And the switch allows you to be very specific about your code blocks and
- 00:20 make decisions based on a very specific set of criteria, right?
- 00:25 So it's a little hard to explain.
- 00:27 Let's just go ahead and make one.
- 00:28 Let's create a string, and we're going to call it day.
- 00:34 And let's set it equal to Monday, all right, so that's our day.
- 00:35 So we want to create a switch statement.
- 00:37 We could just call switch, and then what do we want to be testing?
- 00:41 We want to test day, right?
- 00:44 So here, same thing like the if-else statement, we have these curly brackets.
- 00:48 And inside of here, we create cases.
- 00:51 So for the case of Monday,
- 00:54 let's Console.Write out something here.
- 00:59 And let's say, I've got a bad
- 01:04 case of the Mondays, right?
- 01:09 And then we type break.
- 01:11 So just like with an if statement, when it becomes true, it automatically ends.
- 01:16 With a switch, you have to be explicit.
- 01:18 You have to say, hey, end, and to do that we say break, right?
- 01:21 We say, don't continue on with the rest of the code.
- 01:25 So we've got a case for Monday, let's create a case for Tuesday.
- 01:31 And let me just copy this and say, I don't know,
- 01:37 Tuesday is better than Monday, right?
- 01:43 I don't know, and we could just keep coming through here for
- 01:46 all the cases that we want.
- 01:48 So Wednesday, how do you spell Wednesday?
- 01:51 Is that right, that looks right.
- 01:53 So let's say woohoo, Humpday,
- 01:56 I don't know, right, and break.
- 02:01 And you could just keep coming through here, Monday, Tuesday, Wednesday,
- 02:04 Thursday, Friday, Saturday, Sunday, and creating all the cases that you want.
- 02:08 And that's what switch is good for.
- 02:09 When you know the very specific number and
- 02:11 criteria of the cases you want to create, then you're going to want to use a switch.
- 02:17 Now, we can also do sort of a catch-all, we can create a default, right?
- 02:23 And so here we might say, thank goodness it's not Monday, right, I don't know.
- 02:31 So if we go ahead and save this and run it, what do we think's going to happen?
- 02:37 Well, we see, I've got a bad case of the Mondays, why?
- 02:40 Well, because our day is Monday.
- 02:42 And we start this thing out, and we say, hey,
- 02:44 use this day variable as sort of the criteria.
- 02:47 And if day is Monday, as is the case here, then do this and then stop, right?
- 02:55 If day is Tuesday, do this and then stop.
- 02:59 If it's Wednesday, do this and then stop.
- 03:01 So we can type in Tuesday as our day,
- 03:05 right, run this guy.
- 03:08 Sure enough, Tuesday is better than Monday, right, and on and on.
- 03:12 If we pick some other day that's not listed in our cases here, remember,
- 03:16 we've got this default.
- 03:18 So if none of these are met, if none of these cases exist,
- 03:23 then we just do the default.
- 03:25 So save this and run it, sure enough, thank goodness it's not Monday.
- 03:31 Because that's our default text when none of the days
- 03:36 are explicitly listed here, right?
- 03:39 So switch is very useful.
- 03:41 Right here, I've just got this hard-coded in as Friday.
- 03:44 In reality, you'd probably have some user interaction where you asked the user
- 03:48 a question, hey, what day is it?
- 03:50 They type in Monday, it spits out, I've got a bad case of the Mondays.
- 03:53 They type in Tuesday, it spits out, Tuesday's better than Monday.
- 03:58 Wednesday, woohoo, Humpday, and on and on.
- 04:00 But for sake of just explaining this, I've just hard-coded it in here, and
- 04:04 you could certainly do it like that.
- 04:06 So that's all for this video.
- 04:07 In the next video, we'll start to look at loops.
Lesson notes are only available for subscribers.