WHILE - Implement While Loop

The format of the WHILE External function is:

WHILE(Run@, Test@, Move@, Startup@, Initialise@)

While.gif (19944 bytes)

The function returns a logical value after a loop has run to completion.
In normal programming terms, the operation of a WHILE loop may be visualised as:

          a := initial_value;
          WHILE a < 10 do begin a := a + 3; end; 

There is considerable machinery implicit here, testing, varying the value of a, asserting a sequence and performing a conditional jump. In a knowledge network, a := a + 3 is not a valid statement as the variable "a" can have only one value at a time, and in changing its value, the old value is lost. The WHILE Function is provided to handle the operation of a WHILE loop. The function must be used in conjunction with the GATE or GETPUT functions, which do retain the old value of a variable. The parameters of the WHILE Function are:

Logical output, TRUE while Test@ is being searched for.
Returns value of test, loop finishes if Test@ is FALSE.
Logical input, controls moving of values through GATE.
Logical input, starts loop if TRUE, skip if FALSE.
Logical output, set TRUE on first cycle, then FALSE.

In operation, searching comes in on link 1, the value of the function. What happens next is:

  1. Search for Startup@. If FALSE, return value
  2. Set Initialise@ and Run@ to TRUE
  3. Search for Test@
  4. If Test@ is TRUE, search for Move@ else return value
  5. Reset Run@ to TRUE, set Initialise@ to FALSE
  6. Go to step 3

There are three tricks here.

Setting Initialise@ to TRUE for the first cycle allows the GATE or GETPUT Functions to provide an initial value.

Resetting Run@ to TRUE at the start of each loop will kill the old values, as Run@ controls the
logical environment of at least the GATE functions.

Searching for Move@ allows the GATE functions to become active, and store the incremented
values of variables for use in the next cycle.

The operation of the function will not appear obvious to someone used to computer programming, where a WHILE is made out of a conditional jump back to a previous operation, and the phase of operations is implicit in the sequence. This WHILE operates more like an electrical relay or logic microcircuit, where connections have to be made to its inputs and outputs for it to operate correctly.

WHILE is also provided as a Drawn Function.