Amos Professional Manual  Contents  Index

Control Structures


Unlike that last example, instead of checking if a condition is true or false at the start of a loop, the pair of commands makes its check at the end of a loop. REPEAT marks the start and UNTIL the end of the loop to be checked. This means that if a condition is false at the beginning of a

WHILE ... WEND structure, that loop will never be performed at all, but if it is true at the beginning of a REPEAT ... UNTIL structure, the loop will be performed at least once. Here is an example that waits for you to press a mouse button:

E> Repeat
    Print "I can go on forever" : Wait 25
   Until Mouse Key<>0

Controlled loops
When deciding how many times a loop is to be repeated, control can be made much more definite than relying on whether conditions are true or false.

FOR
TO
NEXT
structure: repeat loop a specific number of times
For index=first number To last number
list of statements
Next index

This control structure is one of the programmer's classic devices. Each FOR statement must be matched by a single NEXT, and pairs of FOR ... NEXT loops can be nested inside one another. Each loop repeats a list of instructions for a specific number of times, governed by an index which counts the number of times the loop is repeated. Once inside the loop, this index can be read by the program as if it is a normal variable. Here is a simple example:

E> For X=1 To 7
    Print "SEVEN DEADLY SINS"
   Next X

STEP
structure: control increment of index in a loop
For index=first number To last number Step size

Normally, the index counter is increased by 1 unit at every turn of a FOR ... NEXT loop. When the current value exceeds that of the last number specified, the loop is terminated. For example:

E> For DAY=1 To 365
    Print DAY
   Next DAY

STEP is used to change the size of increase in the index value, like this:

E> For DAY=1 To 365 Step 7
    Print DAY
   Next DAY
Back    Next
05.04.09