Skip to main content
12-Amethyst
January 12, 2022
Solved

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

  • January 12, 2022
  • 2 replies
  • 3756 views

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

Best answer by terryhendicott

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

2 replies

21-Topaz II
January 12, 2022

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

25-Diamond I
January 12, 2022

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.

12-Amethyst
January 12, 2022

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

25-Diamond I
January 12, 2022

@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