The if statment and syntax

Feb 15, 2011 at 3:42pm

The if statment and syntax

Hi all

I have a number box that updates giving me an average value for whats going on in a matrix (the simple version)

I want to apply a statement to it that evalutes the number and depending on what it is the output will change accordingly. If n is the number i want something like:

if n < = 10 then out1,
if n >10 but =<20 then out2,
if n >20 but =<30 then out3,
etc … up to 100 (or 127 for a midi style change to my instrument – either is fine)

Firstly how do i create an inlet for the ‘if’ itself, it just comes up as an object with no inlets or outlets

secondly the syntax (oh the syntax)

The max documentation sometimes uses the £i1 or £/1 to indicate variable 1, whats the difference?

finally I have looked at the ‘if’ help docs but they don’t cover where im going wrong

#54953
Feb 15, 2011 at 3:52pm

Hi Ali,
the object you want is [split]…….you should find references to [if] on this forum complaining about its unwieldiness (?): use [number] -> [split 0 10] (out right) -> [split 10 20]..etc. I get lots of mileage from the ‘See Also’ menu in such objects’ helpfiles.

The syntax usually follows this structure eg; [if $i1 < 10 then bang else out2 $i1]; the three types of variable are integer, float, symbol (text character[s]): $i1, $f1, $s1.
Brendan

#197891
Feb 15, 2011 at 4:04pm

Cheers Brendan, will look into it now,

Here’s the basic version of the patch im working on by the way of anone fancies tinkering around

#197892
Feb 15, 2011 at 4:10pm

I sort of get what you mean Brendand but how would you write the expression?

for example my understanding would be:

if$i1 < 10 out 1,
if$i1 >10 but < 20 out2
etc.etc

which would then create more outlets for the if object

sending a bang for each outlet would work (as i wnat it to send a message to play a harmonic instrument so if out 1 was 100Hz, out2 was 220Hz etc etc it should work

Cheers for the help
Ali

#197893
Feb 15, 2011 at 4:47pm

1. afaik, [expr] will not take multiple statements (as in ‘elseif’), just multiple conditions: “or” = ||; “and” = &&, etc, for example:

[if $i1 > 0 && $i1 < 10 then bang else out2 $i1], then hook up outlet right to another [expr]. Other readers might be able to create something more flexible using the [vexpr] object but……

2. for this task, do not use multiple [if] statements, I’ve been reprimanded for doing so myself; just passing on the good advice. Use [split]. Not [if].

K?

Brendan

#197894
Feb 15, 2011 at 4:51pm
– Pasted Max Patch, click to expand. –
#197895
Feb 15, 2011 at 4:56pm

Awesome, thanks Brendam, thanks Pid

It makes sense now

Cheers for your help guys :)

#197896
Feb 15, 2011 at 4:56pm

 
1.

not only IF and EXPR but several objects will only show their inlets when
the required arguments are there.
 
 
2.

[if $i1<10 then $i1] (you dont need to mention “out1″)

[if $i1<10 then $i1 else out2 $i1] (there are always only two outs)
 
 
you can use AND or OR:

[if $i1<10 && $i1>3 then $i1]

[if $i1<10 || $i1>3 then $i1]
 
 
you can have up to 9 inlets:

[if $i1>$i2 && $i1>$i3 && $i1>$i4 && $i1>$i5 && $i1>$i6 && $i1>$i7 && $i1>$i8 && $i1>$i9 then $i1]
 
 
you may use brackets and spaces for better readabilty:

[if   ($i1<10)   &&   ($i1>3)   then   $i1]
 
 
the if statement itself may also contain basic math expressions:

[if int(log(((max((($i1)%12)+1,400)/3.14),90.)) >=10) then $i1]
 
 
but not on the output side:

[if $i1<10 then ($i1*5)] (WRONG)
 
 
the output may be a constant or symbol, though:

[if $i1<10 then bang else out2 bing]

[if $f1==7 then 1 else 0] (is the same as [== 7.])
 
 
that should be all.
 
 
-110

#197897
Feb 15, 2011 at 5:01pm

Hello,

sorry, but why not just [/ 10] and [sel 0 1 2 ... 9] ?

#197898
Feb 15, 2011 at 5:02pm

Wow, thanks very much Roman, that clears up a lot of questions i was about to ask
Cheers again.

I will put the instrument up on the forums when its getting near completion for feedback and encourage people to muck around with it and make music

Thanks for all the extensive answers and help guys

Ali

#197899
Feb 15, 2011 at 5:06pm

@vanille

…and this would work for so many other similar scenarios; nice one – simplicity rules!

#197900
Feb 15, 2011 at 7:52pm

@vanille
haha, christ. I didn’t even think of doing it that simply. Just shows you have to keep on your toes when trying to solve problems.

#197901
Feb 16, 2011 at 12:31am

Thanks for the help guys, lots of solutions and variety too. Happy Days! :)

#197902

You must be logged in to reply to this topic.