How to Use Boolean operators?

Rather than examine a single Boolean expression, your program may need to examine two or more Boolean expressions. For example, the following program checks the Boolean expression (Salary > 500) and (Bribes > 700). Only if both Boolean expressions are true does the program print You don’t need to pay any taxes., as the following Liberty BASIC program shows:


PROMPT “How much money did you make”; Salary
PROMPT “How much money did you donate to political
           candidates”; Bribes
IF (Salary > 500) THEN
 IF (Bribes > 700) THEN
   PRINT “You don’t need to pay any taxes.”
 END IF
END IF
END

Rather than force the computer to evaluate Boolean expressions one at a time, you can get the computer to evaluate multiple Boolean expressions by using Boolean operators. A Boolean operator does nothing more than connect two or more Boolean expressions to represent a True or a False value.

The two most common Boolean operators are

  • Image from book AND

  • Image from book OR

The AND operator

The AND operator links two Boolean expressions. By rewriting the previous program using the AND operator, you can simplify the program as follows:


PROMPT “How much money did you make?”; Salary
PROMPT “How much did you donate to politicians?”; Bribes
IF (Salary > 500) AND (Bribes > 700) THEN
 PRINT “You don’t need to pay any taxes.”
END IF
END

In this case, the program prints the message You don’t need to pay any taxes only if both (Salary > 500) and (Bribes > 700) are true. If either Boolean expression is false, this program doesn’t print anything.

Run the preceding program and use the values in Table 9-2 to see what happens.

Value of Salary

Value of Bribes

What the Program Does

Remember The AND operator can represent a True value only if both Boolean expressions that it connects also are true.

Technical Stuff To show how the AND operator works, programmers like to draw something known as a truth table, which tells you whether two Boolean expressions that the AND operator connects represent a True or a False value. Table 9-3 is a truth table listing the values for the following two Boolean expressions:


(Boolean expression 1) AND (Boolean expression 2)

Value of (Boolean Expression 1)

Value of (Boolean Expression 2)

Value of the Entire (Boolean Expression 1) AND (Boolean Expression 2)

The equivalent Revolution program looks similar to the Liberty BASIC version. The only main differences are the commands used to ask the user to type data. In the following Revolution program, the ask command asks the user to type an answer, which gets stored temporarily in the it variable, which you must then store in a more descriptive variable:


ask “How much money did you make?”
put it into Salary
ask “How much did you donate to politicians?”
put it into Bribes
if (Salary > 500) and (Bribes > 700) then
  put “You don’t need to pay any taxes.” into message
end if

Technical Stuff In C++, the AND operator consists of a double ampersand symbol (&&), as in this example:


(Boolean expression 1) && (Boolean expression 2)

Warning! Notice that C++ uses the double ampersand to represent the Boolean AND operator, but Revolution uses the double ampersand to concatenate strings. When using different programming languages, watch out for ways that different languages use identical symbols or commands that mean entirely different things.

The following code shows the equivalent C++ program:


#include <iostream.h>
#include <stdio.h>
int main()
{
   int Salary;
   int Bribes;
   cout << “How much money did you make? “;
   cin >> Salary;
   cout << “How much did you donate to politicians? “;
   cin >> Bribes;
   if ((Salary > 500) && (Bribes > 700))
   {
   cout << “You don’t need to pay any taxes.”;
   }
   cout << “\nPress ENTER to continue…” << endl;
   getchar();
   return 0;
}

The OR operator

The OR operator links two Boolean expressions but produces a True value if either Boolean expression represents a True value. For an example of how this operator works, run the following program:


PROMPT “How far can you throw a football”; Football
PROMPT “What is your IQ”; IQ
IF (Football > 50) OR (IQ <= 45) THEN
 PRINT “You have what it takes to become a professional!”
END IF
END

In this case, the program prints the message You have what it takes to become a professional! if either (Football > 50) is true or (IQ <= 45) is true. Only if both Boolean expressions are false does the program refuse to print anything.

Run the preceding program and use the values in Table 9-4 to see what happens.

Value of Football

Value of IQ

What the Program Does

Remember The OR operator can represent a False value only if both Boolean expressions that it connects are false.

Table 9-5 is a truth table to show how the OR operator works for Boolean expressions in the following example:


(Boolean expression 1) OR (Boolean expression 2)

Value of (Boolean Expression 1)

Value of (Boolean Expression 2)

Value of the Entire (Boolean Expression 1) OR (Boolean Expression 2)

Technical Stuff In C++, the OR operator consists of a double vertical line symbol (||), as shown here:


(Boolean expression 1) || (Boolean expression 2)

Here’s the equivalent C++ program:


#include <iostream.h>
#include <stdio.h>
int main()
{
   int Football;
   int IQ;
   cout << “How far can you throw a football? “;
   cin >> Football;
   cout << “What is your IQ? “;
   cin >> IQ;
   if ((Football > 50) || (IQ <= 45))
   {
   cout << “You have what it takes to become a
           professional!”;
   }
   cout << “\nPress ENTER to continue…” << endl;
   getchar();
   return 0;
   }

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply



eXTReMe Tracker