' Ask the user for 10 numbers. ' Add them up, print the sum. ' LET SUM = 0 LET COUNT = 1 DO WHILE COUNT <= 10 PRINT "Enter a number" INPUT NUMBER LET SUM = SUM + NUMBER LET COUNT = COUNT + 1 LOOP PRINT "The sum is", SUM END
(Notice how the indenting helps you see how things work.)
The LOOPing part of the program (in black) and the ADDING up part of the program (in red) are separate but cooperating parts, somewhat like playing the piano with two hands, or driving a stick shift car. The two parts are mingled together, and it is easy for you to get mixed up.
The program is OK as it is, but it can be improved. It would be nice to ask the user how many numbers to add up, rather than always add up 10 numbers. Here is a dilogue with the improved program:
How many numbers are to be added ? 4 Enter a number ? 100 Enter a number ? 2 Enter a number ? 10 Enter a number ? 1 The sum is 113
The first number the user enters, 4, is how many numbers are to be added up. The next four numbers that the user enters are added into the sum: 100+2+10+1 = 113.
The improved program loops a different number of times in different runs. Where does the program keep the number of times to loop?