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

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

1 ACCEPTED SOLUTION

Accepted Solutions
21-Topaz I
(To:TikkaMasala)

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

6 REPLIES 6
21-Topaz I
(To:TikkaMasala)

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

24-Ruby V
(To:TikkaMasala)

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"

"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.

11-Garnet
(To:Werner_E)

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

24-Ruby V
(To:TikkaMasala)

@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

24-Ruby V
(To:Werner_E)

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

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.

11-Garnet
(To:Werner_E)

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

That's true, sorry for confusion.

Announcements