Help with a double "IF" statement
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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
- Labels:
-
Physics
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Thanks Richard
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Agreed. That's what is in Prime. It has both "else if" and "also if". That makes it very clear what the intent is.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hi
Even so, it would be fine, do not you think?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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