The skip statement has no effect and terminates immediately. The skip statement is normally used in guards. Its form is
skip |
The stop statement stops the execution of the entire program. It has two forms, the latter of which evaluates the given expression and returns the results as exit status.
stop or stop ( expr )
If the exit status is non-zero the final block in the main resource will not be run.
An if statement consists of the keyword if followed by one or more guarded commands separated by the keyword else if.
if guarded_command else if guarded command ... |
Each guarded command is a boolean expression enclosed by parameters followed by the block of commands to be executed if the expression evaluates to true.
( expr ) { block }
|
The list of guarded commands can optionally be followed by an else branch, which will be executed if all guards in the if statement evaluates to false.
else { block }
|
A simple example would be an if statement setting the variable
textttx to its absolute value.
if (x < =) { x = -x }
|
Another simple example sets the variable minimum to the smallest of the two variables x and y.
if (x < y) { minimum = x }
else { minimum = y }
|
The for statement is used for definite iteration. A for statement consists of a (non empty) list of comma separated quantifiers, each defining the initial value of a given variable and how each iteration will effect the value of that variable.
for [ quantifier, quantifier, ... ] { block }
|
Each quantifier introduces a new bound variable (shadowing any previous definitions) and initializes it to the result of the given initial expression. The variable is then incremented or decremented each iteration until the variable reaches the result of the final expression, which is reevaluated each iteration. If there is more than one quantifier the body of the for statement will be run with every combination of the bound variables. The basic form of the quantifier is
variable = init_expr direction final_expr |
where the direction is either to or downto
The size of the increments can be controlled by adding an extra by statement to he quantifier. As above the iteration variable may be used in the by expression, which will be reevaluated each iteration. The iteration value will be incremented or decremented by the value the by expression evaluates to. The form of the by statement is
by expr |
For a more fine grained control of which values the iteration variable should be assigned a such-that expression may be used. When a quantifier contains a such-that clause the iteration variable will only take on those values for which the given expression evaluates to true. All values in the (possibly indirectly) specified range of the bound variables are enumerated, but a values is not used if the expression evaluates to false. The form of the such-that clause is
st expr |
Consider the following example which will print the letters a to z.
for [i = 'a' to 'z' ] { write(i) }
|
In the following example the iteration variable j depends on i and iterates backwards.
for [i = 1 to 3, j = i-1 downto 0] { write(i,j) }
|
Thus, the statement outputs the following:
1 0 2 1 2 0 3 2 3 1 3 0 |
The last example counts the number of zeroes in the array v using a such-that clause:
int cnt = 0
for [i = lb(v) to ub(v) st v[i] == 0] { cnt++ }
|
The final and by expressions are reevaluated in each iteration, which may give unexpected results. Consider for example
int n = 10
for [ i = 1 to (n - i) ]
{ write(i) }
|
This will result in the numbers 1,2,3,4,5 being output, since when i has reached the value 5 then i is equal to the final expression, n - i = 10 - 5 = 5 and the iteration will stop.
A by clause may also take real numbers as increment or decrement step. However, since most real values cannot be represented exactly on binary machines this may also lead to unpredicted results.
The exit statement is used to exit the smallest enclosing iterative statement. Its form is
exit |
The next statement forces return to the loop control of the smallest enclosing iterative statement. Its form is
next |