multiple if objects

fibo's icon

Hello,

I created a patch to calculate student grades (any excuse to make a max patch...). As an added feature I would like it to actually give me a letter grade so I did this.

Can someone tell me why it's not working?

Thanks

Max Patch
Copy patch and select New From Clipboard in Max.

metamax's icon

Well, for starters, you didn't copy your patch in compressed format, yet somehow managed to encode it in your message as if it was compressed. Did you manually prepend and append the compression tag on uncompressed code? Either way, even after pasting the uncompressed part of your code inside of an existing Max window, it crashed Max. So.. as the very least, go back to your patch, select all, edit -> copy compressed, and paste that in a forum message window and resend.

This is the only if argument I could see in your code:

"if $i1 57) || ($i1 59) || ($i1 65) || ($i1 67) || ($i1 69) || ($i1 75) || ($i1 77) || ($i1 79) || ($i1 85) || ($i1 87) || ($i1 89 then A"

If that's what you wrote (and it's not an artifact of the above issue) then I think reading the reference documentation and looking at the help file more closely will help you.

What are you trying to express? How would you say it in natural language?

At the very least... parentheses need to be closed... and changeable args also need to have a functional relationship with the integers to which they are paired. ex. "($i1 > 57)" is valid condition. "($i1 > 57" is not valid becuase the parentheses are not closed. "($i1 57)" is also not valid because there is no function. And "A" is not a valid variable. Variables take the form of $i1 or $f1, etc. There are no a's or x's or n's in the syntax.

In terms of a complete expression, [if] statements go something like: "If some condition is met, then do something, otherwise do something else". A valid example would be "if ($i1 > 60) then 1 else 0" This will output a 1 if the integer sent to the left inlet is greater than 60 and a 0 if it is not.

If you want a letter as the result for meeting a numerical condition, you need to handle that after the output. You can use the select object to select a numerical input and bang a message box containing a letter to output your letter. ex. [if ($i1 >= 60) || ($i1 < 70) then 1 else 0] with [sel 0 1] connected to the outlet of the if object and a message box containing the letter "D" connected to the right outlet of the sel object. That way, if the input is 60 to 69 it will output a 1 and the sel object will bang out a D.

That said, unless you really need to two separate results sent out different outlets, I think the expr object is much more flexible and can pretty much do whatever the if object does... but you will need to get familiar with it. Good luck with your project.

Wetterberg's icon

patch doesn't paste, please use Copy Compressed.

Also; it's time to upgrade, you're on 6.1.0! :)

fibo's icon
Max Patch
Copy patch and select New From Clipboard in Max.

I apologize for that. Here it is.
Thanks for the advice. I'll try with the expr object:

fibo's icon

btw I wasn't using A as a variable but as a symbol to output. For example if score is >90 then output A. That part worked fine. What doesn't work are the parts when I say if score is > 79 and < 86 then output the symbol B.

I mean of course this is a rather lame way to use a maxpatch :-). It just seems that this may come in handy at some point. It's obviously just a syntax issue.

I'll just give everyone an A hehe.

fibo

Andro's icon

why not just use multiple versions of the split object ?

Rick's icon
Max Patch
Copy patch and select New From Clipboard in Max.

[if ($i1 > 87) || ($i1 < 90) then A-] Here's your problem. Both statements need to be true, not just one. So changing your || to && should solve your problem.

Roman Thilenius's icon

damn, why does the forum supplies uncompressed code to the clipboard from the first posts copy button?

edit: oh, i just read back, that can be done by a user ... i did not know know that. :)

Roman Thilenius's icon

the only way how to more integrate those 8 checks for conditions into less then 8 objects would be to use 1 2 3 4 instead of a b c d as result.

metamax's icon

Both statements need to be true, not just one. So changing your || to && should solve your problem.

This.

the only way how to more integrate those 8 checks for conditions into less then 8 objects would be to use 1 2 3 4 instead of a b c d as result.

This too.

Also consider… if you want to handle those conditions in a single expression and you want to cover all possible input values (not just the values on either side of your split points), use the form >= && <

> && < either ignores a value between successive conditionals or you are forced to overlap the upper and lower limits of successive conditionals which can result in more than one condition being met. This is obviously more relevant when dealing with floats and/or compound expressions, but it doesn't hurt to develop good habits...

Here is everything in one expression. Note how "then" is replaced with * and conditions are linked with +. If a logical condition is met, [expr] treats it as a 1 otherwise 0. Multiplying something by 1 gives you that something… multiplying by 0 gives you nothing. So you can string as many of those together as you want. Then all you have to do is translate the numerical output to letters. umenu is perfect for that.

Max Patch
Copy patch and select New From Clipboard in Max.

Roman Thilenius's icon

otoh, there is always that ungodly limit of 9 variables to expr

Roman Thilenius's icon

@metamax:

(a>b)*0 could cause some trouble. :)

if one needs to have 0 - 3 as possible output, one can work with an offset around the expression:

expr ( (($i1>5)&&($i17)&&($i19)&&($i1

-110

metamax's icon

Roman, what's the problem with ($i1

Also, while I think I understand what you're getting at, your expression doesn't output 0 to 3. It outputs -1 to 2. And it seems to suffer from the split point problem I mentioned above… 7 and 9 return -1, as do other numbers out of range. Or maybe I'm misunderstanding you.

Though I suppose this would do it… :)

expr 1 + ( ( (($i1>5)&&($i17)&&($i19)&&($i1

fibo's icon

Thank you all for your suggestions. Rick, that was exactly the problem, both statements needed to be true. Thank you for taking the time to fix the patch.

Best,

fibo

Roman Thilenius's icon

the problem with ($i1

($i1

about my post:

my first error was that there a typo in my post, of course it should read that the expected output is 0-2, not 0-3.

the second problem is indeed that i forgot to take care of the possibility that all conditions are false. (yet it would be a strange application when someone wants to get a zero as one of the encoded answers AS WELL as for "all false".)

this isnt the first time in the last weeks that i am posting half working expressions to the forum, and i think i really should stop posting stuff when i dont have max in front of me.

-110

Roman Thilenius's icon

about the red code: i guess it should be >=9 and not >9 ... you found my error but you did not fix it :P