if/else statement

Apr 11, 2009 at 9:59am

if/else statement

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.

#43314
Apr 11, 2009 at 10:37am

&&

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?

#155337
Apr 11, 2009 at 5:24pm

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-

#155338
Apr 12, 2009 at 1:40pm

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

#155339
Apr 12, 2009 at 8:01pm

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. =)

#155340
Apr 12, 2009 at 9:37pm
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.

#155341
Apr 12, 2009 at 9:50pm

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. =)

#155342
Apr 12, 2009 at 11:43pm
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.

#155343
Apr 13, 2009 at 12:27am
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.

#155344
Apr 13, 2009 at 1:32am
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

#155345
Apr 13, 2009 at 6:05pm
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.

#155346
Apr 14, 2009 at 7:49am
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…

#155347
Apr 14, 2009 at 3:53pm

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.

#155348

You must be logged in to reply to this topic.