How to use variables in Boolean expressions?

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.


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