ExpandCollapsePrev Next Index

+ 7.1 Procedural control

+ 7.1.1 If/do

The basic conditional looks like this:

  proc f(x:int) 
  {
    if x < 0 do
      println$ "Negative";
    elif x == 0 do
      println$ "Zero";
    else
      println$ "Positive";
    done
  }

The elif and else clauses are optional.

+ 7.1.2 While loop

And here's a standard while loop:

  proc f(x:int) 
  {
    while x > 0 do
      println$ x;
      x = x - 1;
    done
  }  

+ 7.1.3 For loop

Felix for loop uses an inclusive range:

  proc f (n:int)
  {
    for var i in 0 upto n - 1 do
      println$ i;
    done
    println$ i;
  }

Note the control variable is available outside the loop. You must leave out the var if the variable is defined elsewhere in the same scope.

This form of for loop requires the upper limit to be greater than or equal to the initial value. It is guaranteed to work for all signed and unsigned integer types and can span the whole range of these types but does not support an empty range with zero iterations.

+ 7.1.4 C like for loop

Felix also provides a more flexible basic loop similar to C:

  proc f (n:int)
  {
    for (var i=1; i<10; ++i;) 
      println$ i; 
  }

Note the ; after the ++i.

+ 7.1.5 Return

Return from a procedure. A procedure returns automatically if control drops through the end.

  proc f(n:int)
  {
    if n < 0 return;
    if n == 0 do
      println$ "Zero";
      return;
    done
    println$ "Positive";
    return;
  }
  

+ 7.1.5.1 Goto

The conventional understanding in computer science is that goto is considered harmful. We agree, but eliminating it from the language entirely is a severe reaction.

If you really need to:

  proc f(n:int)
  {
    if n < 0 goto endoff;
    println$ "Positive or Zero";
  endoff:>
    println$ "Square is " + (n * n).str;
  }