Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Programming for Dummies 2004.pdf
Скачиваний:
109
Добавлен:
17.08.2013
Размер:
8.05 Mб
Скачать

Chapter 9: Making Decisions with Control Statements 119

The NOT operator

The NOT operator affects a single Boolean expression. If the Boolean expression is true, the NOT operator makes it false. If the Boolean expression is false, the NOT operator makes it true. Essentially, the NOT operator converts a Boolean expression to its opposite value.

The following Boolean expression, for example, is true:

(4 < 54)

But this Boolean expression is false:

NOT(4 < 54)

If you want to assign a value of false to the Guilty variable, use the NOT operator as follows:

Guilty = NOT(4 < 54)

This statement tells the computer, “The Boolean expression of (4 < 54) is true. But the NOT operator turns a true value to false, so the value of the Guilty variable is false.” To get a better idea how this works, run the following program:

Guilty = NOT(4 < 54) ‘ The value of Guilty is false

IF Guilty THEN

PRINT “This sentence never prints out.”

ELSE

PRINT “The defendant is not guilty because he’s rich.”

END IF

END

Each time that you run the preceding program, it prints the message, The defendant is not guilty because he’s rich.

Exploring IF THEN Statements

The most common way to control which instruction the computer follows next is to use the IF THEN statement. This statement checks whether or not a certain condition is true. If so, it tells the computer to follow one or more instructions.

120 Part II: Learning Programming with Liberty BASIC

The IF THEN statement looks as follows:

IF (Boolean expression) THEN

‘ Follow one or more instructions listed here END IF

For an example of how this statement works, type and run the following program:

PROMPT “Do you eat cow lips? (Type Y for Yes or N for No)”;

Answer$

IF (Answer$ = “Y”) THEN

PRINT “I have a nice hot dog you might like then.”

END IF

END

Only if you type Y (using uppercase) does the program print the message, I have a nice hot dog you might like then.

If you want the computer to follow exactly one instruction following an IF THEN statement (such as the PRINT instruction in the preceding example), you can shorten the IF THEN statement to the following:

PROMPT “Do you eat cow lips? (Type Y for Yes or N for No)”; Answer$

IF (Answer$ = “Y”) THEN PRINT “I have a nice hot dog you might like then.”

END

If you want the computer to follow two or more instructions following an IF THEN statement, you must enclose them with the END IF line, as follows:

PROMPT “How many cats do you own”; Answer

IF (Answer >= 1) THEN

PRINT “You have my sympathies.”

PRINT “Have you ever thought of getting”

PRINT “your head examined real soon?”

END IF

END

IF THEN ELSE statements

The IF THEN statement tells the computer to follow one or more instructions only if a certain condition is true. If that condition is not true, the computer ignores all the instructions trapped inside the IF THEN statement.