BASIC9.WPS
BASIC LANGUAGE PROGRAMMING
USING IF THEN/ELSE/ELSEIF/ENDIF
BRANCH INSTRUCTIONS
One of the most powerful features of computers is their
ability to make decisions. This sets them dramatically aside
from calculators. Some of the decisions which a computers can make are:
1) < means less than
2) > means greater than
3) <= means less than or equal to
4) >= means greater than or equal to
5) = means equal to
6) <> means NOT equal to
Here is a simple program which employs IF/THEN branching to
decide if one number is larger than another. PLEASE NOTE THAT THE ELSE STATEMENT REQUIRES NO ARGUMENT AFTER IT. The ELSE branch is taken automatically if the program cannot find a TRUE IF -or- ELSEIF statement.
REM THIS PROGRAM DECIDES WHICH NUMBER IS LARGER
CLS
INPUT A
INPUT B
IF A > B THEN
PRINT "A IS LARGER THAN B"
ELSEIF A = B THEN
PRINT " A IS EQUAL TO B"
ELSE
PRINT " B IS LARGER THAN A"
ENDIF
END
(continued - next page)
More complex decisions can be made by combining IF/THEN/ELSEIF/ELSE decisions with AND or OR statements. Here is an example of a program which assigns letter grades to numerical grades by using the IF/THEN/ELSEIF ELSE AND ENDIF statements:
CLS
INPUT GRADE
IF GRADE >= 80 THEN
PRINT " CONGRATULATIONS YOU HAVE AN -A- IN THE COURSE"
ELSEIF 80> GRADE AND GRADE >=70 THEN
PRINT " CONGRATULATIONS YOU HAVE AN -B- IN THE COURSE"
ELSEIF 70> GRADE AND GRADE >=60 THEN
PRINT " CONGRATULATIONS YOU HAVE AN -C- IN THE COURSE"
ELSEIF 60> GRADE AND GRADE >=50 THEN
PRINT " YOU HAVE A -D- IN THE COURSE"
ELSE
PRINT " CONSIDER A DIFFERENT OCCUPATION -F-"
ENDIF
END
Assignment- Use IF/THEN/ELSEIF AND ENDIF statements to
write the following programs:
1) Write program which asks the student their name and age.
If the student is over 16 years of age, the program is
to then remind them (by name) that they are eligible for
a driving license.
2) Write a program which asks the students for their name,
best subject and average in that subject and then
prints out a statement summarizing that information and
also prints out a letter grade for the subject in the
statement.
3) Write a program which asks the user to guess a number
between 1 and 10 and gives the user hints if he/she
is guessing high or low. This process is to repeat itself
until the person can guess the number correctly
(Hint - using a FOR/NEXT LOOP could help! Consider the
maximum number of wrong guesses a person could make in
setting the loop up. Also consider how to end the loop
prematurely if the user guess the correct answer)
Copyright - Barry Boyle (bboyle@kawartha.net)