Logical Operators


C++ has logical operators that enable you to combine comparisons in one if or else if statement. Table 6-1 lists the logical operators supported by C++ and describes what each does.

Table 6-1: Logical Operators

Operator

Name

What It Does

&&

And

Connects two relational expressions. Both expressions must be true for the overall expression to be true.

Or

Connects two relational expressions. If either expression is true, the overall expression is true.

!

Not

Reverses the truth of an expression, making a true expression false, and a false expression true.

The && Operator

The && operator also is known as the logical And operator. It is a binary operator; it takes two Boolean expressions as operands. It returns true only if both expressions are true. If either expression is false, the overall expression is false. Of course, if both expressions are false, the overall expression is false. Table 6-2 illustrates this.

Table 6-2: The Logical And Operator

Expression #1

Expression #2

Expression #1 && Expression #2

true

true

true

true

false

false

false

true

false

false

false

false

The following program shows the use of the logical And operator in determining whether the user is eligible to vote, the criteria being that the user must be at least 18 years old and a citizen.

 #include <iostream> using namespace std; int main(void) {  int age;  char choice;  bool citizen;  cout << "Enter your age: ";  cin >> age;  cout << "Are you a citizen (Y/N): ";  cin >> choice;  if (choice == 'Y')  citizen = true;  else  citizen = false;  if (age >= 18 && citizen == true)  cout << "You are eligible to vote";  else  cout << "You are not eligible to vote";  return 0; } 

The following are several sample runs, separated by ===:

 Enter your age: 18 Are you a citizen (Y/N): Y You are eligible to vote === Enter your age: 18 Are you a citizen (Y/N): N You are not eligible to vote === Enter your age: 17 Are you a citizen (Y/N): Y You are not eligible to vote === Enter your age: 17 Are you a citizen (Y/N): N You are not eligible to vote 

The part of the program that uses the logical And operator is

 if (age >= 18 && citizen == true)  cout << "You are eligible to vote";  else  cout << "You are not eligible to vote"; 

The comparison age > = 18 is referred to as the left part of the expression since it is to the left of the logical And operator. Similarly, the comparison citizen == true is referred to as the right part of the expression because it is to the right of the logical And operator.

If the user s age is at least 18 years, then the program makes the second comparison, whether the user is a citizen. If the user s age is not at least 18 years of age, the second comparison is not even made before the else part is executed. The reason is to avoid wasting CPU time, since if the left expression is false, the overall expression is false regardless of the result of the evaluation of the right expression.

Because the second comparison of whether the user is a citizen is made only if the user s age is at least 18, the flowchart in Figure 6-1 of this program using nested if statements also applies to this program using the logical And operator.

The Operator

The operator is also known as the logical Or operator. Like the logical And operator, the logical Or operator also is a binary operator, taking two Boolean expressions as operands. It returns true if either expression is true. It returns false only if both expressions are false. Of course, if both expressions are true, the overall expression is true. Table 6-3 illustrates this.

Table 6-3: The Logical Or Operator

Expression #1

Expression #2

Expression #1 Expression #2

true

true

true

true

false

true

false

true

true

false

false

false

The following program shows the use of the logical Or operator in determining whether you get into a movie free, the criteria being that the user must be either no more than 12 or at least 65 years old.

 #include <iostream> using namespace std; int main(void) {  int age;  cout << "Enter your age: ";  cin >> age;  if (age <= 12  age >= 65)  cout << "Admission is free";  else  cout << "You have to pay";  return 0; } 

The following shows several sample runs:

 Enter your age: 12 Admission is free === Enter your age: 18 You have to pay === Enter your age: 65 Admission is free 

The part of the program that uses the logical Or operator is

 if (age <= 12  age >= 65)  cout << "Admission is free";  else  cout << "You have to pay";  return 0; 

As with the logical And operator, the comparison age < = 12 is referred to as the left part of the expression and the comparison age > = 65 is referred to as the right part of the expression.

If the user s age is over 12 years, then the program makes the second comparison, whether the user is at least 65 years of age. If the user is no more than 12 years of age, the second comparison is not even made before the else part is executed. The reason, as with the logical And operator, once again is to avoid wasting CPU time, since if the left expression is true, the overall expression is true regardless of the result of the evaluation of the right expression.

Because the second comparison of whether the user is at least 65 years old is made only if the user s age is over 12, the flowchart in Figure 6-2 of this program using nested if statements also applies to this program using the logical Or operator.

The ! Operator

The ! operator also is known as the logical Not operator. My daughters have been using the logical Not operator for years, telling me Dad, you look just like Tom Cruise not!

The logical Not operator inverts the value of the Boolean expression, returning false if the Boolean expression is true, and true if the Boolean expression is false. Table 6-4 illustrates this.

Table 6-4: The Logical Not Operator

Expression

!Expression

true

true

false

true

Unlike the logical And and Or operators, the logical Not operator is a unary operator; it takes only one Boolean expression, not two.

The following program shows the use of the logical Not operator, combined with the logical And operator, in determining whether you get into a movie for free.

 #include <iostream> using namespace std; int main(void) {  int age;  cout << "Enter your age: ";  cin >> age;  if (!(age > 12 && age < 65))  cout << "Admission is free";  else  cout << "You have to pay";  return 0; } 

This program is almost identical to the one used to illustrate the logical Or operator. The only difference is that the statement

 if (age <= 12  age >= 65) 

is replaced by the statement

 if (!(age > 12 && age < 65)) 
Note  

This change is an illustration of DeMorgan s law, which is a rule of inference pertaining to the logical And, Or, and Not operators that are used to distribute a negative to a conjunction or disjunction. In this book, it is only referred to and not covered, but in case you hear DeMorgan s law mentioned in a programming class or another book, you heard it here first!

The Not operator permits you to state a Boolean expression a different way that may be more intuitive for you. In this example, expressing the condition for free admission as being that the age is not between 13 and 64 may be more intuitive than expressing that condition as being that the age is either no more than 12 or 65 or over.

Precedence

Table 6-5 lists precedence, from highest to lowest , among logical operators and between them and the relational operators.

Table 6-5: The Precedence of Logical and Relational Operators

Operator (from highest to lowest)

!

Relational operators (>, >=, <, <=, ==. !=)

&&

Precedence and the Logical Not Operator

Since the logical Not operator has a higher precedence than the relational operators, the program used to illustrate the logical Not operator uses an extra set of parentheses.

 if (!(age > 12 && age < 65)) 

Had the extra set of parentheses been omitted as follows , the result would always be that the user has to pay. Thus, admission would never be free regardless of the age.

 if (!age > 12 && age < 65) 

The reason why the user always has to pay regardless of age is that since the logical Not operator has a higher precedence than the relational operators, the logical Not operator operates on age, not the expression age > 12 && age < 65. If age is non-zero , then !age is zero. Since 0 is not greater than 12, the left part of the logical And expression is false, so the overall expression is false.

The result of the user always having to pay regardless of age is the same even if age is zero. If age is zero, then !age is logical true, the integer equivalent of which usually is 1. Since 1 is not greater than 12, once again the left part of the logical And expression is false, so the overall expression is false.

Precedence and the Logical And and Or Operators

In contrast to the logical Not operator, the logical And and Or operators rank lower in precedence than the relational operators. Therefore, parentheses normally are not necessary to separate the logical And and Not operators from the relational operators. For example, the following two statements (the first taken from the program that illustrated the logical And operator) are equivalent.

 if (age >= 18 && citizen == true)  if ((age >= 18) && (citizen == true)) 

However, parentheses are necessary when logical And and Or operators are used together in one statement and you want the Or done before the And since the logical And operator has higher precedence than the logical Or operator. This issue often arises when you have more than two Boolean expressions.

For example, assume the voting rules were changed so legal residents (represented by the Boolean variable resident having a value of true) as well as citizens who are at least 18 years old could vote. Given that assumption, the statement

 if (resident == true  citizen == true && age >= 18) 

would be the same as the following since the logical And operator has higher precedence than the logical Or operator.

 if (resident == true  (citizen == true && age >= 18)) 

In this expression, a resident under 18 years old would be able to vote. The reason is that even if the expression ( citizen == true && age > = 18 ) is false, as long as resident is true, the overall expression is true, since with a logical Or operator only one of the two Boolean expressions needs to be true for the overall expression to be true.

A resident under 18 years old being able to vote is not a correct result for this program. To avoid this logic error, parentheses would be necessary, so the logical Or operation is performed first.

 if ((resident == true  citizen == true) && age >= 18) 



C++ Demystified(c) A Self-Teaching Guide
C++ Demystified(c) A Self-Teaching Guide
ISBN: 72253703
EAN: N/A
Year: 2006
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net