if-then-else expression in [expr] and [codebox]?

Feb 12, 2012 at 12:43pm

if-then-else expression in [expr] and [codebox]?

Hi all-

I know there are other solutions, but is there an if-then-else or equivalent in Gen’s [expr] and [codebox]?

I seem to remember a statement that there was, but now can’t find any documentation.

Best wishes, Charles

Feb 12, 2012 at 2:03pm

have a look here, the same applies to expr inside gen: http://cycling74.com/forums/topic.php?id=38012

about codebox, i haven’t tried yet but i understood it takes C code so i guess if/then/else statements should work: http://cycling74.com/2011/12/12/week-47-waveshaping-with-gen/

Feb 12, 2012 at 3:48pm

no, codebox doesn’t take if, then, else, for, etc. but apparently it will one day.

codebox is not C. it just feels a little like it. but it is very very simple and easier than javascript or lua.

in gen and codebox i assign first and use [switch]:
x = w ? y : z;
or [selector] or [gate] (same as max land).

Feb 12, 2012 at 3:52pm

tanx for pointing that out!

Feb 12, 2012 at 4:38pm

@pid, @dtr-

Thanks for pointing all this out.

Best, C.

Feb 14, 2012 at 1:27am

I was sitting with a similar problem the last days, missing if-else, it looks like there is an example of how to get around it,using ? operator within codebox,, needing extra lines of code, intermittent variables
its about pd filters realized in gen
a bit down the page, porting c code to gen, >>stkr< < writes himself : (looks ridiculous - getting around GenExpr limitations)


Feb 14, 2012 at 12:21pm

Well, [gen~] is a synchronous language. Just because it has a C-like syntax…

Someone could prove me wrong, but I think it has to do with preserving state across time spent in the rendering loop. Currently with [gen~] everything is forgotten when the audio interrupt is exited. There are exceptions at the object level, like [sah].

But if conditionals have got your nose twisted, try anything that changes the flow of time, like a “for” or “while” loop!

Best, Charles

Feb 14, 2012 at 3:31pm

greetings and just IF more people follow this if-else ? discussion THEN this tiny simple example is working, its doing C*sin(x) where C is variable parameter and x is fed back in the loop and trig is a running sample counter input, so that there will only be a new output every so many samples.

x = in1;
c = in2;
trig = in3;
xout = (trig == 1) ? c * (sin (x)) : x;
out1 = xout;

here on my babysocks in the code landscapes i hope that in this way there is some some cpu saving in the sense that the csinx calculation is only done IF trig==1.. for more complex algorithms this would matter i bet.


You must be logged in to reply to this topic.