Help with if statement
Hi, I'm a total noob with all of this so excuse me if this is a stupid question. I don't really know anything about logic and programming, but I feel like I'll learn as i go so all help is hugely appreciated!
So what I want is essentially really simple: I want to take one integer, compare it to another integer, and if the first integer is equal to or larger than the second one, I want to output the first integer minus 12. So I tried if $i1 >= $i2 then $i1 - 12, expecting it would work but it just did something completely different. What is the correct statement for what I'm trying to do?
I don't think that you can put any form of expression after the then or else, even if you put it in an expr statement or parentheses - Max will just output whatever formula you put there as a text string. You will have just have to put $i1 after the then and do the addition on the output in another object. You then need to consider what happens if your condition is not met - one solution being to send $i1 out through a second output that doesn't go through the subtraction box as below:

Great, thank you! I did figure out that solution on my own but I would never have figured out that "then/else" can't be followed by another expression, that's really good to know
The help and reference documentation is not all that helpful as it suggests that the output can be "anything", but that's only in the context of a message. So output is actually restricted to a bang, integer, float, string, input placeholder or the name of a receive object. You can also "set" the internal value of a comparator.
It's one of the reasons why I would like to see Max be able to handle genuine functions and procedures as stack based subroutines, like you get in other languages, like Pascal and C. Then if/then/else could call a function/procedure and output the result.
"I don't really know anything about logic and programming"
the main issue here is not that you are new when you are new, it is simply that expr and if do not neccessarily have the same feature set in different programming languages.
max particulary leaves out the one or other feature, starting with custom variables in expr (expr (mycoolBPM + 20) ) and the fact that you can only use up to 9 inputs / $i9 (but which is easy to justify in a non text based language)
for the given case you could need something like f-1 (the last received value) - but that also has to be done outside the expr in max.
the solution andrew posted is what i also would do - because your task actually was missing the question what should happen when the condition is not met - you most likely want to pass the note number as it is, and this can be done fine with if and a single additional object.
Bit of a coincidence but you actually fed me a great idea for my current project! Now my “egg timer” (as my good lady calls it) can count up or down depending on what the staring value is - if it starts at zero then it counts upwards (elapsed time) and if it’s not zero it counts down to zero like an, well, an egg timer!
fwiw can also be done like this..

Yes, that’s a very workable solution - if you can get your head around the Boolean logic. Not quite so easy to read though.
I wonder which method runs faster?
in my 32 bit max, where expr is 64 bit inside, i use the rule "if you need 4 or more objects it is faster to use expr in scheduler". in 64 bit max you might say "2 or more".
also, it is always faster than using if for the same job.
of course if is much better to read. and if you need "no output" when some condition is met, or if you need to make a loop, then using expr can create a real mess and you better start this task using gate, maximum or split.
otoh, if you wrote something yourself you can usually read it. :)
Haha! If you saw my project (even the Max tech support guys have commented on it’s size) and how long it’s taken to write, at my age it can be very easy to forget! 😂🤣😂 And my starting point was somebody else’s patch who had used some strange methods to achieve his goal.
I’d agree, using a Boolean expr should be faster as you’re defining the context of the comparison, whereas if has to be more generic. I end up changing methods quite often having used if to get the code logic right, or just using simple comparator objects, like && even though it creates more objects. Perhaps I should use expr more often?
I ended up using a completely different solution for my harmonizer, but now I got it working! It was a challenging but fun project, a lot of quite intricate midi manipulation was required to turn the lower half of the keyboard into "switches" for the harmonizer, rather than just sending midi notes. Here's a demo, the first part is the midi sequence without the effect and the second is the same sequence with the effect turned on:
https://soundcloud.com/modvo/harmony760-demo/s-BWzs38clN5W?si=6e7dee758b4d4740bad6d12ac722482d&utm_source=clipboard&utm_medium=text&utm_campaign=social_sharing
@ andrew
main interest for me is to take stress from the scheduler.
even versus some bitshift operation based objects and a gate which does not do any type check, it might be better to use 1 expr instead of 7 simpler objects because of all the connections.
once you get used to it you will easily transform stuff like bools, [split], [clip], [maximum] into expr´s and then you can combine more objects into a single one than you thought. :D
but if you want to be sure, you have to actually test for which is faster.