This Instruction allows the execution of a group of instructions to be repeated a defined number of times.
Syntax |
for (init, cond, update) |
---|---|
init |
Loop initialization expression, typically a variable assignment (optional, default 0) |
cond |
Boolean expression describing the stay condition within the FOR loop |
update |
Update expression, typically variable increment/decrement. (optional, default 1) |
Validity |
Rule, Task |
Note |
Contrary to what happens in C, in R3 the declaration of a variable inside the arguments of a for loop is not allowed. It is therefore necessary to declare the variable previously |
Example of use:
Example of omitting optional data: Using the FOR loop it is possible to omit both the initialization of the count variable and the increment of the count variable for example the following Instruction:
Example of use with the continue instruction: Use of the continue instruction within a FOR loop is possible; its execution causes the line in question to jump to the end_for instruction without executing any other instruction Example of use with the break instruction: The use of the break instruction within a FOR loop is possible; its execution causes the FOR loop to exit immediately without executing any other instructions.
Example with dynamic assignments: The initial value of the FOR loop control variable is equal to the sum of the contents of the nonvolatile integer register nvr(12) and the nonvolatile integer register nvr(14). The loop is executed until j is less than the difference between the contents of the nonvolatile integer register nvr(22) and the nonvolatile integer register nvr(24). The variable j is incremented by 1 with each iteration. |