# DiGrande.it

Blind, Braille and Embossing Technologies

This site uses cookies to personalize content and ads, provide social media features and analyze links. By closing this banner or continuing to browse, you consent to their use.

# The For cycle

The for cycle is useful for repeating the execution of a code block a defined number of times. The construct of the FOR cycle is as follows:

- For x = 1 to 10

- ...

- EndFor

"x' is the control variable used as a counter. The assignment operator "=" follows. The number "1" is the start value of the cycle, and "10" is the end value. The keyword "to" separates the start value from the finish value.

For example:

- For x = 1 to 10

- Circle(x*10, 50, 8)

- EndFor

This example creates a horizontal line of ten circles that intersect each other. As we have just seen, the FOR instruction is used when a cycle has to be executed a predetermined number of times. The control variable 'x' of the cycle is initialised, monitored and modified by the actions of the FOR instruction. It can be used in the body of the cycle, but should never be modified. The start and end values of the cycle can be either ascending or descending: it is the user agent's task to increase or decrease the control variable.

Several FOR cycles can be nested together. For example, the code below prints a graph of circles that intersect throughout the sheet.

- For x = 1 to 10

- For y = 1 to 10

- Circle(x*10, y*10, 8)

- EndFor

- EndFor

This means that the control variable is incremented or decremented by one unit per iteration. The increment or decrement step, however, can be explicitly defined using the keyword "Step". The "Step" instruction is optional.

For example:

- For x = 1 to 10 steps 2

- Circle(x*10, 50, 8)

- EndFor

We have changed the increment step by setting it explicitly to 2. Concretely it means that 5 circles will be drawn, and no longer 10. The control variable "x" will be incremented by two units, so the cycle will only be repeated five times instead of ten. The Step value can only be positive, since it will think the user agent to use it either as an increase or as a decrease of the control variable. Even if a negative number is used, it will be reported as positive before use.

The start, end and step values can only be numerical. The number type includes all integers and floating point numbers. Therefore, we can also use floating point numbers in the FOR cycle.

For example:

- For x = 0 to 3.13 steps 0.01

- ...

- EndFor

Iteration of a FOR cycle can be stopped using the Break instruction. When you meet the Break instruction the execution goes on to execute the instruction immediately after the body of the cycle.

For example:

- For x = 1 to 10

- If x > 5

- Break

- EndIf

- Circle(x*10, 50, 8)

- EndFor

The example is of course a non-sense, because the If instruction interrupts the cycle when the control variable "x" exceeds 5. The Break instruction should usually not be used, as it has more suitable tools for writing a cycle. However, if necessary, its use can be tolerated.

The Continue instruction has a similar effect to the Break on cycle execution. It is interrupted but, unlike Break, execution returns to the beginning, the control variable is updated and, if necessary, the body of the cycle is repeated.

For example:

- For x = 1 to 10

- If (x >= 4) and (x <= 7)

- Continue

- EndIf

- Circle(x*10, 50, 8)

- EndFor

In the example, a horizontal line of circles is drawn. However, the fourth, fifth, sixth and seventh circles are not drawn, thanks to the If instruction, which restarts the cycle if the variable "x" is equal to or greater than 4 and less than or equal to 7. As with the Break instruction, Continue can be replaced by more powerful instructions that make the code more readable.