0 1 2 3 4 5
Here is the program:
' Loop with a loop condition ' LET COUNT = 0 'Statement 1 DO WHILE COUNT <= 5 'Statement 2 PRINT COUNT 'Statement 3 LET COUNT = COUNT + 1 'Statement 4 LOOP END
It is often useful to try to understand the first number and the last number that are printed.
COUNT is set to zero (Statement 1),
the DO statement (Statement 2) lets the loop body start,
and the PRINT
statement (Statement 3) prints 0.COUNT up from 0 to 1, from 1 to 2,
from 2 to 3, from 3 to 4...COUNT becomes 4 and is printed.
Now Statement 4 changes COUNT from 4 to 5.
Then the DO statement (Statement 2) lets the loop body start since 5 is equal to 5,
and the PRINT statement (Statement 3) prints 5.
Statement 4 changes COUNT from 5 to 6.DO statement
does NOT let the loop body begin again,
so the END is executed and the program is over.Getting confused by a loop is very easy, even among professional programmers. Practice helps. Here is the program again, with changes.
' Loop with loop condition ' LET COUNT = 1 'Statement 1 DO WHILE COUNT <= 3 'Statement 2 PRINT COUNT 'Statement 3 LET COUNT = COUNT + 1 'Statement 4 LOOP END
What will this new program print?