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

Chapter 9: Making Decisions with Control Statements 121

The IF THEN ELSE statement is slightly different because it tells the computer to follow one set of instructions in case a condition is true and a different set of instructions if the condition is false. The IF THEN ELSE statement looks as follows:

IF (Boolean expression) THEN

Follow one or more instructions listed here ELSE

If the condition is false, then follow these

instructions instead

END IF

For an example of how this statement works, run the following program and see what happens:

PROMPT “How long were you in medical school”; Answer

IF (Answer > 4) THEN

PRINT “Congratulations! You should be able to”

PRINT “play a good game of golf in no time.”

ELSE

PRINT “You may not have been in medical school for”

PRINT “very long, but at least you should know”

PRINT “how to put on a white lab coat.”

END IF

END

Unlike the IF THEN statement, the IF THEN ELSE statement always forces the computer to follow one set of instructions no matter what. In this program, if the answer is greater than four, the computer prints out, Congratulations! You should be able to play a good game of golf in no time.

If the answer isn’t greater than four, the computer prints out, You may not have been in medical school for very long, but at least you should know how to put on a white lab coat.

The IF THEN ELSE statement always makes the computer follow one set of instructions. If you use the ordinary IF THEN statement, the computer may or may not follow one set of instructions.

Working with SELECT CASE Statements

Listing multiple conditions in an IF THEN ELSE statements can prove tedious and messy, as the following example shows:

122 Part II: Learning Programming with Liberty BASIC

PROMPT “How old are you”; Answer

IF (Answer = 21) THEN

PRINT “Congratulations! You may be able to rent a”

PRINT “car in some states.”

END IF

IF (Answer = 20) THEN

PRINT “You can’t rent a car, but you’re pre-approved”

PRINT “for 20 different credit cards.”

END IF

IF (Answer = 19) THEN

PRINT “You’re still officially a teenager.”

END IF

IF (Answer = 18) THEN

PRINT “You’re old enough to join the military and”

PRINT “fire an automatic rifle, but you still can’t”

PRINT “buy beer legally. Figure that one out.”

END IF

IF (Answer = 17) THEN

PRINT “You can see R-rated movies on”

PRINT “your own (but you’ve probably done that for years).”

END IF

END

As an alternative to multiple IF THEN ELSE statements, many programming languages offer a SELECT CASE statement, which looks as follows:

SELECT CASE Variable

CASE Value1

Follow these instructions if the Variable = Value1 CASE Value2

Follow these instructions if the Variable = Value2 END SELECT

The SELECT CASE statement provides different instructions depending on the value of a particular variable. If you rewrite the preceding IF THEN ELSE statement, the program looks as follows:

PROMPT “How old are you”; Answer

SELECT CASE Answer

CASE 21

PRINT “Congratulations! You may be able to rent a”

PRINT “car in some states.”

CASE 20

PRINT “You can’t rent a car, but you’re pre-approved

PRINT “for 20 different credit cards.”

Chapter 9: Making Decisions with Control Statements 123

CASE 19

PRINT “You’re still officially a teenager.” CASE 18

PRINT “You’re old enough to join the military and” PRINT “fire an automatic rifle, but you still can’t” PRINT “buy beer legally. Figure that one out.”

CASE 17

PRINT “You can see R-rated movies on”

PRINT “your own (but you’ve probably done that for years).” END SELECT

END

If the user types 21, the program prints, Congratulations! You may be able to rent a car in some states. If the user types 19, the program prints, You’re still officially a teenager. If the user types 17, the program prints, You can see R-rated movies on your own (but you’ve probably done that for years).

Of course, if the user types any value that the SELECT CASE statement doesn’t list, such as 22 or 16, the SELECT CASE statement doesn’t run any of the instructions within its structure.

To make sure that the computer follows at least one set of instructions in a SELECT CASE statement, just add a CASE ELSE command at the very end, as follows:

PROMPT “How old are you”; Answer

SELECT CASE Answer

CASE 21

PRINT “Congratulations! You may be able to rent a”

PRINT “car in some states.”

CASE 20

PRINT “You can’t rent a car, but you’re pre-approved”

PRINT “for 20 different credit cards.”

CASE 19

PRINT “You’re still officially a teenager.”

CASE 18

PRINT “You’re old enough to join the military and” PRINT “fire an automatic rifle, but you still can’t” PRINT “buy beer legally. Figure that one out.”

CASE 17

PRINT “You can see R-rated movies on”

PRINT “your own (but you’ve probably done that for years).” CASE ELSE

PRINT “This sentence prints out if the user does NOT” PRINT “type numbers 17, 18, 19, 20, or 21.”

END SELECT END

124 Part II: Learning Programming with Liberty BASIC

If you run this program and type 21, the program prints out Congratulations! You may be able to rent a car in some states. If the user types

20, the program prints out, You can’t rent a car, but you’re preapproved for 20 different credit cards. If the user types 19, the program prints out, You’re still officially a teenager. If the user types 18, the program prints out, You’re old enough to join the military and fire an automatic rifle, but you still can’t buy beer legally. Figure that one out. If the user types 17, the program prints out, You can see R-rated movies on your own (but you’ve probably done that for years). If the user types any other number (such as 54 or 97, the program prints out, This sentence prints out if the user does NOT type numbers 17, 18, 19, 20, or 21.

Checking a range of values

You often use the SELECT CASE statement to check whether a variable happens to exactly match a specific value, such as the number 21 or the string “yes”. But sometimes you may want to run a set of instructions if a variable falls within a range of values, such as any number between 3 and 18. In that case, you must list all the possible values on a single CASE statement, as in the following example:

CASE 1, 2, 3, , 4

This checks whether a variable represents the number 1, 2, 3, or 4 as shown in the following program:

PROMPT “How many water balloons do you have? “; Answer

SELECT CASE Answer

CASE 1, 2, 3, 4

NOTICE “You need more water balloons.”

CASE 5, 6, 7, 8

NOTICE “Now you need a target.”

CASE ELSE

NOTICE “What are you? A peace-loving hippie freak?”

END SELECT

END

In this example, if the user types a number from 1 to 4, the program prints, You need more water balloons. If the user types a number from 5 to 8, the program prints, Now you need a target. If the user types any number les than 1 or greater than 8, the program prints, What are you? A peaceloving hippie freak?