 Hey, everybody, it's Brian, and this is our 12th C++ tutorial. Today we're going to be discussing the switch statement. What is the switch statement, and why do you need it? Well, let's back up a minute. You know what the if statement is. I'm just going to paste some code I wrote previously. As you can see, we have int i equals 0. If i equal 1, then do this. If it's 2, then this. This can get just monstrous and a real chore to pick through and debug and figure out what's going on. So instead, what we have is the switch statement. If you're coming from Java C sharp, you understand the switch statement. If you're coming from VisorBasic, this is like the select case. Switch i, and then we have case 1. We have to put the break in there. What the break statement does, let me actually put some tabs in here so you can see, the break statement stops flow control. Now, what do I mean by that? Well, I'm going to explain this here in a second. Let's see, case 2. And let's throw in default. Break that up a little bit. We'll just say other. This is the structure of a switch statement. You have switch i. We're saying case 1. So we're saying basically, if i equals 1, then run this. If it's 2, run this. If it's something that hasn't been covered already, run this. This is our default. It's a catch-all. The break statement keeps us from falling through, meaning we say, switch i, if it's 1, do this. If we omit this, the code will immediately jump to case 2. And if we omitted that, it'll immediately jump to default. So you can actually process multiple cases at once. Let's just do the standard here. Let's just say 2. So when we run this, knowing that i equals 2, it's going to say, is it 1? No. Is it 2? Yes. Run this code. Let's run this. Sure enough, 2. Press any key to continue. Let's get rid of the fall through here. Now, in some languages, if I remember right, C-sharp, this would actually be a violation. You can't allow it to fall through. C++ is perfectly legal to do that. Now, you can see we've got multiple results here. This is two other. Well, what happened was we're saying i is 2. So it jumped over that. Said yes, it's 2. But we also have a catch-all. So it triggered that as well. That's fall through. Now, in case you're wondering, what's the best thing to do? Should you use multiple if statements or a switch statement? Well, to be brutally honest, a combination of both. If you notice that you have a if statement where you're just comparing a bunch of numbers and then triggering something, use a switch. But if you need to nest if statements within if statements within if statements, then you should probably look at your algorithm and find a better way to do it. You can, however, add an if statement in here and do something like that. Let's actually throw the fall through back in here, or fall through the breaks back in there. And let's just say int j equals 1. We'll say if j is greater than 0. And let's just throw some more code in here. We're going to say j greater than 0. And you can see, whoops, 2. What did we do wrong here? That's right. We need to set this to 1. Set this to 99 just for the sake of clarity here. OK, now our example should run. Sorry about that. So we have 1 and then j greater than 0. So what happened here is we are saying switch i. Yes, i equals 1. So run this code. And in this code, we have an if statement. If j greater than 0, which it is, run this block of code. And because we have the break, it will not fall through to the other cases. I hope that's pretty clear. This is one of those mind-bending examples that gets very difficult. And usually when you're in your first programming language, they spend a few days talking about this, unless you have a really bad instructor. And they spend five minutes like I just did. This is Brian. Today we've covered the switch statement. Hope you found this video educational and entertaining. And thank you for watching.