Sfairadora

  • Reference Guide

while Cycle

while(cond) block
while(cond) block until(cond2) block2
while(cond) block until(cond2) block2 else block3

The Variant without the until-Branch

The while cycle evaluates the block repeatedly, as long as the cond condition is satisfied. More precisely: first, the cond expression is evaluated, and if its result is TRUE, the block expression (the cycle body) is evaluated. After the block is evaluated, the condition is evaluated again, and so forth until the result of the condition is FALSE.
The value obtained by the last evaluation of the block is returned as the result of the entire while cycle. If the block was not evaluated even once, the result is the initial value of the block type. E.g. if the block result is an integer number, and the condition was not satisfied at the first pass, the result is 0.
Example:
var Int i;
...
var Int factorial = 1;
while(i > 0)
(
factorial = factorial * i;
i--; // decrement the i variable by one
)

The Variant with the until-Branch

If the until-branch is present, after each evaluation of the block, the cond2 condition is evaluated. If the cond2 condition is satisfied, the block2 is evaluated, the cycle is terminated, and the value of block2 is returned as the result of the entire while cycle. If the cycle is terminated because of the while-condition returned FALSE, the block3 in the else-branch is evaluated, and its value is returned as the result of the entire while cycle. If the else-branch is omitted, the result of the while cycle is the initial value of the type of the block2.
This arrangement is suitable for structured coding of searches. The while-condition determines the domain being searched; the block determines the search step. The cond2 condition in the until-branch specifies what is being searched. The block2 in until-branch specified the value found, the block3 in the else-branch the value returned at search failure.
The block between while and until can be left empty. The block2 in the until-branch can also be left empty. The result value is then NONE.
Example:
var Int i = 0;
var String name = while(i<size(document.data.employees))
i++
until(document.data.employees[i].last_name=="Smith")
document.data.employees[i].first_name
else
"";
This example sets the variable name to the first_name of the first employee found in the list whose name is Smith. The else-branch could be omitted, because when the search fails, the result would be the initial value of the type returned by the until-branch, which is an empty string in this case.

Error Handling

If the result of a condition or a block evaluation is an error (the Error type), the cycle is terminated, and its result is that error. For details, see chapter on error handling. If the cycle body manipulates with the Error type, it is necessary to ensure that the Error value is not the body result. The try statement is suitable for this purpose.

Notes

The expressions block, block2, and block3 are blocks in the sense that the scope of variables defined within them is limited to the block.
The cycle can be prematurely terminated by the break statement.
If the cycle iterates through a sequence of numbers or through a table, it is more suitable to use the for cycle.