Forums > MaxMSP

if/else statement

April 11, 2009 | 9:59 am

Hi all.
Can anyone help me out with the if/else statement? How would it look like in maxmsp?
Basically i want to say if $1>180 and if $1<0 then fix the rotation of the jit.gl.gridshape at a certain angle.

Yours Sincerely
Joyce.


April 11, 2009 | 10:37 am

&&

For better or worse, Max’ syntax is based on the C programming language. Everything you need to know is described in the tutorials, but it may be helpful to at pick up a text on C (deadtree or online) and run through the syntax.

But in your case there are much more appropriate objects. Have you looked at split?

For that matter, have you looked at any of the tutorials or the relevant help files?


April 11, 2009 | 5:24 pm

Ya I searched the tutorials but could not find anything on if/else statements. I have no programming experience so it’s pretty confusing. Thanks anyway. =)

-Joyce-


April 12, 2009 | 1:40 pm

On Max 5 it’s "Basic" tutorials 19 & 22. They cover a lot of ground and after a cursory scan I wonder if a beginner might find the Max 4 Tutorial 15 a bit easier to follow. Mind you, you get through the whole Max4 tutorial without using the [if] object.

There is/was a notion that the [if] object is a more procedural way of programming, whereas ‘real’ Max programmers think dataflow, for which other objects are more idiomatic: sel, route, gate, the basic comparison objects, etc.

Yeah, there’s a lot to learn. One hint: searching the documentation on ‘comparison’ will get you tighter results than a search on ‘if’ (which comes up in about 2,398 docs).


April 12, 2009 | 8:01 pm

Hi Peter
Thank you for your reply. in the end I managed to figure out how to use the if function.

if $i1 > 140 then $1==90 else $i1==$i1

Realised that there was an if help file. Max 5 is quite beginner friendly I think. Manage(with the help of some advice from here and my peers) to do up my very own patch. =)


April 12, 2009 | 9:37 pm
starlet_0808 wrote on Sun, 12 April 2009 21:01
if $i1 > 140 then $1==90 else $i1==$i1

I’m curious to know what you think this is actually doing. Replacing the (illegal) $1 with $i1 leaves you with a complicated equivalent to

if $i1 > 140 then 0 else 1

which I assume isn’t your intention. As Peter says, you can probably achieve what you want with "split". Even if what you want to achieve has an iffy feel about it, it’s probably quite rare that the "if" object is the best way to go about it.


April 12, 2009 | 9:50 pm

Oops I think I made a mistake. Basically I wanted this gridshape rotation to be at 90 degrees if say the number sent was more than 140. I’ll take a look at split if I have the time. I was rushing for time so I thought if would be the easier way but yeah. Will definitely try to change it when my exams are over. Thanks anyway. =)


April 12, 2009 | 11:43 pm
Quote:
if $i1 > 140 then $1==90 else $i1==$i1

interesting idea to do "then $1==90" – but be careful that stops working when you need more than one outputs (it does not work with "then $2==").

btw, it might also not work for $1, whatever that is.

have you tried connecting a [print] object to it?

please do so.


April 13, 2009 | 12:27 am
Peter Castine wrote on Sun, 12 April 2009 08:40

There is/was a notion that the [if] object is a more procedural way of programming, whereas ‘real’ Max programmers think dataflow, for which other objects are more idiomatic: sel, route, gate, the basic comparison objects, etc.

True for me too, I rarely use the actual [if] object. The other ones mentioned are essentially special-purpose [if] objects anyways, and a lot easier to use I think, plus you can do multiple tests with one object, like [sel 0 1 2 3 4 5]. Though sometimes the [if] is handy for just the right function.


April 13, 2009 | 1:32 am
Quote:

Though sometimes the [if] is handy for just the right function.

for example when you need "greater than" and "smaller than" it saves you at least one maxmsp level connection compared to comparison operators or [split].
same for excluding and filtering more than one thing.

or both at the same time:

[if $f1 > 0. && $f1 < 100. && $f1 != 25. && $f1 != 75. then bang]

could save you not only 17 metro-driven max messages, but might also be even easier to program.

-110


April 13, 2009 | 6:05 pm
Roman Thilenius wrote on Mon, 13 April 2009 03:32
[if $f1 > 0. && $f1 < 100. && $f1 != 25. && $f1 != 75. then bang]

could save you not only 17 metro-driven max messages, but might also be even easier to program.

Since there are a lot of beginners reading, it’s worth mentioning that comparing floats for equals/not-equals will often not work as you expect. Depending on how the number is calculated, $f1 may never equal 25, but might be 24.99998765. Depending on your logic, you might want the latter case to suppress the bang, too. Note that in a flonum, in most situations both numbers will look identical.

Instead of

$f1 != 25.

you probably want something like

fabs(25. – $f1) < 0.0001

which you’ll have to break out of the if expression anyway. How to implement the latter in Max is left as a research exercise for the reader.


April 14, 2009 | 7:49 am
starlet_0808 wrote on Sat, 11 April 2009 19:24
Ya I searched the tutorials but could not find anything on if/else statements. I have no programming experience so it’s pretty confusing.

But in Max you can live without if/else. Those who have programming experience use if/else/expr to be closer to what they know. Those who don’t, don’t need it… Wink
In your case the split object will do it. Look also into select and route for single values…

Creativity is about finding solutions, not about knowing…


April 14, 2009 | 3:53 pm

Wow so many replies so quickly. In the end I kind of did without the if/else but also means my "eye" is constantly looking away til someone walks past it. Project submitted but I will work on it when I have the time to perfect it! THANKS FOR ALL E POSTS YALL! You’ve all been extremely helpful. This forum rocks.


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