Making
Decisions
an
d
Solving
Problems:
IF-THE
N,
FOR
- NEXT
The IF-THEN and FOR-NEXT commands enable you to write programs
that mimic the way humans approach a decision or a problem.
Especially useful
for games and logic puzzles, the commands let you,
the programmer, make the choices for the computer.
IF·THEN COMMANDS
To practice the IF-THEN statement, type
in
the following program:
NEW
1.
REM
***
BRNPROBE
. IlZ
***
5
PRINT"
'Ii "
1.0
DIM
RAIN$
(3)
20
PRINT:
PRINT
"YES
OR
NO
..
IF
IT
WERE
RAINING
OUTSIDE
..
WOULD YOU
GO
OUT
WITH
AN
UMBRELLA"
;
3 0
INPUT
RAIN$
40
IF
RAIN$="YES"
THEN
PRINT
"YOU
HAVE
A
FORMIDABLE
Ill."
50
IF
RAIN$="NO"
THEN
PRINT
"YOU
ARE
A BORN
RISK
TAKER."
The Brainprobe Ouiz evaluates your answer.
In
line 40, if the answer
stored
in
the string variable
RAI
N$
is yes, the computer prints the
10
messag
e.
If the answer is not yes, the computer reads the
ne
xt line,
line
50,
and evaluates the string variable RAIN$ again. If the answer
is
NO,
the computer prints the risk-taker message. However, if you
answer neither yes nor no, the program just ends. The program has no
instructions for responding to
an
indefinite answer. Try it out.
One way to encourage
an
expected reply is to create
an
infinite loop.
Insert the additional line below:
60
GOTO
20
Evaluating with IF·THEN
Another way to encourage a correct answer is to provide hints. The
following program uses numeric variables to elicit a
correct
response:
NEW
1.
REM
***
NUMBER.
IlZ
***
5
PRINT"
'Ii "
1.0SECRETNUM=INT(RND(0)*1.0)+1.
20
PRINT:
PRINT
"GUESS
A
SECRET
NUMBER
BETWEEN
1.
AND
1.0.
"
55