Hey guys,
I’m trying to understand how to replicate in SuperCollider a return/break mechanism like I am used in C++ and other programming languages.
In practice I’m trying to replicate this snippet of code (very basic for the sake of semplicity and just to stimulate the conversation):
int lookfor(int p)
{
for(int i=0; i<number_of_checks; i++)
if( p == check_array[i] ) return(1);
return(0);
}
I was able to use the block method on a function to create something I think is pretty similar.
Here’s the code of the function and also some code test:
Methods in a class definition can early-return using ^. (This is how includes does it.)
Functions have no equivalent of return from C/Java. So the first example in this thread is impossible to translate as a function token-for-token into SC (though it could be translated as a method).
block on a function is more like break in C. In a function, the closest you can get to early-return is to early-exit from an inner block.
Your block example doesn’t really need the variable, though – you could write it a little more tightly like this. Also for isn’t really idiomatic SuperCollider – we aren’t limited to C’s archaic control structures
I can think of one more way to simulate an early exit from a loop, though I can’t say this is “better” than block: yield. The Routine pauses at the exit point and then, instead of asking it to continue, you just discard it.