Computer Programming                                                      Name -

ACSL - WDTPD Worksheet #3                                        Period -

In addition to knowing how to evaluate certain BASIC functions such as LEN( ), STR$( ), and MID$( ), you must also know how to evaluate several key BASIC statements that control the flow and that determine the logic of a program.

One commonly used statement in BASIC is the INPUT statement. Presenting a variable name after the keyword INPUT stops the program and allows the user to enter a value for that variable. For example,

DIM A AS INTEGER
INPUT A
PRINT A
END

The user is given the chance to input a value for A. That value is then printed on the screen by the PRINT statement.

DIM A, B AS INTEGER
INPUT A, B
IF A > 4 THEN PRINT "A"
IF B > 10 THEN PRINT B
END

Note that two variables are declared, A and B. Two values must be inputted by the user when the program executes since they are both listed in the INPUT statement. The order in which the user inputs the 2 values will determine which one is stored in A and which value is stored in B. The IF/THEN statements are one-line IF/THEN's. In the first IF/THEN statement, if the condition (A > 4) is TRUE then the string literal "A" is printed because of the double quotes around the A. In the second IF/THEN statement, if the condition (B > 10) is TRUE then the value for the variable B is printed on the screen.

DIM A AS INTEGER
INPUT A
IF A > 35 THEN
PRINT (A + 2)
ELSE
PRINT (A - 2)
END IF
END

Note that in the program immediately above the IF/THEN/ELSE statement could be replaced by a one-line version as in:

DIM A AS INTEGER
INPUT A
IF A > 35 THEN PRINT (A + 2) ELSE PRINT (A - 2)
END

Evaluate the following programs and determine the output if 5 is entered as the value for A and 15 is entered for the value of B.

DIM A, B AS INTEGER
INPUT A, B
IF (A > 4) OR (B <= 13) THEN PRINT "YES" ELSE PRINT (B * 2)
END

 

DIM A, B AS INTEGER
INPUT A, B
IF (A = 5) AND B < 15 THEN
PRINT (A + B)
ELSE
PRINT (B - A * 2)
END IF
END

 

DIM A, B AS INTEGER
INPUT A, B
IF (A = 6) OR (B = 15) AND (A > 90) THEN PRINT "OR" ELSE PRINT "AND"
END