Control Operators


Program Flow




The conditional operator if(<bool>,<expr>):


Description: The expression <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.

Example:

if(x<0,println("x is now negative"))


This code fragment reports a message on the console whenever x has a negative value.





The conditional branch operator if(<bool>,<expr1>,<expr2>):


Description: The expression <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).

Example:

if(x<0,                                   
   println("x is now negative"),      
   if (x>0,     
      println("x is now positive"),    
      println("x is zero")                
   )                                        
 )     


This code fragment reports a message on the console that shows whether x is positive, negative, or zero.

A second use of the if-operator is to return a certain value depending on the condition encoded by <bool>. This is particularly useful in the definition of functions.

Example:

f(x):=if(x>0,x,-x)  


This code fragment defines the function f(x) to be the absolute value function (for real values of x).

Example:

A.color=if(A.x>0,(1,0,0),(0,0,1))  


This code fragment takes a geometric element A (most probably a point) and sets its color to red or blue depending on the value of its x-coordinate.




The operator trigger(<bool>,<expr>):


Description: The 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:

Example:

trigger(A.x<0,println("A now entered the x-negative halfplane"))
trigger(A.x>0,println("A now entered the x-positive halfplane"))


This code fragment will print a message whenever point A crosses the y-axis.




The while loop while(<bool>,<expr>):


Description: The 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.

Example:

x=0;
sum=0;
erg=while(x<=4,
          println(x+"  -->  "+sum);
          sum=sum+x;
          x=x+1;
          sum
       )


This code fragment will produce the output

0  -->  0
1  -->  1
2  -->  3
3  -->  6


After the evaluation, the value of the variable erg will be 6.




The repeat loop repeat(<number>,<expr>):


Description: This operator provides the simplest kind of loop in CindyScript: <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.


Example:

repeat(100,
    println(#+" squared is "+#^2)
)


will produce a list of the first 100 integers together with their squares.

Modifiers: The 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




The repeat loop repeat(<number>,<var>,<expr>):


Description: This operator is identical to the operator 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.

Example:

repeat(10,i,
   repeat(10,j,
      draw((i,j))
   )
)


This code fragment will draw a 10 × 10 array of points.




The forall loop forall(<list>,<expr>):


Description: This operator takes a <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.

Example:

a=["this","is","a","list"];
forall(a,println(#))


This code fragment produces the output

this
is
a
list





The forall loop forall(<list>,<var>,<expr>):


Description: Similar to forall(<list>,<expr>), but the run variable is now named <var>.




Subprograms module([<var1>,<var2>,… ],<expr>):


Description: The operator 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.

Example:

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);



This code fragment produces the output

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


WARNING: This operator is currently not fully implemented




Forcing evaluation eval(<expr>,<modif1>,<modif2>,…Ó):


Description: This operator forces the evaluation of the expression <expr>. Free variables of the expression can be substituted by a list of modifiers. The variables for the substitution are assigned only locally. After this, the variables are set to the values they had before the evaluation.

Example:

eval(x+y,x->2,y->5)


This code fragment evaluates to the number 7.





Variable Management






Creating variables createvar(<varname>):

and

Destroying variables removevar(<varname>):


Description: These operators help in the manual administration of the creation of local variables. 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.

Example:

x=10;
println("x is now "+x);
createvar(x);
x=5;
println("x is now "+x);
removevar(x);
println("x is now "+x);


This code fragment produces the output

x is now 10
x is now 5
x is now 10





Creating many local variables local(name1,name2,...):


Description: Creates several local variables whose names are listed as arguments.




Removes many local variables release(name1,name2,...):


Description: Removes several local variables whose names are listed as arguments.
The value of the last one is returned as result of this operation.



Variables have a kind of persistence within CindyScript. If the value of a variable is set in one calculation, it remains set until it is changed. One can explicitly clear variables using the following operators. Often it is useful to put a clear() statement in the init part of the program.


Clear all variables clear():


Description: Invoking this operator clears all variables.




Clear a specific variable clear(<var>):


Description: Invoking this operator clears the variable <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