Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X
Hello.
I'm having trouble combining 2 "IF" statements. I'm sure this is a piece of cake for you experts.
Here is the general form of my program (the actual equations are more complicated).
X=A If M=1
X=B If M=2 and If N=1
X=C If M=2 and If N=0 (or simply X=C otherwise)
Thanks, Al Clark
You're posting this in the general community help section - meaning help with the community site itself, not some specific PTC software. If you can tell us what software you're using, we can provide a better response.
Hi,
in case that you need to define model relations, please see following code.
/* M, N are integer values
X=C
if M==1
X=A
endif
if M==2
if N==1
X=B
endif
endif
MH
Since you have posted in the Mathcad forums before I assume this is a Mathcad question, in which case the best solution is a small program:
Note that you cannot just type "return", "if" and "otherwise". They must be entered from the programming toolbar.
Toby Metcalf please move this to the Mathcad forums.
Thanks Richard
Hi Richard
Some purist can say that returns isn't a proper instruction for structured languages. And I'm a strong advocate of GoTo, GoSub and those instructions, but mathcad have a particular implementation for a sequence of if statements.
Best regards.
Alvaro.
If Mathcad programs were better structured (and in Prime they actually are; one of the few benefits of Prime) I would agree. However, as you note even a dead string used as a comment, such as "some stuff", in the middle of the if statement breaks the logic. So I make a habit of using a return statement if I explicitly want the program to exit and return a specific value at that point. I agree it's not ideal, but I didn't design Mathcad programming
Richard Jackson wrote:
... but I didn't design Mathcad programming
Unfortunate for us. I'm pretty sure that you can improve a lot of things and implement very good news routines.
Best regards.
Alvaro
Well, Prime does not suffer from this problem with unexpected results from if statements that are broken by comments. I'm proud to say that's at least partly because I made an issue out of it
Second and subsequent if's are actually elseif but assumed by the context not by a keyword which could be the better option : a ben program command with a well defined logic
Agreed. That's what is in Prime. It has both "else if" and "also if". That makes it very clear what the intent is.
Hi
Even so, it would be fine, do not you think?
Thanks everyone for your help. I didn't realize that I would initiate such a discussion.
Actually all I needed was to be steered to the Boolean "and", and then I was all set.
Regards, Al