Skip to main content
Engineering LibreTexts

4.9: Case Control Structure

  • Page ID
    10662
  • Overview

    A case or switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via a multiway branch.[1]

    Discussion

    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 unimportant

    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 flowchart example:

    graphics1-6.jpg

    This flowchart 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 wherein 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 flows 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.

    Code to Accomplish Multiway Selection

    Python does not support a case control structure. But using the same example as above, here is C++ / C# / Java / JavaScript / Swift code to accomplish the case control structure.

    switch (age)
    {
        case 18:
            message = "You can vote.";
            break;
        case 39:
            message = "You're middle-aged.";
            break;
        case 65:
            message = "Consider retirement.";
            break;
        default:
            message = "Age is unimportant.";
            break;
    }

    The value in the variable age is compared to the first “case”, which is the value 18 (also called the listed value) using an equality comparison or is “age equal to 18”. If it is true, the message is assigned the value “You can vote.” and the next line of code (the break) is done (which jumps us to the end of the control structure). If it is false, it moves on to the next case for comparison.

    Many programming languages require the listed values for the case control structure be of the integer family of data types. This basically means either an integer or character data type. Consider this example that uses character data type (choice is a character variable):

    switch (choice)
    {
        case 'A':
            message = "You are an A student.";
            break;
        case 'B':
            message = "You are a B student.";
            break;
        case 'C':
            message = "You are a C student.";
            break;
        default:
            message = "Maybe you should study harder.";
            break;
    }

    Limitations of the Case Control Structure

    Most programming languages do not allow ranges of values for case-like structures. Consider this flowcharting example that used ranges:

    graphics2-1.jpg

    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."
    End

    Using the case control structure when using non-integer family or ranges of values is allowed when designing a program and documenting that design with pseudocode or flowcharting. 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.

    Good Structured Programming Methods

    Most textbook authors confirm 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 to build our initial programming skills. It is not a created with the intent to cover programming languages in detail, despite the fact that at times we have to cover language mechanics.

    Key Terms

    case
    A control structure that does multiway selection.
    switch
    A control structure that can be made to act like a case control structure.