Control Structures

STRUCTURE DEFINITION
if If x is true then return y else return z.eg:
  1. if (x, y, z)
  2. if ((x + 1) > 2y, z + 1, w / v)
  3. if (x > y) z;
  4. if (x <= 2*y) { z + w };
if-else The if-else/else-if statement. Subject to the condition branch the statement will return either the value of the consequent or the alternative branch. eg:
  1. if (x > y) z; else w;
  2. if (x > y) z; else if (w != u) v;
  3. if (x < y) { z; w + 1; } else u;
  4. if ((x != y) and (z > w))
    {   
      y := sin(x) / u;
      z := w + 1;     
    }                  
    else if (x > (z + 1))
    {                    
      w := abs (x - y) + z;
      u := (x + 1) > 2y ? 2u : 3u;
    }
    
switch The first true case condition that is encountered will determine the result of the switch. If none of the case conditions hold true, the default action is assumed as the final return value. This is sometimes also known as a multi-way branch mechanism. eg:
switch                                                 
{                                                      
  case x > (y + z) : 2 * x / abs(y - z);               
  case x < 3       : sin(x + y);                       
  default          : 1 + x;                            
}
while The structure will repeatedly evaluate the internal statement(s) 'while' the condition is true. The final statement in the final iteration will be used as the return value of the loop. eg:
while ((x -= 1) > 0)                                   
{                                                      
  y := x + z;                                          
  w := u + y;                                          
}
repeat/until The structure will repeatedly evaluate the internal statement(s) 'until' the condition is true. The final statement in the final iteration will be used as the return value of the loop. eg:
repeat                                               
  y := x + z;                                        
  w := u + y;                                        
until ((x += 1) > 100)
for The structure will repeatedly evaluate the internal statement(s) while the condition is true. On each loop iteration, an 'incrementing' expression is evaluated. The conditional is mandatory whereas the initialiser and incrementing expressions are optional. eg:
for (var x := 0; (x < n) and (x != y); x += 1)        
{                                                     
  y := y + x / 2 - z;                                 
  w := u + y;                                         
}
break/break[] Break terminates the execution of the nearest enclosed loop, allowing for the execution to continue on external to the loop. The default break statement will set the return value of the loop to NaN, where as the return based form will set the value to that of the break expression. eg:
while ((i += 1) < 10)                                   
{                                                       
  if (i < 5)                                            
    j -= i + 2;                                         
  else if (i % 2 == 0)                                  
    break;                                              
  else                                                  
    break[2i + 3];                                      
}
continue Continue results in the remaining portion of the nearest enclosing loop body to be skipped. eg:
for (var i := 0; i < 10; i += 1)  
{                                 
  if (i < 5)                      
    continue;                     
  j -= i + 2;                     
}
return Return immediately from within the current expression. With the option of passing back a variable number of values (scalar, vector or string). eg:
  1. return [1];
  2. return [x, 'abx'];
  3. return [x, x + y,'abx'];
  4. return [];
  5. if (x < y)                                       
        return [x, x - y, 'result-set1', 123.456];      
       else                                             
        return [y, x + y, 'result-set2'];
    
?: Ternary conditional statement, similar to that of the above denoted if-statement. eg:
  1. x ? y : z
  2. x + 1 > 2y ? z + 1 : (w / v)
  3. min(x,y) > z ? (x < y + 1) ? x : y : (w * v)
~ Evaluate each sub-expression, then return as the result the value of the last sub-expression. This is sometimes known as multiple sequence point evaluation. eg:
~(i := x + 1, j := y / z, k := sin(w/u)) == (sin(w/u)))
~{i := x + 1; j := y / z; k := sin(w/u)} == (sin(w/u)))
[*] Evaluate any consequent for which its case statement is true. The return value will be either zero or the result of the last consequent to have been evaluated. eg:
[*]                                                     
{                                                       
  case (x + 1) > (y - 2)    : x := z / 2 + sin(y / pi); 
  case (x + 2) < abs(y + 3) : w / 4 + min(5y,9);        
  case (x + 3) == (y * 4)   : y := abs(z / 6) + 7y;     
}
[] The vector size operator returns the size of the vector being actioned. eg:
  1. v[]
  2. max_size := max(v0[],v1[],v2[],v3[])

Note: In the tables above, the symbols x, y, z, w, u and v where appropriate may represent any of one the following:

  1. Literal numeric/string value
  2. A variable
  3. A vector element
  4. A vector
  5. A string
  6. An expression comprised of [1], [2] or [3] (eg: 2 + x /vec[3])