How to Use Boolean operators?

June 14th, 2008 yudi Posted in Dealing with Branching Statements No Comments »

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;
   }
AddThis Social Bookmark Button

How to use variables in Boolean expressions?

June 14th, 2008 yudi Posted in Dealing with Branching Statements No Comments »

The sample program in the preceding section uses a Boolean expression (4 < 54) that’s fairly useless because it’s always true. Every time that you run the program, it just prints the message Slap him on the wrist and let him go.

For greater flexibility, Boolean expressions usually compare two variables or one variable to a fixed value, as in the following examples:


(MyIQ >= AnotherIQ)
(Taxes < 100000)

The value of the first Boolean expression (MyIQ < AnotherIQ) depends on the value of the two variables MyIQ and AnotherIQ. Run the following Liberty BASIC program to see how it follows a different set of instructions, depending on the value of the MyIQ and AnotherIQ variables:


PROMPT “What is your IQ”; MyIQ
PROMPT “What is the IQ of another person”; AnotherIQ
IF (MyIQ >= AnotherIQ) THEN
 PRINT “I’m smarter than you are.”
END IF
IF (MyIQ < AnotherIQ) THEN
 PRINT “You have a higher IQ to make up for your lack of
           common sense.”
END IF
END

If you run this program and type different values for MyIQ and AnotherIQ, the program behaves in two possible ways: printing on-screen either I’m smarter than you are. or You have a higher IQ to make up for your lack of common sense.

Remember By using variables in Boolean expressions, you give your program the flexibility it needs to handle different data.

AddThis Social Bookmark Button

How to store Boolean expressions in variables?

June 14th, 2008 yudi Posted in Dealing with Branching Statements No Comments »

Many programming languages let you define a variable as a Boolean data type.

In REALbasic, you can define a variable as a Boolean data type like this:


Dim Guilty As Boolean

In C++, you can define a variable as a Boolean data type like this:


bool Guilty;

Remember You can just stuff a Boolean expression into a variable in Liberty BASIC and Revolution because neither of these languages forces you to declare your variables ahead of time like C++ or REALbasic.

You can then assign a Boolean expression to a variable in the following way:


Guilty = (4 < 54)

This example assigns the value of True to the variable Guilty. The preceding statement tells the computer, “The Boolean expression of (4 < 54) is true. Thus the value of the Guilty variable is True.”

To see how a variable can hold a Boolean value, try the following Liberty BASIC program:


Guilty = (4 < 54)
IF Guilty THEN
 PRINT “Slap him on the wrist and let him go.”
END IF
END

Each time that you run the preceding program, it prints the message Slap him on the wrist and let him go.

Here’s what the equivalent program looks like in Revolution:


put 4 < 54 into Guilty
if Guilty then
  put “Slap him on the wrist and let him go.” into message
end if
AddThis Social Bookmark Button

How to Use Boolean Expressions

June 14th, 2008 yudi Posted in Dealing with Branching Statements No Comments »

To make any decision, you first need to ask a question such as, “Do I feel like eating a hamburger?” If the answer is yes, you go to a restaurant that serves hamburgers. If the answer is no, you eat something else.

Computers work in a similar way. Although people can ask questions, computers check something called a Boolean expression. A Boolean expression is a computer’s way of asking a Yes or No question.

Technical Stuff Boolean expressions are part of Boolean algebra, which was named after a man by the name of George Boole. (If you study hard and create your own branch of mathematics, someone may name it after you, too.)

Boolean expressions compare two values. An expression can be either true or false, as shown in Table 9-1.

Boolean Expression

What It Means

Boolean Value

Technical Suff Symbols such as <, >, =, <=, >=, and <> are known as relational operators.

Warning! In most programming languages, you can use the equal sign to test a Boolean expression such as (MyIQ = 120). However in C++ (and other C-derivative languages like Perl), you must use the double equal sign symbol (==) to test for equality such as (MyIQ == 120). If you use a single equal sign, your C++ program won’t work right.

A Boolean expression isn’t very useful by itself, so programmers often use it in branching statements to determine which set (or branch) of instructions the computers should follow at any given time.

The simplest type of a branching statement is called an IF-THEN statement, which offers one alternative set of instructions to follow if a certain Boolean expression is true. An IF-THEN statement typically looks like this:


IF (Boolean expression is True) THEN
   One or more instructions
END IF

Technical Stuff In C++, an IF-THEN statement looks slightly different:


if (Boolean expression is True)
   {
           One or more instructions
   }

In C++, the two big differences are that the word THEN is not used and that you must enclose any instructions to follow within curly brackets.

To see how IF-THEN statements work, look at the following Liberty BASIC program:


IF (4 < 54) THEN                                  ?1
 PRINT “This prints on-screen.”                   ?2
END IF                                            ?3
END                                               ?4

This Liberty BASIC program tells the computer to do the following:

  • ?1 Tells the computer to evaluate the Boolean expression (4 < 54). Because this is true (4 is less than 54), the computer can proceed to the second line.

  • ?2 Tells the computer to print the message This prints on-screen.

  • ?3 Identifies the end of the IF-THEN statement.

  • ?4 Tells the computer that the program is at an end.

To see how IF-THEN statements work in C++, look over the following equivalent program:


if (4 < 54)
   {
   cout << “This prints out on-screen.”;
   }
AddThis Social Bookmark Button




eXTReMe Tracker