In this video, we'll learn about c++ switch statement.
As you've already seen, it's very common to have to execute
specific sections of code depending on the value of a constant.
For this particular use case, the switch statement comes in very useful.
We'll learn the switch, case, break and default keywords.
The syntax for the switch statement is a little more
involved than the if statement.
But it's not too bad once you understand what it's doing.
First, we have the switch keyword, which is followed by the control
expression inside parentheses.
This control expression must evaluate to an integral
type or an enumeration type.
Then you have a series of case statements enclosed in curly braces.
The value of the control expression will be compared to the values,
following the case keywords.
These case statements must also evaluate to an integral or
enumeration type and must be known at compile time that means they
must be constants or literals.
When the value of the control expression matches, the case
expression then the code after the colon is executed until
you hit a break statement.
The break statements are optional, but best practice is to include
break statements for every case statement unless you've got
a really good reason not to.
In the next, several slides we'll go through the behavior
of a switch statement with and without break statements.
Finally, a switch statement can have a default case at
the end that will execute when none of the cases is true.
This is similar to that catch-all else at the bottom of an if-else statement.
The break statement is not needed in the default case.
Let's see some examples of the various forms of the switch statement.
In this example, selection is a character type which of
course is also an integral type.
We assume that the user has entered a character at the keyboard and
it's been assigned to selection.
We can write if else if statements to execute code depending
on the character entered.
But in this case, we can use a switch statement since the control
expression is an integral type and the case statement contains
character literals to match.
So suppose the user enters the character 2, the switch will start at
the first case statement and compare the 2 to the literal expression,
following the case keyword.
So the 2 will first be compared to the 1.
Since this is not true, we proceed and check the next case statement.
In this case, we have a match.
So all the code following the colon will be executed.
Notice that we can have multiple statements here
without the need for a block.
However, if you need to declare variables in this section,
then you need to put the curly braces in for a block.
So we display two selected then we execute the break statement which
completes the switch statement.
No further cases will be checked and we continue with
the rest of the program.
Notice that in the case of 3 and 4, we display 3 or 4 has been selected
and then we're done with this switch.
This is handy since we can write logic that's equivalent to an or
statement, as we see in this case.
Finally, the switch has a default case.
If none of the previous case statements match, then we execute
the default, which in this case displays 1 2 3 4 not selected.
No break is needed in the default statement.
Also the default statement is optional, but it's best
practice to include one.
Let's look at an example where we don't have some break statements.
This is an example of the fall through behavior of the switch statement.
Notice that there are no break statement in cases 1, 2 and 3.
Case 4 is the only case that has a break statement.
Suppose the user enters the character 1, and it was assigned to selection.
We check the first case statement and it matches.
So we execute the code following the colon.
In this case, we display one selected.
Now we continue executing code going straight down the switch statement
until we hit a break statement.
So in this case, we also display 2 selected and 3
selected and 4 selected.
Finally, we hit a break statement.
In case, 4 and we're done with the switch.
Remember, once a match is made, all the code in the switch
cases, including and following that match, will be executed
until we hit a break statement.
No other conditions will be checked.
This sort of logic can be handy in some cases when you want to
execute a subset of statements depending on various conditions.
However, this should not be the way you normally code a switch statement.
Best practice is to include a break statement in every case statement.
If you need logic that falls through, please document the
code and be very clear in the documentation that the fall through
behavior is what you intended.
If another programmer sees the code and needs to modify it,
they'll understand this and be very careful when adding or removing
new conditions or statements.
Let's see an example of using a switch statement with an enumeration.
In this example, we see an example of using a switch statement
with an enumeration type.
We have an enumeration type called color with 3 enumeration
constants: red, green and blue.
We also have a variable named screen color, which we initialize to green.
But it can hold any of the 3 enumeration constants.
Using this enumeration variable in the switch statement is a very
common use case and a great way to execute code depending on its value.
In this case, we switch off the screen color which can
only be red, green or blue.
And we execute the appropriate code when we match the specific value.
Also notice that in this case, we're supplying a default case
but this should never execute unless we add another enumeration
constant to our enumeration and forget to include a case for it.
So let's review what we learned about the switch statement.
The control expression must evaluate to an integral type,
the switch statement is commonly used with enumeration types.
The case expressions must be known at compile time, so only constants or
literals can be used, no variables.
Once a match occurs, all code in the following case statements is executed
until a break statement is reached.
Finally, best practice is to always include break statements for each
case and also provide a default case.
Let's head over to the IDE and see some switch statements in action.
I'm in the IDE in the section 9 workspace in the switch project.
And what we're going to do, we're going to do a little program here
that uses a switch statement.
We're going to ask the user what grade they expect on an exam that'll
be a character: a, b, c, d, f.
And we'll tell them what they need to score to get it.
Okay, so I made a start here where we have a letter grade
that's a character, and that's what the user is going to enter.
We prompt the user to enter the letter grade they expect on the exam,
and we read that into letter grade.
Okay, so now what we want to do is we want to switch off of letter grade.
It's a character.
So that's definitely an integral type.
So we'll say switch, and we want to switch off letter grade.
Okay, I'll put in the syntax I need which is the open and close
curlies for my switch statement.
Now what we have in here is we need to provide the cases.
Now in this case, we can provide upper and lower case for each character.
So we could say.
Case lowercase a and case uppercase a.
I want the same behavior right because the user could
certainly type a lowercase a.
So if they type a lowercase or an uppercase a, all we'll say is cout,
let's say, you need a 90 or above.
And we'll tell them to study hard.
Okay, so that'll be the statement for that specific case.
We need a break.
Otherwise, we'll fall through.
I don't want to fall through in this example.
And let's do the b case.
Again, lowercase and uppercase.
And also remember that I'm using single quote marks here
because these are characters.
So in the case of a b if that's what they want, I'll say something
like you need 80 to 89, for a b.
And we'll say go study.
And I'll use an endline here.
And again, I want to break.
Let's do our c case.
It's only a couple left.
So there's my lowercase c.
Here's my uppercase c.
And in this case, I'll just say output a, something like you need 70 to
79 for an average grade, let's say.
And we'll endline here.
Again, a break.
We'll do the d case.
And here, we'll say something like I don't know like, you
should strive for a better grade.
Let's just say all you need is 60 to 69 or something like that, and
we'll put an endline here as well.
Of course, the break is required here because I don't want to fall through.
And then in the case of f.
Again, lowercase f or uppercase f, here we'll just do cout, let's
just put a little placeholder here to test ourselves because we
want to add a little bit of code here once we finish this example.
Okay, and we'll break out of here.
Our default is going to say something like, sorry that's not a valid grade.
And I think we're pretty much done here.
Okay, let me clean that up just a little bit, so let's look through
this right before we run it.
We're grabbing the letter from -- the letter gray from the user.
We're switching off of it.
We're checking for lowercase, uppercase a.
We've got a message: the b, the c, the d, the f.
And sorry not a valid grade.
And we'll get back to the f in a little bit.
We'll write some code in there.
So let's execute this.
Enter the letter grade you expect.
I want an a, you need 90 or above, study hard.
Okay, let's do the b.
I'll do a lowercase b this time.
You need 80 to 89 for a b.
Go study.
And let's do the f.
And there's my x's.
Okay, so what we want to do now is in this case right here
in the f case, let's ask, are you sure you really want an f.
You know did you type this in correctly.
So what we want to do is we want to write a little
if else if statement here.
And I'm going to declare a variable in here.
So I want to be sure that I'm using a block.
And I want to use the block right in here like this.
Okay, so I'm going to get rid of this guy.
And I want to put the break inside this block.
Sometimes the IDEs don't put those curlies in the right places.
I think it's okay now.
Okay, so this is the logic I want here.
So I want to have another character.
And I want to -- I'll call it confirm because I want to confirm
that they really want an f.
And we'll initialize it to blank, to 0.
And then we'll say cout, are you sure or something like
that, we'll say yes, no.
Okay, so they'll enter a yes or no.
And I'll read into confirm.
Again, so they're going to type in a character, either yes or no hopefully.
So what do we do.
Well, let's say if they type in a yes uppercase or lowercase,
we'll give them a message.
If they type in a no, we'll give them another message.
And we've got to make sure that they type a yes or no.
If they typed anything else, we'll give them a little message that says
I have no idea what you're saying.
Okay, so let's do that.
Let's say if the confirm character is equal to a lowercase
y or the confirmed character is equal to an uppercase y.
Remember, don't forget to do the double equal signs here
because we're comparing.
So if that's the case and I'm not going to use any any block statements
here because I don't need to.
I'll just say cout.
Okay, I guess you didn't study or I guess you don't want to study, right.
And I'll give them an endline here.
Else - let's check to see if they actually typed no
because it could be anything else now, right.
All we know is that it's not a yes.
So in this case, we'll say else if confirm is a little lowercase
n or again confirm is an uppercase n, then we'll just simply say
something like cout good, go study.
So now here, we need that catch-all else, right.
At this point, they typed something in other than a yes or no.
So what do we do here, we tell them boy you really do want that f, right.
We'll just say you cout illegal choice or something or something
more clever if you like.
Okay.
That should do it, now because I'm creating and declaring that
character right here, I need those curlies inside the block.
Otherwise, if I was not declaring a variable here, I
don't need to put the curlies.
But once I have to declare a variable I need those curlies.
Right, so let's execute this and see what's going on.
Okay, so what's the letter grade?
The only one I really changed was the f.
So let's type the f, and now it says are you sure.
I'll say, yes.
Okay, I guess you didn't study, exactly what we expected.
Let me run it again with the f.
Are you sure, no.
Good go study.
And then we'll just give it another character.
Again, we'll go f.
And let's say, are you sure q., illegal choice.
There you go.
So here you can see some logic using the switch statement with
a character, which is an integral type, a bunch of literals here with
this lowercase a's and and so forth.
And in the case f, we've got some if else if logic going on as well.
So you could see that the logic for this is pretty straightforward.
The switch statement makes it nice to do.
It's kind of long.
The code is pretty wordy.
But it's very very easy to see, very easy to understand.
And when you read this, I mean it can't get much
clearer than that, right.
You're switching off a letter greater if it's an a or an a or
a b or a b really, really clear.
Indentation again and style really, really important.
If everything here was at the same level all the way on the
left hand side, it would be really really hard to read and
figure out what goes where.
Okay, so that takes care of the switch statement in this context.
Next, we'll do the switch statement using an enumeration and we'll
do that one really quickly.
Okay, so I've switched to the switch enum project.
And I've started it already.
So let's go over it here.
What I've done here is I've created an enumeration type called direction.
And you can see it right here between lines 10 and 12.
And the enumeration constants are left right up and down.
So it's pretty straightforward.
Remember, when you declare an enumeration, you're
creating a new type.
So we can create variables of that type.
So heading is of type direction, just like we could have said I is an int.
And we're initializing it to left, which is one of
those enumeration constants.
Okay, so now what we want to do is we want to switch off of that heading.
Heading is an enumeration.
So basically, it's an integral type.
That's what your numerations are.
So here, I'm going to type in switch and I want to switch off heading.
In this case, our cases are left, right, up and down, right.
They have to be they can't be anything else.
Let me get rid of that extra curly here.
So we can say, case left and then at this point we could just say
something like cout, let's say going.
Left just something really simple so that we know we hit the right
case, and we'll put an endline here.
And of course, I want to break.
Okay, we'll say case right, and we'll do the same thing, I'll
just copy and paste these two lines to save a little time.
And now I'm going to compile this.
And you'll see the issue that we see right away.
And let me just clean this up, just a little bit.
Okay, so let's compile this.
And I get a run that says going left just like I expect right.
But look down here and you've got some warnings.
The warnings are saying warning enumeration value
up not handled, right.
Warning enumeration value down not handle.
This is telling you that the compiler understands that there are
four enumeration constants here.
You're using an enumeration variable in your switch, but you're only
handling two of those cases.
Well, c++ gives you the warning.
A lot of other programming languages now won't compile.
This this will be an error.
They force you to handle all the types.
So you could create two more types for up and down or you can use a default
that's going to catch everything else.
And I'll just say okay here.
And now no warnings.
All good, and we're going left.
Okay, so that's really what I wanted to show you
with the enumeration types.
When you use them with switch statements I mean there's a reason
you create an enumeration type, right.
You've got these enumeration constants.
So that variable heading could be any one of those four.
So you really should be handling all four cases in your switch or using
a default to handle the fall through in case one of them doesn't match.
Okay, so that finishes up this video with the switch statement.