while(
boolean testFunction(
number)
, bodyFunction(
number)
)
;
testFunction.while(
bodyFunction(
number)
)
;
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”.
( 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.
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.