The switch statements transfers control to one of several statements depending on the value of a condition,
so which evaluates to either an integer or enumeration type.
The switch keyboard denotes a switch statement.
So the switch statements provide conditional branching.
When a switch statement executes control, transfers to the case, fitting to the condition or to a
default condition if no case matches the condition expression.
So the case keyboard denotes a case where the default keyboard denotes the default condition.
Somewhat confusing claim execution will continue until the end of the switch statement or to the break
keyboard.
So you will almost you will always find a break at each end of each description and condition.
So let's create an example.
Switch statement.
For example switch.
Here.
This is our condition.
And then we have a case, this case expression here, or you can write case like that in this and then
handle a case here, and then you will add the here.
And then you will add the brake here.
After this quarter braces, you will add brake, which I will explain why.
What what are these here?
So then, for example, let's add second case case expression to.
Here.
And then let's add here.
Handle, handle a case to here.
And then, as always, we do in switch statements, we do break here.
And after that, after that we have a default keyword in switch statement.
So let's write this as well, default keyword, which I will explain what is default handle?
The default keyword is default case here.
So now I will begin to explain these what these keyboards are and how these such statement, such statement
structure works.
So let's start with this switch keyboard.
So switch keyboard, followed by the condition in parentheses here.
Actually, we don't need to parenthesis here.
It's useless.
So such statement followed by this condition parenthesis here.
So all which begins with this.
You can't.
You just can't write.
Write a case and then switch.
So you have to do switch.
And then cases and after that default here.
So every switch statement starts with switch and condition here, and then each case begins with the
case keyword here, followed by the cases enumeration or integral value.
If condition equals here key.
For example, case one, for example, the code in the block containing here handle case here will execute
after each statement following a case here, you place a Greek keyword.
So if condition matches none of the case.
The default case here, as you can see here, is executes.
So I want to add something here.
The braces here enclosing each case are optional, but I highly recommend this.
Without them, you will sometimes get surprising behavior.
So always add these color braces about in the technical size technical view.
It's not mandatory, but highly recommended.
So let's create a switch statement with the enumeration class.
So I'm going to delete this.
You need this here.
And here.
So let's create an.
First, let's create an enum class here, for example.
Enum class.
Here race.
Or car models.
For my car models here.
I will explain all of these quotes here.
For example, BMW, BMW, Mercedes.
Renowned.
Ferrari.
Well, slugging.
Bought your jeep, and I think that's enough.
So as you can see, one, two, three, four, five, six, seven.
We have seven car models here.
So in main statement, let's write our switch switch case statements here.
So let's click the object race here.
Or my car models.
My car models.
Okay, so cars here or my cars.
Equals mike are models.
Here is operator and for example, Ferrari.
So then I will explain all of these quotes as I do always.
Switch here.
Switch.
As you can see here, our internal integrated environment automatically created this braces here.
So now we will give the switch here, my cars, because we will use this in class here and we will use
enum class members.
So mainly firstly, we will give my car as a parameter and then we will write our case and default statements.
So let's start with.
Let's let's follow this order here.
So switch case, switch, case, switch, case here.
For my cars, my car models, for example, BMW.
And then.
Hair color braces.
If the case is pretty BMW, you have a BMW.
And then we will break code break this case statement after the caller braces here.
And let's add another case here.
Case, my car models, we have the messages here and then print if you have a mercedes Mercedes-Benz
here, and then break our code.
Break our code, add another case because we have six variables here.
Then we have a result.
My car models result here, print f you have a result here, and then break this as well.
Uh, well, okay.
Yeah.
What's the problem here?
Yeah, I fix that.
And my car models renowned.
We have a well here, we have Renault's.
And let's add Volkswagen.
A printer if you.
You have a Volkswagen and then break this code also here.
And lastly, we have two car models as my car models here.
Let's add Ford and.
Here.
You have a fourth.
Break.
And lastly, my car was.
My my car models and jeep here.
Princess.
You.
You have a jeep here.
And at the break statement here, break here.
And then we're going to write.
The default.
Here's the fault.
Here the false statement print.
Plaintiffs.
Error here, for example.
User error.
Unknown.
In.
Unknown make or right model.
No model here and.
At.
Pain.
So.
But let's now explain this clause here.
The we create the enumeration class, declares the enumeration to type my car models, which you use
to initialize my car models to Ferrari.
So the switch statement here values the condition race to determine which condition to hand to control
to.
So because your hardcoded this to the Ferrari earlier in this code, as you can see here execution transfers
to.
Here.
Ops before.
Actually, we forgot to write the Ferrari here after Renault's.
I'm sorry for it.
Let's add case.
My car models Ferrari.
Here, print F.
You have a Ferrari.
Here.
So and execution.
Just let's add break here so.
Because you hardcoded this.
Ferrari error in this code.
The execution here transfers to this Ferrari.
So which prints you to You have a Ferrari.
So the break here, the break here terminates the switch statement.
So the default condition that this here is safety feature.
So if someone adds a new race value or new my new my car models value to the enumeration class, you
will detect that unknown raise to runtime and print an error message.
So try setting this my car race models or racing models to different values and let's see how the output
change.
So firstly I want to compile code and run here.
And as you can see here, we got an output that says you have an you have a Ferrari.
So let's let's change this Ferrari to BMW here.
And we will get and this case executes and we got you have a BMW.
So let's add another make a model here, for example.
With the GMC here.
And use this GMC.
And here, as you can see here, we got an user error.
As you can see, we got printed this because we didn't this defined this GMC on this switch statement.