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

Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X

Question about programming with "if" and "else if"

TikkaMasala
11-Garnet

Question about programming with "if" and "else if"

Hi

I would like to use simple "if" operators in my computation, just because I'm used to do so with Mathcad 15.

Is it so that I also need to use "also if" statements with Prime?

In some of my computations simple "if" statements appear to work, but for some reason the attached computations result into different results.

Could someone clarify why I cannot use only "if" statements?

 

Thanks

ACCEPTED SOLUTION

Accepted Solutions

Hi,

In the second example with only if statements each is treated as an individual statement in turn and execution falls through each statement in turn resulting in the last if-else being the one reported.  The if statement that gives a curve c is simply overwritten by the last independent if-else statement

 

In the first example (I would prefer to use "else if") the code chooses to execute one of the many alternatives.  The choices should be mutually exclusive covering all possibilities which is what the final else statement does.  The linked relationship between the if - "else if" - else means execution completes when one of the linked portions is true.  If one of the linked portions is false execution passes to the next portion.

 

Regards

Terry

View solution in original post

6 REPLIES 6

Hi,

In the second example with only if statements each is treated as an individual statement in turn and execution falls through each statement in turn resulting in the last if-else being the one reported.  The if statement that gives a curve c is simply overwritten by the last independent if-else statement

 

In the first example (I would prefer to use "else if") the code chooses to execute one of the many alternatives.  The choices should be mutually exclusive covering all possibilities which is what the final else statement does.  The linked relationship between the if - "else if" - else means execution completes when one of the linked portions is true.  If one of the linked portions is false execution passes to the next portion.

 

Regards

Terry

It is possible to use only "if" statements as in your second example if you explicitly use the "return" command.

So use

return "Bucking curve c"

instead of just

"Bucking curve c"

And simply type the text in your else-statement as the last program line - without any "else"!

The return statement stops the program and the other commands are not executed. So if no case applies, the last line is the return value.

Don't simply type "return" but use the return command from the menu or type "return" followed by pressing Ctrl-j (a very convenient feature implemented in Prime)

 

BTW, the behaviour of a program with just a series of if-statements is the very same in Prime as it was in real Mathcad! So I am a little bit confused that you wrote "I would like to use simple "if" operators in my computation, just because I'm used to do so with Mathcad 15."

Also remember that its always better to attach your worksheet additional to posting a pic.

With the Mathcad 15 I had the opportunity to use "otherwise" -operator. See attached figure.


@TikkaMasala wrote:

With the Mathcad 15 I had the opportunity to use "otherwise" -operator. See attached figure.


OK, you didn't mention "otherwise" in your first posting.

To achieve the same effect without using return-statements, you would have to use "also if" as you already suspected.

Here' a comparison

Werner_E_2-1641994412979.png

 

 
 

 

Another way to avoid the "also if" cascade would be to store the return values in a local variable

Werner_E_3-1641994921773.png

 

But I would rather prefer using the explicit "return" statements. Also for performance reasons (no big deal here) - no need to check further conditions if the first already applies.

"OK, you didn't mention "otherwise" in your first posting."

 

That's true, sorry for confusion.

Announcements

Top Tags