Skip to main content
Mail a PDF copy of this page to:
(Your email address will not be added to a mailing list)
Show menu

Scripts - Flow control and looping constructs


The conditional If statement executes some code if a condition is true.

if (condition)

The condition is evaluated and when resulting in true then the statementblock is executed.


The If statement can contain an optional else clause.

if...else executes some code if a condition is true and another code if that condition is false.

if (condition) {
} else {

The condition is evaluated and when resulting in true, then statementblock1 is executed. If not, then statementblock2 is executed.


The while loop executes a block of code as long as a specified condition is true.

while (condition) {

While the evaluation of the condition results to true the statementblock is executed.


The do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true.

do {
} while (condition);

The statementblock is executed while the evaluation of the condition results to true.

This means that the statementblock is executed at least once.


The for loop loops through a block of code a specified number of times. It is used when you know in advance how many times the script should run.

for (initialization; condition; increment) {

The initialization code is executed (one time) before the execution of the statementblock.

The condition defines the condition for executing the statementblock. If false, then the statementblock is not executed and the loop is interupted.

increment is executed every time after the statementblock has been executed.

When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop.

The following code prints the numbers 1 to 5:

for (i=1; i<6; i++) {
	print i;


The ForEach loop is executed for each element in an enumeration.

foreach (enumeration [sort] as variablename) {

In each loop, the element of the enumeration is assigned to the workspace variable variablename.

When the optional argument sort is specified, the values are enumerated in an alphabetical or numerical ascending order.

The ForEach loop has 5 variants:

For example:

"Treatment" "BMI"
A            23.6
B            28.2
B            24.1
A            22.2
foreach (category(treatment) as tv) {
	print tv; println;

results in



The infinite loop loop executes a block of code repeatedly. It never terminates by itself.

loop {
	if (condition) { break; }

All statements inside the block are executed repeatedly. The loop can only be interrupted by the break (or halt) statement.


You can jump out of a loop (while, do-while, for, foreach, loop) using the break statement.

In the following example random numbers are generated until it is the number 5:

loop {
	if (a=5) { break; }
	print a; println;


The continue statement breaks one iteration in a loop, usually if a specified condition occurs, and continues with the next iteration in the loop.

The following example lists the reciprocal of the numbers -2 to 2 but skips the value 0:

for (i=-2; i<3; i++) {
	if (i=0) { continue; }
	print 1/i; println;


The halt statement immediately stops execution of the script.

See also