MedCalc

# Scripts - Flow control and looping constructs

## If

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

if (condition)
{
statementblock;
}

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

### If...Else

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) {
statementblock1;
} else {
statementblock2;
}

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

## While

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

while (condition) {
statementblock;
}

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

## Do-while

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 {
statementblock;
} 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.

## For

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) {
statementblock;
}

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;
println;
}

## ForEach

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

foreach (enumeration [sort] as variablename) {
statementblock;
}

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:

• ForEach (Variable as v): enumerates all variables (and derived variables) defined in the spreadsheet
• ForEach (Filter as v): enumerates all filters
• ForEach (Category(var) as v): enumerates all different categories of a variable var defined in the spreadsheet
• ForEach (Value(var) as v): enumerates all values of a variable var defined in the spreadsheet
• ForEach (Row as v): enumerates all spreadsheet rows
• ForEach (Column as v): enumerates all spreadsheet rows

For example:

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


results in

A
B


## Loop

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

loop {
statements;
if (condition) { break; }
morestatements;
}


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

## Break

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 {
a=random(10);
if (a=5) { break; }
print a; println;
}


## Continue

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;
}


## Halt

The halt statement immediately stops execution of the script.