Infinite Loop Next: Finite Loop Up: Basic InstructionsPrevious: An Example

Infinite Loop

SugarCubes provides two kinds of loops: infinite loops and finite ones (described in next section), which both extend UnaryInstruction.

When the body of an infinite loop of class Loop is terminated, it is automatically and immediately restarted.

A loop is said to be ``instantaneous'' when its body terminates completely in the same instant it is started. Instantaneous loops are to be rejected because such a loop would never converge to a stablestate closing the instant. The Loop  class detects instantaneous loops at run time, when the end of the loop body is reached twice during the same instant. In this case, the loop stops its execution for the current instant instead of looping for ever during the instant.

public class Loop extends UnaryInstruction
  protected boolean endReached = false;

  public Loop(Instruction body){ super.body = body; }

  final public String toString(){ 
    return "loop " + body + " end"; 

  public void reset(){ super.reset(); endReached = false; }

  final protected byte activation(Machine machine)
    byte res;
      res = body.activ(machine);
      if (res == TERM){
         if (endReached){
               "warning: instantaneous loop detected");
            res = STOP;
         endReached = true;
      }else break;
    if (res == STOP){ endReached = false; }
    return res;

Note that the loop body is restarted after termination, with the reset method.