if(<bool>,<expr>)
:<expr>
will be evaluated if the Boolean condition <bool>
evaluates to true
. In this case the value of <expr> is returned. Otherwise, _?_
is returned. A typical use of the if
-operator is the conditional evaluation of side effects.if(x<0,println("x is now negative"))
x
has a negative value.if(<bool>,<expr1>,<expr2>)
:<expr1>
will be evaluated if the Boolean condition <bool>
evaluates to true
. If <bool>
evaluates to false
, then <expr2>
is evaluated. In each case the value of the evaluated expression is returned. Thus this ternary version of the if
-operator encodes an if/then/else functionality. There are two typical uses of this version of the if
-operator: First, the if
-operator is used to force the conditional evaluation of program parts (which usually will cause side effects).if(x<0, println("x is now negative"), if (x>0, println("x is now positive"), println("x is zero") ) )
x
is positive, negative, or zero.if
-operator is to return a certain value depending on the condition encoded by <bool>
. This is particularly useful in the definition of functions.f(x):=if(x>0,x,-x)
f(x)
to be the absolute value function (for real values of x
).A.color=if(A.x>0,(1,0,0),(0,0,1))
A
(most probably a point) and sets its color to red or blue depending on the value of its x-coordinate.trigger(<bool>,<expr>)
:trigger
operator is very similar to the if
operator. In contrast to if
, the trigger
operator is has a dynamic flavor. The expression <expr>
will be evaluated whenever <bool>
changes from false
to true
. This means that during the dragging of a construction, <expr> will be evaluated if <bool> was false
in the previous instance and is now true
. The purpose of this operator is to trigger side effects whenever some event occurs while the construction is being dragged. The following code fragment demonstrates this behavior:trigger(A.x<0,println("A now entered the x-negative halfplane")) trigger(A.x>0,println("A now entered the x-positive halfplane"))
A
crosses the y-axis.while(<bool>,<expr>)
:while
operator evaluates the expression <expr>
as long as the condition <bool>
is true. The result of the very last evaluation will be returned as the function value.x=0; sum=0; erg=while(x<=4, println(x+" --> "+sum); sum=sum+x; x=x+1; sum )
0 --> 0 1 --> 1 2 --> 3 3 --> 6
erg
will be 6
.repeat(<number>,<expr>)
:<expr>
will be evaluated <number>
times. The result of the last evaluation will be returned. During the evaluation of <expr>
the special variable #
will contain the counting variable of the loop.repeat(100, println(#+" squared is "+#^2) )
repeat
loop admits a variety of modifiers. These modifiers can be used to control the start value, stop value, and step size of the loop. The modifier start
sets the start value of the loop. The modifier stop
sets the end value of the loop. The modifier step
sets the step size. Arbitrary combinations of modifiers are possible. As long as not all three modifiers are set, the loop will always be executed <number>
times. For the modifiers, only real values are allowed. The table below demonstrates different uses of the modifiers.Script Code: | output: |
repeat(6, println(#+" ") ) | 1 2 3 4 5 6
|
repeat(6, start->4, println(#+" ") ) | 4 5 6 7 8 9
|
repeat(6, stop->2, println(#+" ") ) | -3 -2 -1 0 1 2
|
repeat(6, step->3, println(#+" ") ) | 1 4 7 10 13 16
|
repeat(6, stop->12,step->4, println(#+" ") ) | -8 -4 0 4 8 12
|
repeat(6, start->3,step->2, println(#+" ") ) | 3 5 7 9 11 13
|
repeat(6, start->3,stop->4, println(#+" ") ) | 3 3.2 3.4 3.6 3.8 4
|
repeat(6, start->0,stop->-3 println(#+" ") ) | 0 -0.6 -1.2 -1.8 -2.4 -3
|
repeat(6, start->3,stop->4,step->0.4 println(#+" ") ) | 3 3.4 3.8 4.2
|
repeat(<number>,<var>,<expr>)
:repeat(<number>,<expr>)
, except for one difference: the run variable is now assigned to <var>
. This allows for the use of nested loops with different run variables.repeat(10,i, repeat(10,j, draw((i,j)) ) )
forall(<list>,<expr>)
:<list>
as first argument. It produces a loop in which <expr>
is evaluated for each entry of the list. For each run, the run variable #
takes the value of the corresponding list entry.a=["this","is","a","list"]; forall(a,println(#))
this is a list
forall(<list>,<var>,<expr>)
:forall(<list>,<expr>)
, but the run variable is now named <var>.module([<var1>,<var2>,… ],<expr>)
:module
defines a subroutine without arguments for which certain local variable names are reserved in a list. The variables will be created locally when execution of the module begins. The variables will be restored to their old values when the execution of the module terminates. Modules of this kind are also suitable for the generation of recursive routines. Each time the module is invoked, a new set of variables is generated.x=10; y="Hello"; module([x,y], x="new"; y=10; println("x is now "+x+" and y is now "+y); ) println("x is now "+x+" and y is now "+y);
x is now 10 and y is now Hello x is now new and y is now 10 x is now 10 and y is now Hello
eval(<expr>,<modif1>,<modif2>,…Ó)
:eval(x+y,x->2,y->5)
7
.createvar(<varname>)
:removevar(<varname>)
:createvar(x)
creates a new variable with name x
, while the old value is put on a stack. removevar(x)
removes the local variable and restores the value from the stack. Notice that usually, variables do not have to be created explicitly. They will be generated during their first use automatically. The createvar
and removevar
operators are used only if one wants to reserve a variable name for a certain local region of the code.x=10; println("x is now "+x); createvar(x); x=5; println("x is now "+x); removevar(x); println("x is now "+x);
x is now 10 x is now 5 x is now 10
local(name1,name2,...)
:release(name1,name2,...)
:clear()
statement in the init
part of the program.clear()
:clear(<var>)
:<var>
.
Page last modified on Wednesday 24 of May, 2006 [21:40:17 UTC].
The original document is available at
http://doc.cinderella.de/tiki-index.php?page=Control%20Operators