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

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

Question of setup a condition for IF ELSE calculation

yhuang-3
1-Newbie

Question of setup a condition for IF ELSE calculation

Hi all, I have a question of properly setup the if condition in Mathcad, would someone help me with the attachment? Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
MJG
18-Opal
18-Opal
(To:yhuang-3)

How about this?  others may have a better way to handle condition1.

View solution in original post

11 REPLIES 11
MJG
18-Opal
18-Opal
(To:yhuang-3)

How about this?  others may have a better way to handle condition1.

LucMeekes
23-Emerald III
(To:yhuang-3)

How about this:

MJG
18-Opal
18-Opal
(To:yhuang-3)

After further consideration, I like this solution better.  It allows you to have better descriptive error text.

Here is another way to do it:

StuartBruff
23-Emerald II
(To:yhuang-3)

Just a general observation, it's often better to use the forms

rather than

as there can be a danger that both conditions a and b could be true, which means you end up getting result y even though you really wanted x.

There's no need for the return statement in the "return z" line above;

I just find it more informative and consistent that way,

Stuart

RichardJ
19-Tanzanite
(To:StuartBruff)

as there can be a danger that both conditions a and b could be true, which means you end up getting result y even though you really wanted x.

IMO, this is another example where Prime is better than MC15. In MC15 the "otherwise" is attached to the preceding "if". The first "if" is completely independent. Prime changes "otherwise" to "else" (a change not liked by some because it's more like a programming language and less like math notation, but I can live with it), and adds "else if" and "also if", so we can have much more versatile, and better structured, conditional statements.

MJG
18-Opal
18-Opal
(To:RichardJ)

Stuart, thanks for the suggestion.  I don't do a lot of programming in Mathcad.

Richard, I don't think this statement is accurate:

In MC15 the "otherwise" is attached to the preceding "if". The first "if" is completely independent.

From Mathcad Help:

"If you use more than one if statement before an otherwise statement, the otherwise statement is executed only when all previous conditions are false."

MJG
18-Opal
18-Opal
(To:MJG)

After re-reading your comment, perhaps you meant that the first "if" is independent of the second "if" - that is correct: if both a and b are true, the program will return y.

When I first read your statement, I interpreted it to mean the first "if" is independent of the "otherwise" - that is incorrect: if a is true and b is false, the program will return x, not z.

RichardJ
19-Tanzanite
(To:MJG)

Sorry. That was badly written. However, you can make the "otherwise" independent of the first "if " if you add a statement between the two "if" statements, even if that statement does not evaluate to anything.

This has come up in more than one post to the forums. So I can argue that the Mathcad help is equally badly written

StuartBruff
23-Emerald II
(To:RichardJ)

Richard Jackson wrote:

ta

as there can be a danger that both conditions a and b could be true, which means you end up getting result y even though you really wanted x.

IMO, this is another example where Prime is better than MC15. In MC15 the "otherwise" is attached to the preceding "if". The first "if" is completely independent. Prime changes "otherwise" to "else" (a change not liked by some because it's more like a programming language and less like math notation, but I can live with it), and adds "else if" and "also if", so we can have much more versatile, and better structured, conditional statements.

Yes, an if..elseif..else structure is better, and I think a request for this went way back before Prime.  I think Prime's implementation, though, is too constrictive and takes up far more vertical space than is needed on many occasions, thus impeding rather than assisting readability of an entire program.  I would have preferred to have the option for a proper mathematical notation for piecewise functions, eg

{x, if x>=0;; -x, otherwise;

- which would, after all, be just a syntactic sugaring of the if..elseif...else (and, yes, this is another long-standing request).

Stuart

Better use the function if

if(x=1,a,b,)

not the operator if

if x=I

than a

else b

Thy?

The function works in free Mathcad Express!

Top Tags