Examples of the Switch Statement

Here are a set of examples involving the logical operators switch statement

The first one counts the number of times the letter grades (A, B, C, D, and F) are entered, making it a variation of the code used to demonstrate the use of logical 'or' with the 'if' statement. Note, also, how it uses a boolean variable. In this example, the name of the variable implies that the code in the loop should continue until the boolean variable holds the value "false". This is the opposite of how such booleans are often used. However, there really is no fundamental difference in the code. The boolean is initialized to a value before the loop; the 'while' statement test checks to see if the variable still has the same value; and inside the loop there is a section of code that, under the right conditions, changes the value of the variable.

// A program to count the number of A's, B's etc entered as grades
// Uses a typedef to declare a boolean and a switch statement
// All code in 'main' in order to focus on 'switch' statement
// file: ch6xmpl1.cpp

#include <iostream.h>

void main()
{
// Purpose:     A program count the number of A's, B's etc entered as grades

// Receives:    NONE
// Returns:     NONE

  
  int numA = 0;
  int numB = 0;
  int numC = 0;
  int numD = 0;
  int numF = 0;
  char letterGrade;

  bool moreGrades = true;  // until told otherwise, there are more grades

   while (moreGrades)
   {  cout << "Please enter a letter grade (A, B, C, D or F)\n";
      cout << "Enter a Z to exit\n";
      cin >> letterGrade;
      switch(letterGrade)
      {  case 'A':
         case 'a':
   		  	numA++;
            		break;

         case 'B':
         case 'b':
   		  	numB++;
            		break;

         case 'C':
         case 'c':
   		  	numC++;
            		break;

         case 'D':
         case 'd':
   		  	numD++;
            		break;

         case 'F':
         case 'f':
   		  	numF++;
           		 break;

         case 'Z':
         case 'z':
   		  	cout << "Do you really want to quit\n";
            		char answer;
            		cin >> answer;
         		if ((answer == 'Y') || (answer == 'y'))
           		{	moreGrades = false;
            		}

         default:
         		cout << "An invalid letter was entered\n\n";
      } // switch
   }  // while
cout << "                             Grade Count\n";
cout << "A's \t\tB's \t\tC's \t\tD's \t\tF's";
cout << "\t\t" << numA << "\t\t" << numB << "\t\t" << numC
		  << "\t\t" << numD << "\t\t" << numF;
}
If the use of the boolean variable here gave you trouble, you might want to review a simpler example or view the material in "The Story of C++"

The second example translates numeric grades into letter grades

// A program to demonstrate the use of the switch statement by translating
// a numeric grade into a letter grade.
// The code assumes a valid grade between 0 and 100 is entered.
// The code in the switch statement here would most likely be written as a
// function in other programs
// File: ch6xmpl4.cpp

#include 

void main()
{  int numericGrade;
  char letterGrade;

   cout << "Please enter a numeric grade\n";
   cin >> numericGrade;

   numericGrade = numericGrade / 10; // reduce the grade to a value between
                                      // 0 and 10
   switch(numericGrade)
   {	case 10:
   	case 9:
         	letterGrade = 'A';
                break;

        case 8:
                 letterGrade = 'B';
                 break;

        case 7:
                letterGrade = 'C';
                break;

        case 6:
                letterGrade = 'D';
                break;

        case 5:
        case 4:
        case 3:
        case 2:
        case 1:
        case 0:
                letterGrade = 'F';
   }
   cout << "The letter grade is " << letterGrade;
}
The switch statement in "The Story of C++"