Combining types of loops

You can combine repetitive and conditional loops to create a compound loop. The following loop is set to repeat 10 times while the quantity is less than 50, at which point it stops.
quantity = 20
DO number = 1 TO 10 WHILE quantity < 50
quantity = quantity + number
SAY 'Quantity = 'quantity ' (Loop 'number')'
END
The result of this example is as follows:
Quantity = 21 (Loop 1)
Quantity = 23 (Loop 2)
Quantity = 26 (Loop 3)
Quantity = 30 (Loop 4)
Quantity = 35 (Loop 5)
Quantity = 41 (Loop 6)
Quantity = 48 (Loop 7)
Quantity = 56 (Loop 8)
You can substitute a DO UNTIL loop, change the comparison operator from < to > , and get the same results.
quantity = 20
DO number = 1 TO 10 UNTIL quantity > 50
quantity = quantity + number
SAY 'Quantity = 'quantity ' (Loop 'number')'
END

Exercises: combining loops

What happens when the following program runs?
DO outer = 1 TO 3
SAY /* Produces a blank line */
DO inner = 1 TO 3
SAY 'Outer' outer 'Inner' inner
END
END
Now what happens when the LEAVE instruction is added?
DO outer = 1 TO 3
SAY /* Produces a blank line */
DO inner = 1 TO 3
IF inner = 2 THEN
LEAVE inner
ELSE
SAY 'Outer' outer 'Inner' inner
END
END
ANSWERS
  1. When this example runs, it produces the following:
    Outer 1 Inner 1
    Outer 1 Inner 2
    Outer 1 Inner 3
    
    Outer 2 Inner 1
    Outer 2 Inner 2
    Outer 2 Inner 3
    
    Outer 3 Inner 1
    Outer 3 Inner 2
    Outer 3 Inner 3
  2. The result is one line of output for each of the inner loops.
    Outer 1 Inner 1
    
    Outer 2 Inner 1
    
    Outer 3 Inner 1


rvse025.html | Timestamp icon Last updated: Monday, 5 June 2017