cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can Bookmark boards, posts or articles that you'd like to access again easily! X

Help with a double "IF" statement

aclark-3
1-Newbie

Help with a double "IF" statement

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

12 REPLIES 12
TomU
23-Emerald IV
(To:aclark-3)

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


Martin Hanák
RichardJ
19-Tanzanite
(To:aclark-3)

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.

fig.gif

RichardJ
19-Tanzanite
(To:AlvaroDíaz)

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

RichardJ
19-Tanzanite
(To:AlvaroDíaz)

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

RichardJ
19-Tanzanite
(To:AlvaroDíaz)

Agreed. That's what is in Prime. It has both "else if" and "also if". That makes it very clear what the intent is.

-MFra-
21-Topaz II
(To:aclark-3)

Hi

Even so, it would be fine, do not you think?

returnif.jpg

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

Top Tags