Output:
As you can see this loop repeated itself until the condition is met. If for some reason your loop does not have a condition that can be or will ever be met like 1 = 2, or if there is no condition at all (which is legal for this type of loop) then the loop will continue to loop forever.
The second type of loop is also a pre-test loop, the difference with this one is
that instead of looping WHILE a condition is true, it loops UNTIL a condition is true. Here is an example:
X% = 0
DO UNTIL X% = 10
X% = X% + 1
PRINT X%
LOOP
1
2
3
4
5
6
7
8
9
10
The next type of loop to discuss (which is still a DO-LOOP) is what is know as a post-test loop.
I'm sure you can guess what exaclty that means from the previous examples. Essentially a post-test loop does the condition test
last instead of first.
The first example is a loop WHILE a condition is true, like the first example above, and the second example loops UNTIL
a condition is met. Here are the examples:
Example 1:
X% = 0
DO
X% = X% + 1
PRINT X%
LOOP WHILE X% < 10
1
2
3
4
5
6
7
8
9
10
X% = 0
DO X% = 10
X% = X% + 1
PRINT X%
LOOP UNTIL X% = 10
1
2
3
4
5
6
7
8
9
10
Back | Tutorial Part 1 | Next |