' Ask the user for three numbers ' Add them up, print the sum. ' PRINT "What is the first number" INPUT NUMBER1 ' PRINT "What is the second number" INPUT NUMBER2 ' PRINT "What is the third number" INPUT NUMBER3 ' LET SUM = NUMBER1 + NUMBER2 + NUMBER3 PRINT "The total is:", SUM ' END
(You might have thought of a better answer.)
The above program works, and would be OK if all you wanted to do was add up three numbers. But is is somewhat awkward. Here is a somewhat different version:
' Ask the user for numbers. ' Add them up, print the sum. ' LET SUM = 0 ' Clear the sum ' PRINT "Enter a number" ' First number INPUT NUMBER ' User enters first number LET SUM = SUM + NUMBER ' increase the SUM ' PRINT "Enter a number" ' Second number INPUT NUMBER LET SUM = SUM + NUMBER ' PRINT "Enter a number" ' Third number INPUT NUMBER LET SUM = SUM + NUMBER ' PRINT "The total is:", SUM ' At this point, SUM is the total of all numbers ' END
To understand this program,
review how the LET
statement works:
Say that the first four statements of the program are executed and that that the user enters "12".
' Ask the user for numbers. ' Add them up, print the sum. ' LET SUM = 0 ' Clear the sum ' PRINT "Enter a number" ' First number INPUT NUMBER ' User enters first number LET SUM = SUM + NUMBER ' increase the SUM . . . .
What value will be in SUM
at this point?