The if statment and syntax

Ali's icon

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
if n >10 but =
if n >20 but =
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

brendan mccloskey's icon

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's icon

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

1817.numberboxandifstatement.maxpat
Max Patch
Ali's icon

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

brendan mccloskey's icon

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's icon
Max Patch
Copy patch and select New From Clipboard in Max.
Ali's icon

Awesome, thanks Brendam, thanks Pid

It makes sense now

Cheers for your help guys :)

Roman Thilenius's icon

1.

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

2.

[if $i1

[if $i1
you can use AND or OR:

[if $i13 then $i1]

[if $i13 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   ($i13)   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
the output may be a constant or symbol, though:

[if $i1

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

Ali's icon

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

brendan mccloskey's icon

@vanille

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

TFF's icon

@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's icon

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