Forums > MaxMSP

The if statment and syntax


Ali
February 15, 2011 | 3:42 pm

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


February 15, 2011 | 3:52 pm

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



Ali
February 15, 2011 | 4:04 pm

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



Ali
February 15, 2011 | 4:10 pm

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


February 15, 2011 | 4:47 pm

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



pid
February 15, 2011 | 4:51 pm
– Pasted Max Patch, click to expand. –


Ali
February 15, 2011 | 4:56 pm

Awesome, thanks Brendam, thanks Pid

It makes sense now

Cheers for your help guys :)


February 15, 2011 | 4:56 pm

 
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



Ali
February 15, 2011 | 5:02 pm

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


February 15, 2011 | 5:06 pm

@vanille

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



TFF
February 15, 2011 | 7:52 pm

@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.



Ali
February 16, 2011 | 12:31 am

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


Viewing 12 posts - 1 through 12 (of 12 total)