[Csnd] A for-loop statement for Csound
Date | 2018-08-18 22:37 |
From | Mauro Giubileo |
Subject | [Csnd] A for-loop statement for Csound |
Hi all, for (i1 = 0; i1 < ksmps; i1++) ... but then I remember that Csound doesn't have a 'for' statement (and it doesn't neither have the '++' operator) and have to rewrite it with a while... The problem I have with the while construct is that sometimes I forget to write the increment statement, so when I run the code, the while goes in an infinite loop... :-D I love the C-style for construct, because it's compact and because its particular syntax help me to not forget to specify the increment statement. So I thought I could make something similar in Csound with its macro system and I want to share my results with you: ; ADD "FOR/NEXT" STYLE SYNTAX TO CSOUND: If you place these two define in the global area of your code, then you could write something like: $FOR(i1 = 1 ' i1 <= 10) That is similar to a C-style for with the difference that the increment statement has to be placed as a parameter of the $NEXT macro... This is to make sure that the increment is done only after the code part between $FOR and $NEXT.
A very nice thing of this syntax is that if you forget to write the increment statement, Csound gives an error and stops, instead of doing an infinite loop. Moreover I also like that the above syntax is more compact but not less readable than:
i1 = 1
while (i1 <= 10) do printf_i "For iteration number: %d\n", i1, i1 i1 += 1 od Best Regards,
Mauro |