Skip to content

while

Syntax

while (<predicate>) {
    <statement>
    ...
}

Parameters

<predicate>
A truthy or falsey numeric expression.
<statement>
A valid FRED statement, such as an action or an assignment.

Description

Loop over the provided statements while the predicate is true.

The agent executes the given statements when the given predicate evaluates to true. After finishing execution of all statements in the block, the predicate is evaluated again. If it's true, it executes the statements again, continuing this pattern until the predicate evaluates to false.

Truthy and Falsey Values

Like in many programming languages, FRED's numeric expressions can be evaluated as either true or false. Values that evaluate to true are considered "truthy", and values that evaluate to false are considered "falsey".

In FRED, a value of 0 is falsey, and all non-zero values are truthy.

Examples

This example prints the values in a list x.

while (i < length(x)) {
    print("x[", i, "] = ", x[i])
    i = i+1
}

See Also