How to Use Boolean Expressions

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.”;
   }

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