- The C Language let's you handle complex decisions
by stacking a bunch of if/else conditions.
Sometimes that structure can get a bit unwieldy.
As an alternative, you can employ the switch case structure
which is yet another decision making tool in the C Language.
In this movie, I introduce the switch case structure.
You'll see how it's constructed,
how it can handle multiple decisions,
plus I'll demonstrate a few tricks.
The bulk of this code is a switch case structure.
It starts with a switch at line 10, this is followed
by a series of "case" statements,
each of which has its own statements.
The final piece, is default, it's followed
by its own statement as well
and then a closing curly bracket.
These are the elements of the switch case structure.
The switch case structure also makes use
of the "break" keyword.
So all told, this structure contains
four C Language keywords: switch, case, default, and break.
Build and run the code to see what it does.
I'll type 1, "excellent choice."
Run it a few times to try out various options.
The code accepts character input, although
it could have easily accepted integers.
Here's how the structure works.
"Switch" handles a single value, not a comparison.
It can be a mathematical equation, but the result
must be a single value.
That value, specified by switch, is then compared
with the values for each case statement.
If the comparison is true, then the statements
belonging to the case statement are executed.
If not they're skipped.
So, at line 12, if the character 1 is typed, then
the put statement at line three is executed.
The break at line 14 ends the switch case evaluation,
returning control to the line after the switch structures
final curly bracket, which is at line 23.
Otherwise, if a match isn't made, execution falls
to the next case statement, and then the next.
Finally, the default condition is executed when none
of the case conditions match, it doesn't need a
break statement, as it's the end of the structure.
Modify the code by commenting out all the break statements.
Insert double slashes at line 14, line 17, and line 20.
Save the changes, build and run the code.
If you type a character other than 1, 2 or 3, you see
the invalid choice message displayed.
But type a 1, you see all the messages displayed.
That's because execution falls through
to each case statement.
Open exercise, switch two.
Here's another switch case structure, one that
evaluates input as a menu system.
Build, and run the code.
I'm going to type "A".
The problem with these options is that you may have typed
a little "a" instead of a big "A."
In that case the program doesn't seem to behave properly.
Address the issue by adjusting the case statements
to account for both upper and lowercase input.
Edit the source code to add duplicate case statements
for the lowercase letters, save the changes.
Build, and run.
Now the evaluation should work
for both upper and lowercase letters.
You could also address this problem by using C Language
functions that modify character case.
That topic is covered in another movie.