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 keyword denotes a switch statement, so the switch statements provide conditional branching.
When a switch statement executes control, transfer to the case fitting to the condition or to a default
condition if no case matches the condition expression.
So the case keyword denotes a case where the default keyword denotes the default condition.
Somewhat confusingly, execution will continue until the end of the switch statement or to the break
keyword.
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 cases 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 break here after this curly braces, you will add break, which I will explain
why.
Uh, 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 two 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 will explain what is default handle.
The default keyword is default case here.
So now I will begin to explain this, what these keywords are and how this switch statement.
Switch statement structure works.
So let's start with the switch keyword.
So switch keyword followed by the condition in parentheses here.
Actually, we don't need to parentheses here.
It's useless.
So switch statement followed by this condition.
Parentheses here.
So I'll switch begins with this.
You can't you just can't 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.
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 break 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, uh, enclosing each case are optional, but I highly recommend this.
Without them, you will sometimes get surprising behavior.
So always add these curly braces.
But in the technical sides, 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 don't 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 codes.
Here, for example, BMW, BMW, Mercedes.
Burnout.
Ferrari.
Volkswagen.
Port here cheap.
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 create the object race here.
Or my car models.
My car models.
Okay, so cars here or my cars.
Equals my car models here.
This operator and for example, Ferrari.
So then I will explain all of these codes as I do.
Always switch here.
Switch.
As you can see, our integrated development 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
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.
Um, switch.
Case here.
Were my cars, my car models.
For example, BMW and then.
Here curly braces.
If the case is BMW, you have a BMW and then we will break code.
Break this case statement after the curly braces here.
And let's add another case here.
Case, my car models, we have the Mercedes 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 renowned my car models renowned here.
Print f you have a renowned here and then break this as well.
Uh, well, okay.
Yeah.
Uh, what's the problem here?
Yeah.
Fix that.
And my car models renowned.
We have a well here we have renowned.
And let's add Volkswagen.
Print f you.
You have a Volkswagen and then break this code.
I'll also here.
And lastly, we have two car models, case my car models here.
Let's add Ford and.
Here.
Print f you have a fourth.
Break.
And lastly, my car mode.
Oops.
My my car models and jeep here.
Print f you.
You have a jeep here.
And add the brake statement here.
Break here and then we're going to write.
Did default.
Here.
The fault.
Oops.
Here the false statement print.
Print f.
Uh, error here, for example.
User error.
Unknown.
UN.
Unknown make here or let's write model.
Unknown model here and.
Add.
And press here.
So.
But let's now explain this clause here.
The we create the enumeration class declares the enumeration type my car models, which you use to initialize
my car models to Ferrari.
So the switch statement here evaluates the condition race to determine which condition to hand to control
to.
So because your hard coded this to the Ferrari earlier in this code as you can see here execution transfers
to.
Uh, here.
Ops before.
Actually, we forgot to write the Ferrari here after.
Yeah, 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 our let's add break here.
So.
Because you hardcoded this.
Uh, Ferrari 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 at this here is safety feature.
So if someone adds a new race value or a new my new my car models value to the enumeration class, you
will detect that unknown race to runtime and print an error message.
So try setting this my car race models racing models to different values and let's see how the output
changes.
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 the you have a BMW.
So let's add another make model here, for example.
Let's add GMC here.
And use this GMC.
And here, as you can see here, we got an a user error.
As you can see, we got a printed this because we didn't define this GMC on this switch statement.