Syntax: for loop

The for statement is a simple loop that will loop through a series of statements while the test expression is true, at which point the next line after the loop is executed.

Syntax:

for (initialise; test; increment)

list of statements
}

When the loop begins the expression ‘initialise’ is executed. Then the following 3 steps are performed repeatedly:

  1. (i) The expression test is evaluated and if it is false the loop is complete and the next statement after the loop is executed.

  2. (ii) The list of statements inside the loop is executed.

  3. (iii) The expression increment is executed and then we begin again at step 1.

Remarks:

for – is a keyword, the system recognises this is the start of a loop.

initialise – is executed before the first iteration of the loop.

test – an expression that is evaluated at the beginning of each iteration of the loop. When it evaluates to false the loop is complete.

increment – a statement that is run at the end of each iteration of the loop.

list of statements – any valid OpusScript statements.

; - the semi-colon must be placed in-between the expressions or the loop will not work.

( ) – the expressions must be surrounded by round brackets.

{ } – the list of statements must be surrounded by curly brackets.

Any of the parameters initialise, test and/or increment may be omitted, as long as the semi-colons are still present. An omitted test is assumed to evaluate to true, thus the loop will go on forever, unless there is a break or return statement inside it.

image\Script_Button.jpgExamples