#### 4.1.2 Control structures

Following structures had been introduced so that entering commands is made easier:

• the alternative: if then else fi,
• the Conditional loop: while do od,
• the repeat loop: repeat until od,
• and the iterating loop: for do od.

• The command Set (assignment) can be replaced with :=, eg: x:=2 instead of Set(x,2). The Set command is evaluating the first argument alphanumerically, that is to say that if $k$ is a variable with the value $2$, then the command Set( ["x",k], 5) will be interpreted as : Set(x2,5). This can also be done with the assignment symbol: ["x",k]:= 5.
• If $x$ is a variable containing a list, the command Copy(x, n, 1), that is returning the n-th element of the list, can be replaced by : x[n]. In general: x[start, number] with the convention that if number=$0$ then we go until the end of the list, and if start=$-1$ then we start from the end of the list backwards.

NB: The command x[n]:=1 won’t change the n-th element of the list $x$, because x[n] is a value ! It is the replace macro that can modify the list’s elements : replace(x, n, 12) will replace the n-th element of the $x$ list (that must be a variable) with the value $12$. The replacement value can also be a list.

##### The alternative

This is equivalent to the if command. That function returns the Nil value.

• if <condition1> then <instructions> elif <condition2> then ... else <instructions> fi
• Description: <condition> is a boolean expression, whose value is 0 (false) or 1 (true), elif stands for the contraction of else and if, so that tests cascads are allowed. Instructions are separated with a comma.
• Exemple(s): A piecewise defined function of the $t$ variable:
if t<=0 then 1-t elif t<pi/2 then cos(t) else t^2 fi

to draw such a function, it is a better way to create a macro that is representing the function. For example, create a macro called f defined with the command if %1<=0 then 1-%1 elif %1<pi/2 then cos(%1) else %1^2 fi, the sequence %1 represents the first parameter of the macro. We can then create a graphical element (curve) by giving it a name and the parameters :t+i*f(t) or t+i*$\$f(t), in that second version, $f\left(t\right)$ is directly replaced by its expression.

##### The conditional loop

This is a version of the command:Loop.

• while <condition> do <instructions> od
• Description: <condition> is a boolean expression, whose value is 0 (false) or 1 (true). Instructions are separated by a comma.
• Exemple(s): List of cubes under 1000:
[x:=0, k:=0, while x<=1000 do x, Inc(k,1), x:=k^3 od]

Running that command (in the command line at the bottom of the window) gives: [0, 1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]. The first loop’s instruction (x) returns $x$’s value, the second (Inc) adds 1 to the $k$ variable and returns Nil, the third (:=) assigns the cube of $k$ to the variable $x$ and returns the value Nil.

##### The iterative loop

This is a version of the commands Seq and Map. There are two syntaxes:

• for <variable> in <list of values> [step <step> or by/By <package of>] do <instructions> od
• Description: For each value of the variable taken in the list, the instructions are executed. The step is 1 by default that is to say that the values are read one by one. The option by or By permits to read the values by packets and handling the case of the jump constant : with the by option the structure returns jump if there is one in the list. With the option By, jump is not returned. If the last packet is incomplete, it is not treated.

To follow a list by components (two components are separated by a jump), we use by jump or By jump. For example : for z in [1,2,jump,3,4,5] by jump do sum(z) od returns [3,jump,12], but for z in [1,2,jump,3,4,5] By jump do sum(z) od returns [3,12].

• for <variable> from <initial value> to <final value> [step <step> ou by/By <packets of>] do <instructions> od
• Description: For each variable’s value from the start to the final value, the instructions are executed. The value is incremented with the step (1 by default). The step can be negative and a non integer real. The by/By option is the same as above.

NB: step and by can’t be used at the same time.

NB: While following a list by component (by/By jump), the constant sep is containing the value of the jump that is ending the current component. That value is in fact a particular complex, this is only the real part that gives the jump status (1E308), the imaginary part can be used to store a numerical information.

• Exemple(s)
• The command for m in [-1,-0.25,0.5,2] do Color:=4*m, Courbe(t+i*t^m) od used in a User graphical element is useful to draw a cartesian curve family: $t↦{t}^{m}$ for m varying in the list $\left[-1,-0.25,0.5,2\right]$, For each value of $m$ the color of the drawing is also changed.
• The command for k from -2*pi to 2*pi step pi/2 do Droite(1,0,k) od put in a User graphical element will allow to draw the lines whose equations are: $x=-2\pi$, $x=-2\pi +\pi ∕2$, ..., $x=2\pi$.
• Browsing by packet with the option by or By: The command for z from 1 to 7 by 2 to z, jump od returns: [1, 2, jump, 3,4, jump, 5, 6, jump]. In that example, the z variable takes successively the values [1, 2], [3, 4], and [5, 6], the last packet is incomplete, therefore not treated
• Browsing with condition: the command for k in [1, 8, 4, 3, 2, 6, 5, 7] by 3 andif min(k)<=4 do k[1]+k[3] odfi returns [5,9].