Product SiteDocumentation Site

11.3.7.4. "Do This While"

"While" loops execute continuously while their "test condition" is evaluated to be "true". Upon reaching the loop, the SuperCollider interpreter executes the test condition. If it is "fale", the interpreter does not execute the loop, and continues with the code after the loop. If it is "true", the interpreter executes the code in the loop once, then re-executes the test condition. If the test condition is "true", the loop is executed, the test condition re-executed, and so on. Until the test condition returns "false", the interpreter will never leave the loop.
Here is the format of a "while" loop in SuperCollider:

while(boolean testFunction(number), bodyFunction(number));

or like this:

testFunction.while(bodyFunction(number));

The test condition, called testFunc, is a function which returns a boolean value - either true or false. The loop's body, called bodyFunc, is a function which can do anything. The loop body function is not provided any arguments by the interpreter. You will have to use comparison operators and boolean expressions when writing the Function for the test condition. For information on how these work in SuperCollider, see Section 11.3.8.1, “Boolean Operators” and Section 11.3.8.2, “Boolean Expressions”.
The following three code blocks are equivalent:
(
   10.do( { "repeat".postln; }; );
)
and
(
   var counter = 0;
   while( { counter < 10; }, { "repeat".postln; counter = counter + 1; } );
)
and
(
   var counter = 0;
   { counter < 10; }.while( { "repeat".postln; counter = counter + 1; } );
)
You can see how it's easier to write this particular activity as a "do" loop. It's often the case that a "do" loop better reflects what you want to do, but not always.
Contemplate a situation where you are waiting for the user to input some information, which you're going to use to calculate the rest of the composition. The following example isn't real code. It's intended to simplify a complex situation, so you can see where a "while" loop makes more sense than a "do" loop.
play( some background music );
while( { is the user still inputting information? }, { keep playing music } );
stop( some background music );
The background music is begun, and then the interpreter would enter the loop. For as long as the user is still inputting information, the interpreter will then "keep playing music." When the user is not still inputting information, the interpreter will move on to the next command, which stops the music. An equivalent "do" loop would be very difficult to write, if not impossible. This is because we won't know when the user has finished inputting their information until after they've finished, so we can't plan in advance for how long to play background music.
Thus, the most appropriate use of a "while" loop is for cases where you cannot know in advance how many times something should be executed. For most other cases of repeated execution, a "do" loop is the most appropriate choice.