if(<bool>,<expr>)
<expr>
is evaluated, if the Boolean condition <bool>
evaluates to true
. In this case the return value of the if
-function is <expr>. Otherwise, ___
is returned. A typical use of the if
-operator is the conditional evaluation of side effects. x
has a negative value. if(x<0,println("x is now negative"))
if(<bool>,<expr1>,<expr2>)
<expr1>
is evaluated, if the Boolean condition <bool>
evaluates to true
. If <bool>
evaluates to false
, then <expr2>
is evaluated. In any 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: Firstly, the if
-operator is used to force the conditional evaluation of program parts (which usually causes side effects).x
is positive, negative, or zero.if(x<0, println("x is now negative"), if (x>0, println("x is now positive"), println("x is 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)
to be the absolute value function (for real values of x
).f(x):=if(x>0,x,-x)
A.color=if(A.x>0,(1,0,0),(0,0,1))
trigger(<bool>,<expr>)
trigger
operator is very similar to the if
operator. In contrast to if
, the trigger
operator has a dynamic flavor. The expression <expr>
is evaluated whenever <bool>
changes from false
to true
. This means that during the dragging of a construction, <expr> is 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.A
crosses the y-axis.trigger(A.x<0,println("A now entered the x-negative half-plane")) trigger(A.x>0,println("A now entered the x-positive half-plane"))
while(<bool>,<expr>)
while
operator evaluates the expression <expr>
as long as the condition <bool>
is true. The result of the very last evaluation is returned as the function's value.x=0; sum=0; erg=while(x<4, x=x+1; sum=sum+x; println(x+" --> "+sum); sum ); println(erg);
1 --> 1 2 --> 3 3 --> 6 4 --> 10 10
erg
is 10
. A word of caution: one should be aware of the fact that while
operations may easily create infinite loops, if the conditional is never satisfied.repeat(<number>,<expr>)
<expr>
is evaluated <number>
times. The result of the last evaluation is returned. During the evaluation of <expr>
the special variable #
contains the run variable of the loop. repeat(100, println(#+" squared is "+#^2) )
repeat
loop supports 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. Only real values are allowed for the modifiers. The table below demonstrates different uses of the modifiers. Code | Result |
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
|
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 its first argument. It produces a loop in which <expr>
is evaluated for each entry of the list. In every iteration, 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>
. eval(<expr>,<modif1>,<modif2>,…)
7
.eval(x+y,x->2,y->5)
createvar(<varname>)
removevar(<varname>)
createvar(x)
creates a new variable named 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 are automatically generated when they are first used. The createvar
and removevar
operators should only be used 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
regional(name1,name2,...)
local
statement and creates several local variables. However, unlike with the local
statement, the variables are removed automatically, when the function terminates. Therefore, an explicit call of release
is not necessary. Most often it is much more convenient to use regional
than to use local
. clear()
statement under the init
event of the program.clear()
clear(<var>)
<var>
. keys(<var>)
<object>:<key>=<something>
declaration. A:"age"=34; A:"haircolor"="brown";
println(A:"age"); println(A:"haircolor");
keys
returns a list of all associated keys of an object. So in this exampleprintln(keys(A));
["age","haircolor"]
.
Page last modified on Monday 09 of February, 2015 [20:09:42 UTC].
The original document is available at
http://doc.cinderella.de/tiki-index.php?page=Control%20Operators