98 Section 8: Program Branching and Controls

Looping

Looping is an application of branching which uses a t instruction to repeat a portion of the program. A loop can continue indefinitely, or may be conditional. A loop is frequently used to repeat a calculation with different variables. At the same time, a counter, which increments with each loop, may be included to keep track of loop iterations. This counter can then be checked with a conditional test to determine when to exit the loop. (This is shown in the example on page 112.)

Conditional Branching

There are two general applications for conditional branching. One is to control loops, as explained above. A conditional test can check for either a certain calculated value or a certain loop count.

The other major use is to test for options and pursue one. For example, if a salesperson made a variable commission depending on the amount of sale, you could write a program which takes the amount of sale, compares it to a test value, and then calculates a specific commission depending on whether the sale is less than or greater than the test value.

Tests. A conditional test takes what is in the X-register (“x”) and compares it either to zero (such as ~) or to “y”, that is, what is in the Y-register (such as £). For an x:y comparison, therefore, you must have the x- and y-values juxtaposed in the X- and Y-registers. This might require that you store a test value and then recall it (bringing it into the X-register). Or, the value might be in the stack and be moved, as necessary, using ®, ), or (.

Tests With Complex Numbers and Matrix Descriptors. Four of the conditional tests also work with complex numbers and matrix descriptors:

~, T 0 (x0), T 5 (x = y), and T 6 (xy). Refer to sections 11 and 12 for more information.

Flags

As a conditional test can be used to pick an option by comparing two numbers in a program, a flag can be used to pick an option externally. Usually, a flag is set or cleared first thing in a program by choosing a different starting point (using different labels) depending on the condition or mode you want (refer to the example on page 95).