[Previous] Lesson 4 [Next]

Advanced Control Flow

The Computed COME FROM

We have seen that a COME FROM statement can take a constant value.  It can also take a variable value, in order to dynamically COME FROM different parts of the program.

A Simple Conditional

A conditional jump can be effected by using a COME FROM statement whose argument will evaluate to the correct label if the jump is to be executed.  However, there is a catch.  The value that any COME FROM argument value evaluates to must correspond to an existent label in the program at all times.  It is therefore sometimes necessary to include a few dummy labels.  Here is a simple example:

    NOTE Example 4.1: A conditional message
    TELL "Would you like to see a message? (y/n)"
10  ASK response
21  TELL "Hello, world!" NEXT
    COME FROM 10 + response - "n"

Here, 21 is a dummy label, since a COME FROM it at the point where it occurs would have no effect.  However, in this example, if the user inputs anything but y or n then a runtime error will result, since the COME FROM argument would evaluate to a non-existent label.

This is where the SGN operator comes in useful, since it reduces a potentially infinite number of possible values to only three, substantially reducing the number of labels that a program would need to contain.  This example illustrates its most common use:

    NOTE Example 4.2: A better conditional message
    TELL "Would you like to see a message? (y/n)"
10  ASK response
11  TELL "Hello, world!" NEXT
 9  NOTE Dummy label
    COME FROM 10 + SGN(response - "n")

There are two dummy labels here: 11 and 9.  The second of these needs a dummy statement to hold it, and in this case a comment has been chosen for this purpose.  In practice, any null statement can serve as a dummy on which a label can be held.

There is one exception to the rule that a COME FROM must reference an existent label.  If a COME FROM statement contains one or more variables in its argument, then it takes effect only from the time at which all its variables have been assigned values.

Simulating a Loop

This is similar in principle to simulating a conditional.  Here's an example:

    NOTE Example 4.3: Counting to 10 (or rather, down from 10 to 0)
10  CALL 10 count
    COME FROM 10 + SGN count
    TELL "Hello, world!" NEXT
11  CALL count - 1 count

In this example, only one dummy label is needed, since count will never be negative and so it will never look for a label 9.  This is of course a 'test after' loop, i.e. the code within the loop is always executed at least once.  A simple modification can produce a 'test before' loop, and this is left as an exercise for the reader.