Hello, welcome to the 28th tutorial on the c++ programming series. And in this part we'll be looking at the switch statement. switch statements are essentially a simplified if else if else statement. And the related check for variable has a certain value you cannot use integrals or enumeration basically how you integrate or Charles kinds of strings technically that's a user defined name, user defined type even though you belong to the standard namespace. It's not part of the it's not starting to grow or it's not an enumeration I think it was pronounced interviewer of integral earring. Yeah.
So you can only use and and Charles and lots it's a very simple if else if statement doesn't need to do really a condition. The only condition that it does itself is allows you to specify a variable and then you can check if the variable equals in Nevada so you got a is greater And or if this is greater than this, and this is this, all this all these locks a very simple stem. And there are certain instances where you just want some kind of switch statement. So let's show you how to use a switch statement very extensible about very easily to add new conditions. So we're going to do is we're gonna create a variable, we're gonna call it superhero, we're gonna assign the value one to it. What we're gonna do is switch.
And this is the syntax of a switch statement, you have your expression, which is basically variable that you want to check, don't put superhero, then the constant is basically the comparison. So those superhero equals zero. And then statement is, whatever you want to do. Once this other code equals haven't noticed with 78, whatever, if you could have a for loop, you can happen with any sort of code you want. So what we We're going to do is we're going to do STD c mon STD and line and the default statement is basically the outs the equivalent if not the case your tree will fall back to this and in here we're just gonna put the CDC not a superhero semicolon, nephew right? I was gonna open up the debugger area since nobody's up Rob because he checks those superego zero now and I know the checks he just default to this statement but what we can do that another case of case is literally to add a case you do case then the You can put one colon, and then you put whatever thing you want to do.
Let me just show you something. Actually, let me show you this clip. And then I'll show you what the purpose of the break statement. Nothing now bring this at all to an in your day. He's not a superhero. Yeah, super power.
So let's see the superhero. Run this, and you get Yoda. So what we're going to do is, yeah, forgot to put in the break statement. And that's a good way to illustrate what the break statement does. The brace timer prevents the code from going to the next case or to the default case. And if you don't have a break statement, it doesn't do the check.
It's essentially as this this wasn't there, or this wasn't there. And both of these longer in the case, you might think of that sort of fault. No, we didn't because sometimes, you may just want to group a bunch of conditions together. You want you may do it this. Let me show you for a break here. And you might just do case two, case three, case four, case five and an order all the cases we want to do the same function until it's going to print out.
T case need to put quotation mark s, t and y when it changes we just need to input mode I forgot to put a break statement and we put we run a faith mode has come down here they'll supercritical zero not equal one none superhero equal to yes no breaks emanates witches so this one is no code here but uh, no breaks down miniver thankful for and then it goes to Princeton said then it's The break and then the exits out to the switch there. They have it for this tutorial. If you have any questions feel free to message us at support at sono system correct Hey, the email will be in the description in comment on this video with us directly messages via YouTube wherever you feel comfortable with or requiring for source code. We'll also be inscription in the next part of the series we'll be looking at functions and as usual, thanks for watching and I hope you have a nice day.