Hello YouTubers, I'm Parvez and this is our sixth video in C++ programming language
In this video we are going to talk about the switch statement in C++
So switch statement is used when we have multiple conditions and we need to perform different
action based on the condition. For example, if we have different conditions and we want to execute a block of code based
on the certain condition, so in that case we need to use switch statement
Even though we can handle these kind of scenarios using else if statement, but it's a good idea
if you have multiple conditions you can use switch statement. So now let me just show you the practical example of using switch
So I'm going to just make this example based on the user input
So I'm going to just create a variable of type character and I'm going to call it grade
After this I'm going to get the user input and based on that I'm going to create my switch statement
So let me just say please enter your grade and let me just end line and I'm going to
get the user input and I want to store that input in my grade variable that I have created
So I'm going to just CN and grade like this. Okay now after this we are going to just create our switch statement
So let me just say switch and let me just switch
Okay now in here you need to we are going to add our grade and after that we are going
to add our cases. So for example, if our we have case A, so for example, if the user enter A, we are going
to just see out. So if the user enter, for example, case A, we are going to execute this statement
So we are going to just say that see out and I'm going to just say for example, excellent
And let me just end line. And also you need to break the statement
Okay like this. Now this was our case A. Now in the case B, so let me just add my case B in here
Okay so in the case B, it means that if the user enter B. So if the user enter B, it means
I'm going to just let me just copy this and paste it in here
See out, I'm going to say that very good. Okay. And let me just copy this because I'm going to add more some more cases
So now in the case, for example, C, if the user enter C, I'm going to just say good
For example, let me just remove this. And I want to say good
And in the case D, let me just add one more. Let me just add this make this D. And in this case, I'm going to just say you're paused
So you were paused. And in the case E, I'm going to just say, for example, you are failed
You were failed. Okay. So now let me just describe this code
And here I have created a variable of type character, and I'm going to get the input
from the user and store that in my create variable. After that, I'm going to check the cases
So if this if the user enter A, I want to say that excellent
I want to break. After that, if the user enter B, I'm going to say very good
Unlike this, I want to use my five cases in here. So now if I run this code, now let me just run this
Okay, now if I enter A in here, you can see that I am receiving excellent
Now let me just run it again. For example, if a user enter D, it's receiving that you are paused
Now if I enter another one, for example, E, you're failed. Now search is working, there is no problem
But also we need to handle default cases. For example, if I enter for example, aim, so we are not receiving anything, we need
to handle these kind of cases. For that you need to add default, we need to add a default case in here
So let me just. Okay, now in the default case, I'm going to just say that invalid great
Okay, so now if I run this, okay, now if I enter for example, A, so there is no problem
I'm receiving excellent. Now if I run it again, and if I enter for example, n, you can see that we have agile
the default and we are receiving invalid. So thank you guys. If you have any questions, please let me know and for the further videos, please subscribe
to my channel, like the video and also share the video