For Statement

2.10 For Statement

T-Plan Robot Enterprise supports for statements which allow to iterate over a range of values or loop as long as a particular condition is satisfied. There are two general forms of the for statement.

1. Conditional For Statement

First variant of the for statement executes as long as the specified Boolean expression results true:

for (<statement1>; <boolean expression>; <statement2>) {
    <commands>
}

As the Boolean expression is being evaluated before every loop, the commands inside the loop will not be executed at all if the expression results in false right from the beginning.

Statements statement1 and statement2 are evaluated using the Eval command and should have a syntax like '<variable>=<numeric expression>'. They can be also empty. The following examples are equivalent and loop for values of variable 'index' ranging from 0 to 5. Both code snippets will type '012345':

for (index=0; {index}<6; index={index}+1) {
    Type {index} 
}

Eval index=0
for ( ; {index} < 6; ) {
    Type {index} 
    Eval index={index}+1
}

Version 2.3 and newer also supports a simpler syntax where the variable calls inside the statement header omit the curly braces, such as for example:

for (index=0; index<6; index=index+1) {
}

2. For Statement Iterating over a Predefined Set of Values

This form allows to iterate over a predefined set of values:

for <variable name> in <list of space-separated values> {
    <commands>
}

The following example will type a sentence "I speak English, Spanish, Brazilian Portuguese."

Type "I speak " 
for language in English Spanish "Brazilian Portuguese" {
    if ("{language}" == "Brazilian Portuguese") {
        Type "{language}." 
    } else {
        Type "{language}, " 
    }
}

The value set can be also specified by a variable. Version 3.0.1 and higher support values containing spaces specified through a variable. The following code shows the previous example with the value set specified through variables.

Note that the variable call must NOT be enclosed with double quotes because it would be handled as a single value.

Type "I speak " 
Var VALUES="English Spanish \"Brazilian Portuguese\""
for language in {VALUES} {
    if ("{language}" == "Brazilian Portuguese") {
        Type "{language}." 
    } else {
        Type "{language}, " 
    }
}

Execution of the for statement can be interrupted by a break command. The current loop can be skipped by the continue command. If there are nested loops the break/continue commands will apply to the innermost for statement.

The following example illustrates how to use a combination of for and waitfor to hold on execution until the remote desktop stops to update. #

# Infinite loop
for (; 0 == 0; ) {

    # Wait for at least 20% update of the remote screen.
    # If it doesn't update for 10 seconds, break the loop.
    Waitfor update extent=20% timeout="10s" ontimeout="break"
}