Syntax:
while <expression1> do <expression2> until <expression1> do <expression2> do <expression2> while <expression1> do <expression2> until <expression1>
These are similar to other languages. However, as in GEL it is simply an expression that must have some return value, these
constructs will simply return the result of the last iteration or NULL
if no iteration was done. In the boolean expression, =
is translated into ==
just as for the if
statement.
Syntaxe :
for <identifier> = <from> to <to> do <body> for <identifier> = <from> to <to> by <increment> do <body>
Boucle où l'identifiant (identifier) prend toutes les valeurs comprises entre <from>
et <to>
, avec en option un incrément autre que 1. Elles sont plus rapides, plus simples à utiliser et plus compactes que les boucles classiques ci-dessus mais moins flexibles. L'identifiant doit être un identifiant et ne peut pas être un déréférencement. La valeur de l'identifiant est la dernière valeur prise par l'identifiant ou <from>
si le contenu (body) de la boucle n'a jamais été évalué. Vous êtes sûr que la variable est initialisée après une boucle donc vous pouvez l'utiliser sans risque. <from>
, <to>
et <increment>
ne doivent pas être complexes. Il n'est pas sûr que la valeur <to>
soit atteinte, mais elle n'est jamais dépassée. Par exemple, ce qui suit affiche les nombres impairs de 1 à 19 :
for i = 1 to 20 by 2 do print(i)
When one of the values is a floating point number, then the final check is done to within 2^-20 of the step size. That is, even if we overshoot by 2^-20 times the "by" above, we still execute the last iteration. This way
for x = 0 to 1 by 0.1 do print(x)
does the expected even though adding 0.1 ten times becomes just slightly more than 1.0 due to the way that floating point numbers are stored in base 2 (there is no 0.1, the actual number stored is just ever so slightly bigger). This is not perfect but it handles the majority of the cases. If you want to avoid dealing with this issue, use actual rational numbers for example:
for x = 0 to 1 by 1/10 do print(x)
This check is done only from version 1.0.16 onwards, so execution of your code may differ on older versions.
Syntax:
for <identifier> in <matrix> do <body>
For each element in the matrix, going row by row from left to right we execute the body with the identifier set to the current element. To print numbers 1,2,3 and 4 in this order you could do:
for n in [1,2:3,4] do print(n)
If you wish to run through the rows and columns of a matrix, you can use the RowsOf and ColumnsOf functions, which return a vector of the rows or columns of the matrix. So,
for n in RowsOf ([1,2:3,4]) do print(n)
will print out [1,2] and then [3,4].
Vous pouvez aussi utiliser les instructions break
et continue
dans les boucles. L'instruction continue
reprend la boucle à sa prochaine itération alors que l'instruction break
sort de la boucle actuelle.
while(<expression1>) do ( if(<expression2>) break else if(<expression3>) continue; <expression4> )