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

Return statement with If statement

ygupta
11-Garnet

Return statement with If statement

Hi Friends,

 

I am trying to write a formula for evaluating some conditional statement. I have an old pdf of that Mathcad sheet where all these calculation had been done but unfortunately I do not have this file with me.

Can anyone tell me how can I combine return statement with if statement as shown in attached image.

 

 This is also done in Mathcad but do not know the version. I am now using Mathcad prime 3.1 to do the same thing.

I can use IF statement with else function here to get the desired output but first preference to write the formula in a similar way as shown in figure attached.

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Werner_E
24-Ruby V
(To:ygupta)


 This is also done in Mathcad but do not know the version.


From the picture we can say that it was done with real Mathcad (that is version 15 or below).



I can use IF statement with else function here to get the desired output


And that is exactly what you should do! I would also add for completeness a branch if the quotient is negative and deal with the division by zero error which would be thrown in case T.res is zero. Furthermore the quotient is calculated three times in the program shown in your picture. It may make more sense to calculate it only once and assign the result to a local variable (I had chosen "q").

B2.png

 



but first preference to write the formula in a similar way as shown in figure attached.


Why? The "returns" are not necessary as shown in the example above.

Here is what the program looks like in Prime when created the same way as shown in your picture:

B1.png

An even shorter version can be achieved by using the if-function instead of the programmed if-statement:

B3.png

This variant will also fail when T.res is zero and it will return 2 delta1 if the quotient is negative (the version in your picture would fail in that case).

View solution in original post

4 REPLIES 4
Fred_Kohlhepp
23-Emerald I
(To:ygupta)

Prime 3,1 should program as shown, but that's sloppy programming.

  • it will fail for negative values
  • multiple returns are confusing.
Werner_E
24-Ruby V
(To:ygupta)


 This is also done in Mathcad but do not know the version.


From the picture we can say that it was done with real Mathcad (that is version 15 or below).



I can use IF statement with else function here to get the desired output


And that is exactly what you should do! I would also add for completeness a branch if the quotient is negative and deal with the division by zero error which would be thrown in case T.res is zero. Furthermore the quotient is calculated three times in the program shown in your picture. It may make more sense to calculate it only once and assign the result to a local variable (I had chosen "q").

B2.png

 



but first preference to write the formula in a similar way as shown in figure attached.


Why? The "returns" are not necessary as shown in the example above.

Here is what the program looks like in Prime when created the same way as shown in your picture:

B1.png

An even shorter version can be achieved by using the if-function instead of the programmed if-statement:

B3.png

This variant will also fail when T.res is zero and it will return 2 delta1 if the quotient is negative (the version in your picture would fail in that case).

ygupta
11-Garnet
(To:Werner_E)

Thanks @Werner_EWerner_E for the detailed answer & Thanks again  @LucMeekes for replying with an alternative,

 

Now I fully understood the logic in this calculation. 

 

Cheers !

LucMeekes
23-Emerald III
(To:ygupta)

There is no 'else' function, else only exists as part of an if statement. But there is an if function, see below.

You cannot have it Prime look the same way as it does in Mathcad.

According to the help, in Prime the execution 'on True' is always on a line below the if {condition}.

I also think that your view may be wrong:

What is in that program is an IF statement, with a RETURN statement in the execution part, it's NOT a 'return with an if'.

Further I fully agree with Fred, that it's better to use the IF with an ELSE, because you want to account for all situations. It might help if you know that Tk1/Tres cannot become negative..

You may want to use this:

LM_20181222_If.png

 

Success!
Luc

 

Top Tags