



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
College Notes. An introduction to the case control structure and how it is implemented using a switch within the C programming language. Case Control Structure, Connexions Web site. http://cnx.org/content/m19963/1.4/, Apr 5, 2010. Case, Control, Structure, Kenneth Leroy Busbee, Traditional Case, Control Structure, Accomplish, characters, Limitations, Programming, Methods
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




This work is produced by The Connexions Project and licensed under the Creative Commons Attribution License †
Abstract An introduction to the case control structure and how it is implemented using a switch within the C++ programming language.
1.1 Multiway Selection using the Case Structure
One of the drawbacks of two way selection is that we can only consider two choices. But what do you do if you have more than two choices. Consider the following which has four choices:
if age equal to 18 you can vote else if age equal to 39 you're middle aged else if age equal to 65 consider retirement else age is un-important
You get an appropriate message depending on the value of age. The last item is referred to as the default. If the age is not equal to 18, 39 or 65 you get the default message. In some situations there is no default action. Consider this owchart example:
∗Version 1.4: Apr 5, 2010 1:09 pm GMT- †http://creativecommons.org/licenses/by/3.0/
Figure 1
This owchart is of the case control structure and is used for multiway selection. The decision box holds the variable age. The logic of the case is one of equality where in the value in the variable age is compared to the listed values in order from left to right. Thus, the value stored in age is compared to 18 or is "age equal to 18". If it is true, the logic ows down through the action and drops out at the bottom of the case structure. If the value of the test expression is false, it moves to the next listed value to the right and makes another comparison. It works exactly the same as our nested if then else structure.
1.2 C++ Code to Accomplish Multiway Selection
Using the same example as above, here is the C++ code to accomplish the case control structure.
Example 1: C++ source code - case structure with integers
switch (age) { case 18: cout "\nYou can vote.";
Figure 2
Consider also the following pseudocode for the same logic:
Case of age 0 to 17 Display "You can't vote." 18 to 64 Display "You're in your working years." 65 + Display "You should be retired." Endcase
Using the case control structure when using non integer family or ranges of values is allowed when design- ing a program and documenting that design with pseudocode or owcharting. However, the implementation in most languages would follow a nested if then else approach with complex Boolean expressions. The logic of the above examples would look like this:
if age > 0 and age <= to 17 display You can't vote. else
if age is >= 18 and age <= 64 display You're in your working years. else display You should be retired.
Most text book authors conrm that good structured programming techniques and habits are more important than concentrating on the technical possibilities and capabilities of the language that you are using to learn programming skills. Remember, this module is concentrating on programming fundamentals and concepts and it uses the C++ programming language to build our initial programming skills. It is not a created with the intent to cover the C++ programming language in detail, despite the fact that at times we have to cover C++ language mechanics.
Denition 1: case A control structure that does mulitway selection. Denition 2: switch A C++ control structure that can be made to act like a case control structure.