Product SiteDocumentation Site

11.3.7.3. Example "Do" Loops

These examples illustrate different ways to use "do" loops for trivial tasks.
(
   var tL = List.new;
   tL = [27, 46, 102, 81, 34, 0, 39, 26, 203, 62];

   do( tL, { arg item, rep; [rep, item].postln; }; );

   nil;
)
This example is of the first syntax shown. For each element in tL, the interpreter executes the function once, giving it first the corresponding element of the Collection, and then the iteration counter, which happens to be equal to the element's List index number.
(
   var tL = List.new;
   var myFunc =
   {
      arg item;
      item.postln;
   };

   tL = [27, 46, 102, 81, 34, 0, 39, 26, 203, 62];

   tL.do( myFunc; );

   nil;
)
This example does several things differently, but maintains the same basic functionality as the previous example. In this case, the Function only uses the first argument that the interpreter provides, and completely ignores the iteration counter. The syntax here also puts the Collection outside the parentheses, which perhaps makes it more clear that tL is not part of the function.
(
   10.do( { "repeat".postln; }; );

   nil;
)
This example simply prints the string "repeat" ten times. If the Function accepted one argument, it would receive the integers zero through nine. If it accepted two arguments, both of the arguments would be equal.