Skip to main content
Engineering LibreTexts

6.9: Case Control Structure

  • Page ID
    29065
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Traditional Case Control Structure

    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.

    C++ Code to Accomplish Multiway Selection

    Using the same example as above, here is the C++ code to accomplish the case control structure. 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.

    //C++ source code - case structure with intergers
    #include <iostream>
    using namespace std;
    
    int main()
    {
       int age = 33;
       switch (age)
       {
          case 18: cout << "\nYou can vote.";
               break;
          case 39: cout << "\nYou're middle aged.";
               break;
          case 65: cout << "\nConsider retirement.";
               break;
          default: cout << "\nAge is un-important.";
        }
        
        return 0;
     } 
    

    The value in the variable age is compared to the first "case" (note: case is one of the C++ reserved words) 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 cout is executed which displays “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.

    Most programming languages, including C++, 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):

    C++ source code - case structure with characters
    switch (choice)
    {
      case 'A': cout << "\nYou are an A student.";
                break;
      case 'B': cout << "\nYou are a B student.";
                break;
      case 'C': cout << "\nYou are a C student.";
                break;
      default:  cout << "\nMaybe you should study harder.";
    } 
    

    A couple of things to note:

    • A default value is NOT required
    • if you leave out a break your code falls through
         switch (age)
         {
            case 18: cout << "\nYou can vote.";
                 
            case 39: cout << "\nYou're middle aged.";
                 break;
            case 65: cout << "\nConsider retirement.";
                 break;
            default: cout << "\nAge is un-important.";
          }
      This is valid C++ code. If age were set to 18, it falls through to the next case and the would simply output: 
      You can vote
      You're middle aged.
    • Most programming languages, including C++, do not allow ranges of values for case like structures. 

    Adapted from:
    "Case Control Structure" by Kenneth Busbee, Download for free at http://cnx.org/contents/303800f3-07f...93e8948c5@22.2 is licensed under CC BY 4.0


    This page titled 6.9: Case Control Structure is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.