1 title::Control Structures
4 related:: Classes/Boolean
6 Control structures in SuperCollider are implemented via message sends. Here are a few of those available.
7 See link::Reference/Syntax-Shortcuts:: for the various ways expressions can be written.
9 section:: Basic Control Structures
13 Conditional execution is implemented via the code::if:: message. The code::if:: message is sent to an expression which must return a link::Classes/Boolean:: value.
14 In addition it takes two arguments: a function to execute if the expression is true and another optional function to execute if the expression is false. The code::if:: message returns the value of the function which is executed. If the falseFunc is not present and the expression is false then the result of the if message is nil.
19 if (expr, trueFunc, falseFunc);
23 expr.if (trueFunc, falseFunc);
28 if ( [false, true].choose, // Boolean expression (chooses one at random)
29 { "expression was true".postln }, // true function
30 { "expression was false".postln } // false function
35 z = if (a < 5, { 100 },{ 200 });
41 if (x.isNil, { x = 99 });
50 The while message implements conditional execution of a loop. If the testFunc answers true when evaluated, then the bodyFunc is evaluated and the process is repeated. Once the testFunc returns false, the loop terminates.
55 while ( testFunc, bodyFunc );
59 testFunc.while( bodyFunc );
66 while ( { i < 5 }, { i = i + 1; "boing".postln });
70 while expressions are also optimized by the compiler if they do not contain variable declarations in the testFunc and the bodyFunc.
75 The for message implements iteration over an integer series from a starting value to an end value stepping by one each time. A function is evaluated each iteration and is passed the iterated numeric value as an argument.
80 for ( startValue, endValue, function )
84 startValue.for ( endValue, function )
89 for (3, 7, { arg i; i.postln }); // prints values 3 through 7
94 The forBy selector implements iteration over an integer series with a variable step size. A function is evaluated each iteration and is passed the iterated numeric value as an argument.
99 forBy ( startValue, endValue, stepValue, function );
103 startValue.forBy ( endValue, stepValue, function );
108 forBy (0, 8, 2, { arg i; i.postln }); // prints values 0 through 8 by 2's
114 Do is used to iterate over a link::Classes/Collection::. Positive Integers also respond to code::do:: by iterating from zero up to their value. Collections iterate, calling the function for each object they contain. Other kinds of Objects respond to do by passing themselves to the function one time. The function is called with two arguments, the item, and an iteration counter.
119 do ( collection, function )
123 collection.do(function)
128 [ 1, 2, "abc", (3@4) ].do({ arg item, i; [i, item].postln; });
130 5.do({ arg item; item.postln }); // iterates from zero to four
132 "you".do({ arg item; item.postln }); // a String is a collection of characters
134 'they'.do({ arg item; item.postln }); // a Symbol is a singular item
136 (8..20).do({ arg item; item.postln }); // iterates from eight to twenty
138 (8,10..20).do({ arg item; item.postln }); // iterates from eight to twenty, with stepsize two
140 Routine({ var i=10; while { i > 0 } { i.yield; i = i - 5.0.rand } }).do({ arg item; item.postln });
143 Note:: The syntax code::(8..20).do:: uses an optimization to avoid generating an array that is used only for iteration (but which would be discarded thereafter). The return value of code:: (8..20).do({ |item| item.postln }) :: is 8, the starting value.
145 (8..20) do: { |item| item.postln } // is not optimized, and returns the array.
151 Object implements a switch method which allows for conditional evaluation with multiple cases. These are implemented as pairs of test objects (tested using if this == test.value) and corresponding functions to be evaluated if true.
152 The switch statement will be inlined if the test objects are all Floats, Integers, Symbols, Chars, nil, false, true and if the functions have no variable or argument declarations. The inlined switch uses a hash lookup (which is faster than nested if statements), so it should be very fast and scale to any number of clauses.
158 testvalue1, trueFunction1,
159 testvalue2, trueFunction2,
161 testvalueN, trueFunctionN,
168 var x=0; //also try 1
169 switch(x,0,{"hello"}, 1, {"goodbye"})
174 z = [0, 1, 1.1, 1.3, 1.5, 2];
175 switch (z.choose.postln,
189 z = [0, 1, 1.1, 1.3, 1.5, 2];
190 x = switch (z.choose)
203 Function implements a case method which allows for conditional evaluation with multiple cases. Since the receiver represents the first case this can be simply written as pairs of test functions and corresponding functions to be evaluated if true. Case is inlined and is therefore just as efficient as nested if statements.
209 z = [0, 1, 1.1, 1.3, 1.5, 2];
213 { i == 1.1 } { \wrong }
214 { i == 1.3 } { \wrong }
215 { i == 1.5 } { \wrong }
216 { i == 2 } { \wrong }
217 { i == 0 } { \true };
222 section:: Other Control Structures
224 Using Functions, many control structures can be defined like the ones above. In the class link::Classes/Collection#iteration:: there are many more messages defined for iterating over Collections.
226 section:: Inline optimization
228 code::if::, code::while::, code::switch:: and code::case:: expressions are optimized (i.e. inlined) by the compiler if they do not contain variable declarations in the functions. The optimization plucks the code from the functions and uses a more efficient jump statement:
241 4 E6 SendSpecialBinaryArithMsg '=='
242 5 F8 00 06 JumpIfFalse 6 (14)
243 8 42 PushLiteral "hello"
244 9 A1 00 SendMsg 'postln'
245 11 FC 00 03 JumpFwd 3 (17)
246 14 41 PushLiteral "hello"
247 15 A1 00 SendMsg 'postln'
249 a FunctionDef in closed FunctionDef
252 Failure to inline due to variable declarations:
263 WARNING: FunctionDef contains variable declarations and so will not be inlined.
264 in file 'selected text'
268 -----------------------------------
272 4 E6 SendSpecialBinaryArithMsg '=='
273 5 04 00 PushLiteralX instance of FunctionDef in closed FunctionDef
274 7 04 01 PushLiteralX instance of FunctionDef in closed FunctionDef
275 9 C3 0B SendSpecialMsg 'if'
277 a FunctionDef in closed FunctionDef