In this lecture I want to go over a different control flow structure that's able to make decisions.
The switch statement.
So here's how we make a switch statement.
You start with surprise surprise the word switch is a key word in C++ and we have in here the parentheses
urgency expression for now and this expression is what gets evaluated so much like the if statement.
This is kind of the condition but it's a bit slightly different.
You'll see why.
So the next thing we have is something called a case.
So these are all the different cases that can happen.
And the cases are based on some constant expression.
And again you'll see an example preceding and between this after the semi or this colon here we'll have
all our statements like before statements go here.
DE
and optionally have something called a poor make statement as is optional and I'm not going to go over
this quite yet.
But ules.
You'll see in a second what this is for.
So you can have more Case statements Onsen expression say two and you can have as many as you want.
OK statements
in this last thing is kind of like the L's case in our IF statement last time.
But we're going to call it the default.
And this is the catch all for this switch statement.
So if if somehow the expression didn't match one of these cases it would go to the default and do and
do this.
This set of code default statements.
So again there's lots to unpack here.
So the expression here Mohs.
So this must evaluate to an integral type.
So an integral type could be something like an int or could be a char.
This is also an integral type or it could be a boolean that's also considered a integral type.
So it has to evaluate to some something like an int or a char or a can for our purposes and here let's
say let's say we use just X and we said indexical 0 K and the constant expressions here.
This is just some constant value.
So say let's say in constant 0 here.
This is a constant expression as well.
0 is just constant expression.
Or here we can have one let's say.
And that's also a constant expression.
So let's just see a little example of this.
Let's see Ouch.
Case 0
in C H.
Case 1.
And here in the default we will see a c h you fault's case
this and that's right.
You don't need these right now.
And through this
their case zero.
So we assign X the value.
And it's a switch on X and then it'll match this constant expression.
Whatever it is say X is 1 Greenwich it did Case 1.
Is this right here.
So one is matched to this case 1 and then it did these these statements.
Let's say do many geese one is d and s.
To say
right in this case one with the exclamation and just ones to gain so many statements.
So what this break here though this is kind of weird right.
Well this break will effectively just break out of the switch statement.
Once it's once you get to this point.
So it did give us one.
And then it said case two or so he said.
Case one in in case one statement two and then it hit this break point or is not breakpoint but break
state.
Right.
And then it broke out of the switch entirely and came here to line let's say 39.
So let's just say out of switch statement.
So case one case one statement two.
And then out of switch statement so it skipped say this default case.
So let's see what happens if we didn't have this break.
So it says case one case one statement two and then defaults case and then you're out of the switch
statement.
So like I said before it all go.
So it'll switch on x.
It'll find the first case that matches X and then it'll do every case below this one.
So will do every case below case 1 until the end or until it hits a break and then it'll exit the switch.
So this is a little bit different than if and else if control structure that we did last time.
So that's why we need the break.
So you'll usually see break for each case because usually we don't want to do cases below the case that
we matched.
Usually sometimes you do but usually do we don't.
So one interesting thing about switch statements is you can't do something like this.
Y equals zero.
Run this.
So air exists it's pointing to this.
Why doesn't.
It doesn't like this for some reason.
So if you want to make new variables inside of a case you have to do this you have to use the braces
that you talked about earlier.
So just brace around the case say it why 0
then this should work.
Like that.
So it doesn't mind it now.
So just remember if you ever want to make new variables inside of a case for a switch state and always
just wrap it around these curly braces.
In fact what I usually do for switch statements in and generals I always make curly braces for the case
every time.
And that is I just wrap until the break and then that's it.
This and that's usually how I write my switch statements.
So let's see a real example of this.
So it's delete lead on this.
So let's solve the please enter day of the week from 1 to seven problem that you solved in the first
section.
So let's have a day equals zero saying ouch E's teacher day.
6:59
see in a and then three switch statement switch on the day.
Case on remember we start the day on Monday right.
So one is Monday.
See out
Monday.
I'm not going to rate the braces for this like that and break it.
We don't want it to fall through.
If it was Monday or you don't want to do the cases below case 1 if it was Monday case to see Ouch Tuesday
and all of the break.
Case 3
CH
Wednesday
the
case for
Thursday
5
Friday
six
Saturday
seven C..
CH Sunday
and remember we had the default case B
say entered something outside of 6:59 say that was not such a valid day.
Spell it wrong.
Ballasted and through this so please enter day of the week so say 6 so it's Saturday.
You should read this again.
So we said negative one.
That wasn't about day so it went to the default case.
In this case the negative one is so see if we did three or remove all the breaks
all the breaks from the tires which state and see what happens.
So let's say it's Tuesday.
So the day of the week is Tuesday Wednesday Thursday Friday Saturday Sunday and that was not a valid
day.
So see how it might fail through.
So it matched to two notice it didn't match to one at all.
There's no Monday being out put it here.
So it it to and then it did every case below 2.
So that is why you need the break statements
so always remember that.
So in the next lecture I'll show you even one more way of making decisions.