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

Chapter 9

Making Decisions with

Control Statements

In This Chapter

Understanding Boolean expressions

Using IF THEN statements

Using Select Case statements

The whole purpose of a program is to make the computer behave in a certain way. The most primitive programs act exactly the same way each

time that you run them, such as displaying, “Hello, world!” or the name of your cat on-screen.

Such primitive programs may work fine for learning to program, but they’re relatively useless otherwise, because most programs need to accept data and modify their behavior based on any data that they receive.

Many banks, for example, use a computer to analyze risks before they loan money. If you have an income over a certain amount and don’t have a history of declaring bankruptcy every two years, the bank’s computer may approve your loan before it approves a loan for someone who has no income or assets. In this case, the bank’s computer program must accept data and decide what to do based on that data.

Each time that you feed the program different data, the program may spit out a different answer. To find out how to give your program the capability to make decisions, you must use something mysterious known as control statements.

Using Boolean Expressions

Whenever you make a decision, such as what to eat for dinner, you ask yourself a question such as, “Do I feel like eating a hamburger?” If the answer is yes, you go to a restaurant that serves hamburgers.

112 Part II: Learning Programming with Liberty BASIC

Computers work in a similar way. Although people can ask questions, computers check a Boolean expression. A Boolean expression is anything that represents one of two values, such as true/false or zero/nonzero.

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.) The simplest Boolean expression is one that compares two values, as shown in Table 9-1.

Table 9-1

Evaluating Boolean Expressions

Boolean Expression

What It Means

Boolean Value

4

< 54

4 is less than 54

True

 

 

 

 

4

> 54

4 is greater than 54

False

 

 

 

 

4

= 54

4 is equal to 54

False

 

 

 

 

4

<= 54

4 is less than or equal to 54

True

 

 

 

 

4

>= 54

4 is greater than or equal to 54

False

 

 

 

 

4

<> 54

4 is not equal to 54

True

 

 

 

 

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

Try running the following program to guess which sentence the program prints out:

IF (4 < 54) THEN

PRINT “This prints out on-screen.”

ELSE

PRINT “Why did you pick this sentence?”

END IF

END

This BASIC program tells the computer to do the following:

1.The first line tells the computer to evaluate the Boolean expression (4 < 54). Because this is true, the computer can proceed to the second line.

2.The second line tells the computer to print the message, This prints out on-screen. Then the computer skips over the third and fourth lines to the fifth line of the program.

3.The third line tells the computer, “In case the Boolean expression (4 < 54) happens to prove false, proceed to the fourth line of the program.”

Chapter 9: Making Decisions with Control Statements 113

4.The fourth line tells the computer to print the message, Why did you pick this sentence? Because the Boolean expression (4 < 54) is never false, the third and fourth lines never run.

5.The fifth line simply identifies the end of the IF THEN ELSE statement (which you find out more about in the section “IF THEN ELSE statements,” later in this chapter).

6.The sixth line tells the computer that the program is at an end.

You can assign a variable to a Boolean expression 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.”

Liberty BASIC doesn’t really know the difference between true and false. Because Liberty BASIC can’t assign a true value to a Boolean expression, Liberty BASIC just assigns the number –1 to the Boolean expression, which represents the value true. If Liberty BASIC wants to assign a false value to a Boolean expression, it assigns the number 0 to the Boolean expression.

Try running the following program:

Guilty = (4 < 54)

IF Guilty THEN

PRINT “Slap him on the wrist and let him go.”

ELSE

PRINT “This sentence never prints out.”

END IF

END

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

Using 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 out 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)

114 Part II: Learning Programming with Liberty BASIC

The value of the first Boolean expression (MyIQ < AnotherIQ) depends on the value of the two variables MyIQ and AnotherIQ. Run the following 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.” ELSE

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.

If you use variables in Boolean expressions, you give the computer more flexibility in making decisions on its own.

Using Boolean operators

Examining a single Boolean expression at a time can prove a bit cumbersome, as the following example 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

The only time that this program prints the message, You don’t need to pay any taxes on-screen is if both Boolean expressions (Salary > 500) and (Bribes > 700) are true.

Rather than force the computer to evaluate Boolean expressions one at a time, you can get the computer to evaluate multiple Boolean expressions by

Chapter 9: Making Decisions with Control Statements 115

using Boolean operators. A Boolean operator does nothing more than connect two or more Boolean expressions to represent a true or a false value.

Every programming language uses the following four Boolean operators:

AND

OR

XOR

NOT

The AND operator

The AND operator links two Boolean expressions. You can, for example, rewrite the program in the preceding section as follows by using the Boolean operator AND:

PROMPT “How much money did you make”; Salary PROMPT “How much money did you donate to political

candidates”; 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 out the message, You don’t need to pay any taxes only if both (Salary > 500) is true and (Bribes > 700) is 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.

Table 9-2 Different Values Determine How the AND Operator Works

Value of Salary

Value of Bribes

What the Program Does

100

100

Nothing

 

 

 

900

100

Nothing

 

 

 

100

900

Nothing

 

 

 

900

900

Prints the message

 

 

 

116 Part II: Learning Programming with Liberty BASIC

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

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)

Table 9-3

The Truth Table for the AND Operator

Value of

Value of

Value of the Entire

(Boolean Expression 1)

(Boolean Expression 2)

(Boolean Expression 1)

 

 

AND (Boolean Expression 2)

False

False

False

 

 

 

True

False

False

 

 

 

False

True

False

 

 

 

True

True

True

 

 

 

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 athlete!”

END IF END

In this case, the program prints the message, You have what it takes to become a professional athlete! 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.

Chapter 9: Making Decisions with Control Statements 117

Table 9-4 Different Values Determine How the OR Operator Works

Value of Football

Value of IQ

What the Program Does

5

70

Nothing

 

 

 

70

70

Prints the message

 

 

 

5

5

Prints the message

 

 

 

70

5

Prints the message

 

 

 

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

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

(Boolean expression 1) OR (Boolean expression 2)

Table 9-5

The Truth Table for the OR Operator

Value of

Value of

Value of the Entire

(Boolean Expression 1)

(Boolean Expression 2)

(Boolean Expression 1) OR

 

 

(Boolean Expression 2)

False

False

False

 

 

 

True

False

True

 

 

 

False

True

True

 

 

 

True

True

True

 

 

 

The XOR operator

The XOR operator links two Boolean expressions but produces a true value only if one Boolean expression represents a true value and the other Boolean expression represents a false value. For an example of how this operator works, run the following program:

PROMPT “Is your spouse around (Type 1 for Yes or 0 for No)”; SpouseHere

PROMPT “Is your best friend around (Type 1 for Yes or 0 for No)”; BestfriendHere

IF (SpouseHere = 0) XOR (BestfriendHere = 0) THEN

PRINT “You won’t be lonely tonight!”

END IF

END

118 Part II: Learning Programming with Liberty BASIC

The only two times that this program prints the message, You won’t be lonely tonight! is if your spouse is around (SpouseHere = 1) but your best friend isn’t (BestfriendHere = 0) or if your best friend is around (Bestfriendhere = 1) but your spouse isn’t (SpouseHere = 0).

If your spouse is around (SpouseHere = 1) and your best friend is around (BestfriendHere = 1), nothing happens. Similarly, if your spouse is gone (SpouseHere = 0) and your best friend is gone (BestfriendHere = 0), nothing happens.

Run the preceding program and use the values in Table 9-6 to see what happens:

Table 9-6 Different Values Determine How the XOR Operator Works

Value of SpouseHere

Value of BestfriendHere

What the Program Does

0

0

Nothing

 

 

 

1

0

Prints the message

 

 

 

0

1

Prints the message

 

 

 

1

1

Nothing

 

 

 

The XOR operator can represent a false value only if both Boolean expressions that it connects are false or if both Boolean expressions are true.

Table 9-7 is the truth table to show how the XOR operator works for the following Boolean expressions:

(Boolean expression 1) XOR (Boolean expression 2)

Table 9-7

The Truth Table for the XOR Operator

Value of

Value of

Value of the Entire

(Boolean Expression 1)

(Boolean Expression 2) (Boolean Expression 1) XOR

 

 

(Boolean Expression 2)

False

False

False

 

 

 

True

False

True

 

 

 

False

True

True

 

 

 

True

True

False