Section 5 - Logical Operators

3B.5.1 Logical Operators

If the user types a capital Y instead of a lower-case y in response to:

cout << "Are you in-state? ";

then the test:

if (state == 'y')

would be considered false. We want to allow for either a Y or a y. This can be done by combining the two relationals:

(state == 'y')

and

(state == 'Y')

using the logical or, ||, like so:

if ( (state == 'y') || (state == 'Y') )

(To type the || you would hit the vertical pipe bar, |, twice.)

Now the entire expression will evaluate to true if either state=='y' OR state=='Y'. The parentheses are not necessary in this expression, but they are a good idea: C++ has lots of operators whose precedence is counterintuitive. Instead of trying to remember which they are, just over-do the parentheses.

3B.5.2 Truth Tables for the Three Principal Logical Operators

The or, || operator, and, &&, operator and not, !, operator are defined like so:

OR
a b a||b
true true true
false true true
true false true
false false false

AND
a b a&&b
true true true
false true false
true false false
false false false

NOT
a !a
true false
false true

With these statements, you can build rather complex logical expressions. You will see and create many, and there are lots of examples in the text. For now the important thing to remember is that whenever a logical expression is called for, it evaluates to either true or false, and this is interpreted by the larger statement (the if statement, e.g.) and a certain action is taken as a result.

3B.5.3 An Example

Here is a (silly but illustrative) state tax example.

Pay attention to:

  1. Indentation.
  2. User Input
  3. Conversion of string to char
  4. Logical expression.
#include <iostream>
#include <string>
using namespace std;

int main()
{
char inState;
double price, tax;
string tempString;

// find out if we should charge state tax
cout << "Are you in-state? ";
getline(cin, tempString);

// copy the first char only
inState = tempString[0];

// Get the amount
cout << "What is the amount? ";
cin >> price;

if (inState == 'y' || inState == 'Y')
{
if (price < .12)
tax = 0;
else if (price < .24)
tax = .01;
else if (price < .36)
tax = .02;
else
tax = price * .083;
}
else
tax = 0;

cout << "The amount of tax is " << tax << endl;
}

3B.5.4 An Example with Strings

If we want to test whether the user typed a "yes" or "no" in response to a question, one easy way to do it (as we have already demonstrated) is to pluck the first character from the answer and test it against 'y' or 'n'.  This gives the user a little leeway in his spelling skills.  If he messes up the rest of the word, we will forgive him. As long as he types the first letter correctly, we'll act on that, alone.

What if we want to make sure the user types some word or phrase, exactly?  We have to compare the entire string.  The way to do this in C++ is using the == operator. It is very simple:

if ( userString == "Yes" )
cout << "You typed Yes";

We can turn this into a complete program that demands that the user type in either the word "Yes" or "yes", but nothing else.

#include <iostream>
#include <string>
using namespace std;

int  main()
{
// user-provided values
string userString;

// grab the string
cout << "Type in a word: ";
getline(cin, userString);

if ( userString == "Yes"  || userString == "yes" )
cout << "You typed an acceptable form of \"yes\".";
else
cout << "You didn't type an acceptable form of \"yes\".";
cout << endl;

return 0;
}

/* ------------------- Sample Run #1 --------------------
Type in a word: YES
You didn't type an acceptable form of "yes".
Press any key to continue . . .
---------------------- End Sample Run #1 ---------------- */


/* ------------------- Sample Run #2 --------------------
Type in a word: Yes
You typed an acceptable form of "yes".
Press any key to continue . . .
---------------------- End Sample Run #2 ---------------- */

Notice that we have also demonstrated a technique for including a double quote mark, ", inside a string literal by preceding it with a so-called escape character, otherwise known as the backslash, \.

Warning About Logical Operators

Be careful to use double && and ||, not single & and |.  In C++ the single operators mean something entirely different than the double operators and will usually result in logical errors (that means bugs).