Welcome back.
In this video I want to show you a switch statement and how it works.
And in the next video we're going to look at the difference between the two.
So when to use which of the two variants.
Because the switch statement is very similar to the if statement.
So we've seen how the if statement works including else, if and else.
Now let's look at how the switch statement works.
And it uses keywords such as switch case break and default okay.
So let's use all of those four words by starting with a switch statement.
So you see there is this snippet this code snippet.
So if you enter switch and press tab then it gives you a default switch here.
So obviously you need to replace the variables that you are looking on.
So basically a switch is like a light switch that you can turn on or off.
Right.
But the light switch only has two cases.
But there are situations where you might have multiple cases.
So there is a switch which can go like for example, when you're driving in the car, you have a switch
which goes from driving to park or from parking to, I think reverse to neutral to driving and then
to speed or sport.
I think sport it is, at least in my car.
So there you have basically also a switch which has five different cases.
Okay.
So here we're looking at switch.
And what we're checking for is our integer month okay.
So int month.
And this is an example where we are actually using an int.
But you can also use strings or other data types in here.
But usually it's used with int and very easily.
So.
And then you have the different cases.
So for example case one.
So in case month this integer here is one.
Then we want for our month name to be January okay.
Very simple example.
But then we need to add this break keyword.
Why do we need this break keyword.
What break does is it makes sure that after this statement is true, none of the below can be true.
It's similar to what we saw with if statements, right?
So if this if statement is true, then none of this code will be executed.
However, if this if statement is not true, then this one will be checked.
And if this one is true, none of the below will be executed.
So basically we're saying break out of this if statement, get out of it, and then keep going with
the code below that.
So once this break is executed, the code goes on at line 29.
In this case okay.
So now let's have the other cases.
So for example we have case two where the month is February.
And then we have case three where the month is March.
And then we have this a default case which basically sets our month name to unknown for example.
So what default does is default is basically this else block which takes care of all the other options.
So if month is anything but the numbers that we have covered, we're going to jump into the default
case.
And then we're just going to set the month name to unknown.
And that's generally speaking how a switch case or a switch statement looks like.
So you have the switch keyword.
Then you have your brackets in which you put in the variable that you want to compare to.
And then you compare the variable to the cases.
So you compare month to the value of month to the case of the value being one.
If that's the case, run this code and then break out of it.
If that's not true, then go to case two.
And if that's again not true, go to case three.
If that's again not true, go to case default.
Now obviously you can also have case four, five, six and so forth.
You could go all the way to basically have all of the months.
All right.
So this is how a switch case looks like in comparison to an if and else if.
All right.
And now we could go ahead and print out the name of the month is.
And then we use month name here month name like so.
And we need to add the dollar sign here.
So that month name the variable will be used.
If we now run it we should see the month is unknown okay.
So now this is obviously duplicate code now.
But I just wanted to show you the two in comparison to each other so that you see how similar they are.
And the logic behind them is pretty much the same.
So they work in a very similar fashion, but they have certain advantages and disadvantages, and that's
something I want to cover in the next video.
So see you there.