Examples of the Logical Operators

Here are a set of examples involving the logical operators, &&, ||, and !
The first one counts the number of times the letter grades (A, B, C, D, and F) are entered. Study carefully the way the logical operators are used and then read the discussion below the program.
// A program to count the number of A's, B's etc entered as grades
// Uses a sentinel value of 'Z'
// file: ch3xmpl5.cpp

#include <iostream.h>

void main()
{
// Purpose:     A program to 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;

	cout << "Please enter a letter grade (A, B, C, D or F)\n";
	cout << "Enter a Z to exit\n";
	cin >> letterGrade;

	while ((letterGrade != 'Z') && (letterGrade != 'z'))
	{	if ((letterGrade == 'A') || (letterGrade == 'a'))
		{	numA++;
		}
		else // can't be an A
		{  if ((letterGrade == 'B') || (letterGrade == 'b'))
			{	numB++;
			}
			else // can't be an A  or a B
			{  if ((letterGrade == 'C') || (letterGrade == 'c'))
				{	numC++;
				}
				else  // can't be an A  or a B or a C
				{  if ((letterGrade == 'D') || (letterGrade == 'd'))
				{	numD++;
				}
					else  // can't be an A  or a B or a C or a D
					{	numF++;   // assumes that only correct letters are entered
					}
				}
			}
		}
	cout << "Please enter a letter grade (A, B, C, D or F)\n";
	cout << "Enter a Z to exit\n";
	cin >> letterGrade;
	}
	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;
}
The 'if' statements are relatively straight forward. For example, the first one says, "If the grade entered equals 'A' or 'a', add 1 to the number of A's." The end of loop test is a bit more complex. The loop should end when either a 'Z' or a 'z' is entered. Notice that the English uses an 'or' while the program uses the symbol for 'and'. The reason for this is that the English describes when the loop will end but the 'while' test determines if the loop will continue. In other words, the program tests for just the opposite of what the English descibes. You need to be careful of this when writing code. If you are told when a loop should end but the code itself checks to see if the loop should continue, you need to change the logical operator you are using.

Mathematically this can be explained using what is called DeMorgan's Law:

"x == y || x == z" is equivalent to "x != y && x != z"

Other points worth noting:

  1. The way the if...else statements are indented. This makes the code easier to read

  2. The use of nested 'if' statements. The code could have been written without them as in:
      if ((letterGrade == 'A') || (letterGrade == 'a'))
      {   numA++;
      }
      if ((letterGrade == 'B') || (letterGrade == 'b'))
      {   numB++;
      }
      etc.
    However, this would mean that even if the program determines that an 'A' was entered, it still would need to test to see if the letter entered was one of the other possible letters. The use of 'else' avoids this waste of CPU time.

  3. In the output the symbol '\t' is used. Just as '\n' stands for a single symbol that indicates the end of a line, so '\t' is a single symbol indicating a tab mark.
Discussion of the Logical Operators in "The Story of C++"
Logical Operators and the While Loop