When it runs it will print out:
2 3 4
The first number printed is "2" because COUNT
was started
out at 2 and 2 < 5 is true:
' Loop with LESS THAN ending test ' LET COUNT = 2 'Statement 1 ' DO WHILE COUNT < 5 'Statement 2 PRINT COUNT 'Statement 3 LET COUNT = COUNT + 1 'Statement 4 LOOP ENDThe program keeps increasing and printing
COUNT
until COUNT
reaches 5.
Then it fails the condition 5 < 5 so the program ends.
Here is what happens in detail:
COUNT | condition | true or false | what happens |
---|---|---|---|
2 | 2 < 5 | true | loop body executed, COUNT increased by 1 |
3 | 3 < 5 | true | loop body executed, COUNT increased by 1 |
4 | 4 < 5 | true | loop body executed, COUNT increased by 1 |
5 | 5 < 5 | false | loop body NOT executed, execution skips to END. |
To be sure that your boredom is complete, look at another program:
' Loop with different beginning ' LET COUNT = -2 'Statement 1 ' DO WHILE COUNT < 2 'Statement 2 PRINT COUNT 'Statement 3 LET COUNT = COUNT + 1 'Statement 4 LOOP ENDHere
COUNT
is started out with a value of -2.
This is OK. The DO WHILE
"gatekeeper" can test negative
numbers as well as positive numbers, and -2 is certainly less than 2.
What will the new version of the program print out?