The program will print:
0
This is because MILE and MILE# are two different names, for two
different variables.
MILE# was first used in the PRINT statement, so that is when
memory was found for it. It was initialized to zero, so
it held a zero when
the division MILE# / 12.5
was done.
A program may use many variables. Variables are useful for storing the intermediate parts of a calculation. Here is a different solution to the program you saw in chapter two:
Problem: Calculate the number of 8 ounce glasses of grape soda consumed if a 5 gallon keg of grape soda has 1.5 gallons still left in it. (There are 128 fluid ounces per gallon.)
' Glasses of Grape soda ' LET KEGSIZE = 5 ' statement 1 LET LEFTOVER = 1.5 ' statement 2 LET GALLONS = KEGSIZE - LEFTOVER ' statement 3 LET OUNCES = GALLONS * 128 ' statement 4 PRINT "glasses of soda =", OUNCES / 8 ' statement 5 END
Remember the idea of sequential execution. The statements of this program will execute one by one in order starting with the first statement.
In statement 1, the variable KEGSIZE
is used for the first time,
so memory is found for it.
The LET
puts a 5 in that memory.
Here is what computer main memory looks like after statement 1
has executed.
|
Now statement 2 executes. Memory is found for the variable LEFTOVER
and the value 1.5 is saved in it:
|
|
Statement 3 is more complicated.
LET GALLONS = KEGSIZE - LEFTOVER ' statement 3
This statement does several things:
Remember that using the variables KEGSIZE and LEFTOVER in statement 3 does not change them. After statement 3 has executed, main memory looks like:
|
|
|
When statement 3 is finished, statement 4 executes.
What 4 things does statement 4 do?
LET OUNCES = GALLONS * 128 ' statement 4