Control Structures:
Filter:
Reference | Language

Control Structures

flow control

Control structures in SuperCollider are implemented via message sends. Here are a few of those available. See Syntax Shortcuts for the various ways expressions can be written.

Basic Control Structures

if

Conditional execution is implemented via the if message. The if message is sent to an expression which must return a Boolean value.

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 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.

Discussion:

Syntax

Examples

while

The while message implements conditional execution of a loop. If the testFunc answers true when evaluated, then the loopFunc is evaluated and the process is repeated. Once the testFunc returns false, the loop terminates.

Discussion:

Syntax

Example

while expressions are also optimized by the compiler if they do not contain variable declarations in the testFunc and the loopFunc.

for

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.

Discussion:

Syntax

Example

forBy

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.

Discussion:

Syntax

Example

do

Do is used to iterate over a Collection. Positive Integers also respond to 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.

Discussion:

Syntax

Example

NOTE: The syntax (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 (8..20).do { |item| item.postln } is 8, the starting value.

However, if do is written as an infix binary operator, as in (8..20) do: { |item| item.postln }, then it will generate the series as an array first, before calling Array:do. One side effect of this is that it is valid to do over an infinite series within a routine only if do is written as a method call. If it is written as a binary operator, you will get a "wrong type" error because the array must be finite.

switch

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.

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.

NOTE: Hash lookup by definition implies testing identity rather than equality: a switch construction that is not inlined will test ==, while one that is inlined will test ===. See the examples. One implication is that strings should be avoided: switch(text) { "abc" } { ... } may not match, even if text == "abc". Symbols are preferred.
NOTE: Floating point numbers may sometimes appear to be equal while differing slightly in their binary representation: (2/3) == (1 - (1/3)) is false. Therefore floats should be avoided in switch constructions.

Discussion:

Syntax

Examples

or:

Inlined vs non-inlined comparison:

The identity comparison 1.0 === 1 is false: while 1.0 and 1 represent the same numeric value, one is floating point and the other is an integer, so they cannot be identical.

case

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.

Discussion:

Example

try

Function implements a try method which allows catching exceptions thrown by code and run the exception handler. For more information see Exception.

Discussion:

Example

protect

Function implements a protect method which allows protecting code from exceptions thrown by code. The difference with try is the exception handler code is always run, even if there is no exception. If an exception occurs, it is rethrown after the exception handler block is run. For more information see Exception.

Discussion:

Example

Other Control Structures

Using Functions, many control structures can be defined like the ones above. In the class Collection: Iteration there are many more messages defined for iterating over Collections.

Inline optimization

if, while, switch and 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:

Failure to inline due to variable declarations:

You can switch on and off the above warning (see: LanguageConfig: *postInlineWarnings)