The FOR statement

The FOR statement is used when the number of occurrences to process is known. This statement is used to manage the number of occurrences via a variable in which the passages performed in the loop will be counted.

The syntax of FOR statement is as follows:

FOR Subscript = Start Value TO End Value

Process to run

END

For example, the following code runs the process 2000 times:

FOR Cnt = 1 TO 2000

// Process to run

END

Remark: An increment step of subscript can be defined via the STEP keyword. For example, the following code runs the process 2000 times and the Cnt variable decreases by 10:

FOR Cnt = 2000 TO 1 STEP -10

// Process to run

END

See FOR statement for more details.

The LOOP statement

The LOOP statement is used to perform loops when the number of occurrences to process is unknown. In this case, a test must be used to exit from the loop.

The syntax of LOOP statement is as follows:

LOOP

Process to run

IF <Expression> THEN BREAK

END

For example:

Counter is int

Counter = 10

LOOP

// Process to run

Counter = Counter - 1

IF Counter = 0 THEN BREAK

END

 

The WHILE statement

The WHILE statement and the LOOP statement operate according to the same principle. The difference is that the test of exit condition is performed BEFORE running the loop code. This test is used to compare a variable. This variable starts from a start value and it is modified in the loop until it reaches the value that triggers the exit from the loop.

The syntax of WHILE statement is as follows:

 

<Initialize the variable to its start value>

WHILE <Compare the variable to its end value>

Process to run

<Modify the variable>

END

For example:

Counter is int

Counter = 0

WHILE Counter<10

// Process to run

Counter = Counter + 1

END