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
Yours Sincerely
Joyce.
&&
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?
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-
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).
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. =)
starlet_0808 wrote on Sun, 12 April 2009 21:01if $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.
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. =)
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.
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.
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
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.
starlet_0808 wrote on Sat, 11 April 2009 19:24Ya 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...
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...
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.
hello anybody here to help me to build that very simple lookup table?
if ($i1==1) then 7
else if ($i1==2) then 10
else if ($i1==3) then 11
else 0
it work only for the first if statement :-/
many thanks
[select 1 2 3]
[t 7] [t 10] [t 11] [t 0]
or
[expr (($i1==1)*7) + (($i1==2))*10 + (($i1==3))*11]
or
Hi guys,
I'm also a bit new with Max and the syntax for 'if statements'.
I'm trying to resolve the following issue:
'If $i1 == $i2 FOR $i3 (amount of time) then bang'. I'm starting to wonder if this is even possible? Is there a very simple solution tot his that I just can't think of?
All the best,
Alex
no "for", no "while", no nothing.
only what is in the if.maxhelp - plus all math stuff from expr.maxhelp (for the input side)
"for three times" could be solved with objects like uzi or counter, and if you want recursion, you have to insert a trigger or an empty subpatch so that you can connect an object to its own inputs.
in your case i would probably do
[==]
[select 1]
[accum] (collect 3 times "true", then reset)
[select 3]
Hi Roman,
Thanks for your reply!
I should have been more clear however,
What I want to resolve is:
If $i1 == $i2 'for an amount of time (in seconds) $i3' then bang.
Does this seem possible to you?
I've never come across the [accum] object and will look at it now!
Best,
Alex
Try this.
even more "only outside the if object".
datetime or metro will provide you with timing in seconds.
but the first thing to do is to look into [delay] and [pipe]
The problem with this kind of time dependent detection of equal values
is that if by any chance both values continue to come in equaly,
delay will bang only after the stream stops.
"==" or"i f $i1 = $i2" actually gives You allways first No and then Yes
wenn 2 streams of ints are equal. Even if You connect 1 int to
both inlets of ==, as max does right to left order, they will allways be
first detected as different, and after that the same...
That means that if You want to have guaranteed bang xy seconds after
equality has been detected even if numbers change having same value, it will
become tricky.
Have a look at the patch which demonstrates that :
But maybe that is what You want ...
Here is another variant that will bang after set time, even if ints continue
to get in equaly.
Or simplier said, as soon as equal ints get detected and remain equal,
no matter if they change or not, bang will be triggered, and in between
detected equal ints ignored.
@source audio your solution is pretty similar to mine, but you don't need the '> 0' (the output of == is going to 0 or 1, '> 0' will return 0 for 0 and 1 for 1) and you probably want a 'change' between '==' and 'sel' to avoid resetting the delay object every time a new match in a sequence of matches is detected. That way, the time to match actually causes a bang after N ms of continuous matching has elapsed.
Yes sure, > 0 is just forgotten leftover from patch I extracted the pasted stuff above.
But the change object does not have any effect if ints compared
are the same as illustrated.
It happend to me in a project that I overlooked the possibility that something like that could possibly happen, but it did, a client had some automated
parameters which he linked...
you can use "1" and [pipe] in order to avoid the usual problem with "bang" and [delay]
sure, all this extra info is only to point to the fact that several inlets of pak or bondo -> pack
routed to whatever comparison, would allways give wrong result followed by correct result if all inputs change simultaneously and have same value.
If one wants to measure the time in which incoming ints remain equal,
change object is not working as it gets reset on any input.
Pipe would just delay all incoming numbers, so of what help would it be in the context ?
with pipe you can delay 0 and 1 and what not, where with delay you would need one instance which delays the trigger command and another instance for a reset command or whatever you need.
Maybe You wouldn't mind showing me how would that be best implemented for the actual question Alex asked in the thread : "What I want to resolve is: If $i1 == $i2 'for an amount of time (in seconds) $i3' then bang." with the help of pipe in the patch below:
Sometimes I catch myself using some objects
I got used from the past and overlook the ease
of doing same thing with some other, more apropriate objects ...
apropos ease. if he is addicted to typing and reading "for" he could make an abstraction right now which lets him create a [myif == for 3] object.
max works how you tell it to!
Apropos apropos ... the world is as it is most of the time
oder es ist schwer zu trinken und gleichzeitig klaren kopf zu behalten.
lebensweisheiten gibt es zuhauf...
@source audio
Thank you very much, that patch you posted was pretty much what I was looking for!
I'm relatively inexperienced with processing lists of info/data so this is a good way of understanding how to use list information!
All the best,
Alex
This solved my multi-conditional judgment problem in a simple and easy to understand way, thank you very much! @nouserid