Else-if control structure / return statement

Hi guys,

I’m working on a problem I’m used to solve with an if/else if/else control structure:

if( myVar < 10 ) {
	//do something
} else if( myVar < 55 ) {
	//do something new
} else if( myVar < 73 ) {
	//do somenthing different
} else {
	//do something particular
}

In SC it seems there’s no else if control structure but only if/else.

A way I can solve my problem in SC would be to use a lot of nested if/else, but I would prefer not to do so: I find this approach difficult to scale up and also to read and debug.

Another way I can think would be to use many if/else control structures, each one with its own return statement.

Does the return statement exist in SC?
Can I use something like this?

if( myVar < 10, {
	// do something
	return;
});
if( myVar < 55 , {
	// do something new
	return;
});
if( myVar < 73, {
	// do somenthing different
	return;
});
if( myVar < 100, {
	// do something particular
	return;
});

What’s the best way create a control structure logic like this in SC?

As always, thank you so much for your help and support

Hello

Maybe you could use case ?

(
i = 12;

case
{ i < 10 } { "1".postln; }
{ i < 55 } { "2".postln; }
{ i < 73 } { "3".postln; };
)

Best

Geoffroy

3 Likes

Thank you so much @Geoffroy!
This is just what I was looking for :slight_smile:

See also the help file ‘Control Structures’

1 Like