Skip to content

FOR ​

Execute and repeat a part of code a finite number of times, either for a number between a range or each token in a list.

Syntax ​

Syntax for lopping a number between two values:

leo-grammar
FOR VariableName from:Number to:Number [ by:Number ] {
  # Body with commands ...
} .
# Note: the curly brackets are needed (terminal symbols).

Syntax for iterating through a token list:

leo-grammar
FOR VariableName in:tokenStr [ sep:strValue ] {
  # Body with commands ...
} .
# Note: the curly brackets are needed (terminal symbols).

Parameters ​

Parameters for lopping a number between two values:

  • VariableName (LowerCaseIdentifier) - The name of the variable to set the numeric value and modify with each iteration.
  • from (Number) - The value at which the variable should start. The value can be either an intValue or a realValue.
  • to (Number) - The value for the variable until which the loop should be executed. The value can be either an intValue or a realValue.
  • by (Number, optional) - The amount by which the variable should be changed with each iteration. If from is smaller than to then this should be a positive value, otherwise it should be a negative value. The value can be either an intValue or a realValue. The default is 1.

Parameters for iterating through a token list:

  • VariableName (LowerCaseIdentifier) - The name of the variable to set with the value of each token for each iteration.
  • in (tokenStr) - The string with the token list to iterate through.
  • sep (strValue, optional) - A single character used as the separator. The default is a space " ".

Details ​

The FOR command allows to repeat a set of commands a finite number of times. Two variants are available:

  1. A numeric variant, where a variable is set to a value between two values with each iteration.
  2. A token list variant, where a variable is set to each token of a string list in order.

The numeric variant repeats the set of commands according to a variable between two values. First the from value is assigned to the variable and the loop is executed. At the end of the loop, the value of by (default 1) is added to the variable. Afterwards the loop is executed again. This procedure is repeated until the value of the variable reaches or exceeds the to value. The value of from can be larger than the value of to, but in such a case a negative value must be provided for the by parameter.

The token list variant repeats the set of commands according to a variable moving through a series of tokens. This token sequence is specified with in. Optionally, sepcan specify a token separator (otherwise the blank is used as default).

These commands can be used in combination with the commands that set variables, like AVAL or BITMAPINFO, to realize different graphical representations for different objects of the same class.

See Also ​

Examples ​

Draw five darkgray squares, descending towards the bottom-right.
Five dark gray squares, descending towards the bottom-right

leo
GRAPHREP
SHADOW off

SET dSquareSize:1cm

PEN color:"darkgray"
FILL color:"white"
SET dSquareOffset:(dSquareSize / 2)
FOR nSquareNumber from:1 to:5 {
  RECTANGLE x:((nSquareNumber-1) * dSquareOffset) y:((nSquareNumber-1) * dSquareOffset) w:(dSquareSize) h:(dSquareSize)
}

Draw many small circles along a sinus curve, each one in a different color hue.
63 small circles, each one in a different color along a sinus curve

leo
GRAPHREP
SHADOW off

FOR nX from:-3.14 to:3.14 by:0.1 {
  # Using the nX variable to set the "hue" in the HSV color space.
  PEN color:(hsv2rgb(hsvval(((nX + 3.14) / 6.28) * 359, 1, 0.9)))
  # Calculating the y position using the sinus function (+y in GraphRep is different from typical mathematical y-axis).
  ELLIPSE x:(CM nX) y:(CM sin(nX)) rx:0.2cm ry:0.2cm
}
# In total the loop is executed 63 times.

Draw a square of 30 by 30 smaller squares, showing various saturation and value levels for the color red.
30 by 30 small squares with various saturation and value levels for the color red

leo
GRAPHREP
SHADOW off

PEN style:null
SET nCount:30 dSize:0.2cm
FOR nX from:0 to:(nCount-1) {
  FOR nY from:1 to:(nCount-1) {
    FILL color:(hsv2rgb(0, nX / nCount, nY / nCount))
    RECTANGLE x:(nX * dSize) y:((nCount - nY) * dSize) w:(dSize) h:(dSize)
  }
}

Draw several small squares, each one filled with a different color.
Six squares from left to right filled with different colors: red yellow green cyan blue magenta

leo
GRAPHREP
SHADOW off

SET sColors:"red yellow green cyan blue magenta"
SET dX:0cm dW:0.5cm
FOR sC in:(sColors) {
  FILL color:(sC)
  RECTANGLE x:(dX) y:-0.5cm w:(dW) h:0.5cm
  SET dX:(dX + dW)
}

Draw a small square for each color value selected in the enumeration list attribute "Colors".
Six squares from left to right filled with different colors: red yellow green cyan blue magenta

leo
GRAPHREP
SHADOW off

AVAL sColors:"Colors"
# Enumeration lists use a \r\n as a separator in GraphRep and also always end with an \r\n, which we change to a single ;
SET sColors:(replall(sColors, "\r\n", ";"))
# If there are several tokens, then remove the last ; at the end to only draw a black square if nothing is selected.
SET sColors:(cond(tokcnt(sColors, ";") > 2, copy(sColors, 0, (LEN sColors) - 1), "black"))

SET dX:0cm dW:0.5cm
FOR sC in:(sColors) sep:";" {
  FILL color:(sC)
  RECTANGLE x:(dX) y:-0.5cm w:(dW) h:0.5cm
  SET dX:(dX + dW)
}

Versions and Changes ​

Available since ADOxx 1.3