Skip to main content
25-Diamond I
February 17, 2013
Solved

Simple program - wrong return value

  • February 17, 2013
  • 2 replies
  • 7620 views

This is a problem I stumbled upon writing a more complex routine (http://communities.ptc.com/message/197514#197514) and was able to cut down to the attached simple programs.

Can anybody explain whats wrong with Test2()?

Why is the else branch ("otherwise") not evaluated when the first "if" is triggered??

Inserting that dummy line as in Test1() "solves" that problem, but whats the reason for this?

ProgramOutputTest.png

Best answer by RichardJ

I only wonder why I didn't came across that descrepancy sooner, or did I?

You got lucky?

In MC15 the only ways to terminate a conditional block is with either an "otherwise", or a line with no conditional statement in it. Since the only conditional statements available are "if" and "otherwise", there had to be a choice between evaluating all consecutive if statements, or exiting the block when any one was true. I guess it was decided the lesser of two evils was to evaluate all of them (a decision I personally agree with). Since the "otherwise" is only meant as a trap when none of the conditions are met, in my opinion it is logical that it is not evaluated unless none of the conditions are met. So your test1 program has two conditional blocks, but your test2 program has only one. It's made even more confusing by the fact that you don't have a return statement on the first line. It means that in test1 the line actually has no effect at all.

You are not the first person to be confused by this behavior (surprise!). Even if you know how it works, it can also make it hard to figure out what a program will actually do. It's also rather limiting, because sometimes what you want is an "else if" or case statement, but that is not easy to create in MC15. Hence the changes in Prime. There are things I don't like about Prime, but IMHO the new conditional structure is a huge improvement.

2 replies

Werner_E25-Diamond IAuthor
25-Diamond I
February 17, 2013

Converting the worksheet in Prime2 format shed a little light on the phenomenon but I'm still lacking full understanding. Changing that "also if" to a simple "if" does what I want, but how can I achieve this in Mathcad15 without inserting that dummy line?

Is still don't fully understand when and how the else branch is evaluated in that example.

ProgramOutputTest2.png

3-Newcomer
March 23, 2013

I opine that the real problem is poorly constructed programs, though I may have missed the point of this bit of logic. The program can be rewritten to reflect programming standards

Nested+If+M15+and+MP3A+0100.gif

So, while the "also if" may be necessary to accomodate a certain programming style, /I/ don't want what MP3 recently did to my program.

In the following, M15 does exactly what I expect, and even though I am not a fan of the current M1x or MP programming metaphor, both are OK for what they do: M1x is not complete enough and requires those extra "" lines to clean up the logic, and MP's || lines are superfluous when single lines suffice (and I prefer them).

Nested+If+Case+2+M15+and+MP3A+0100.gif

Werner_E25-Diamond IAuthor
25-Diamond I
February 17, 2013

After some more investigation (RTFM, aka Mathcad help) it seems that this behaviour is implemented on purpose and I guess the only way to deal with it is to add an extra line - either the dummy I had inserted or a dummy "otherwise" statement after the first if.

From the MC15 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. However, all previous if statements continue to be evaluated regardless of the results of the previous if. In the example above fy(5) = 9 and fy(2) = 1. There is no "else if" or "case" statement in Mathcad that allows you to switch on multiple cases, other than to use nested if ... otherwise pairs.

As Mona wrote in a thread last year (http://communities.ptc.com/message/183475#183475) the "also if" was introduced in Prime2 for backwards compatibility. In Prime a sequence of if-statements before an otherwise-statement evaluates the way I expected it to do in MC15.

Should I change my opinion regarding Prime? No. Should I remind myself that Mathcad15's help is feasible, quite complete and worth a look? Yes!

I only wonder why I didn't came across that descrepancy sooner, or did I?



RichardJ19-TanzaniteAnswer
19-Tanzanite
February 18, 2013

I only wonder why I didn't came across that descrepancy sooner, or did I?

You got lucky?

In MC15 the only ways to terminate a conditional block is with either an "otherwise", or a line with no conditional statement in it. Since the only conditional statements available are "if" and "otherwise", there had to be a choice between evaluating all consecutive if statements, or exiting the block when any one was true. I guess it was decided the lesser of two evils was to evaluate all of them (a decision I personally agree with). Since the "otherwise" is only meant as a trap when none of the conditions are met, in my opinion it is logical that it is not evaluated unless none of the conditions are met. So your test1 program has two conditional blocks, but your test2 program has only one. It's made even more confusing by the fact that you don't have a return statement on the first line. It means that in test1 the line actually has no effect at all.

You are not the first person to be confused by this behavior (surprise!). Even if you know how it works, it can also make it hard to figure out what a program will actually do. It's also rather limiting, because sometimes what you want is an "else if" or case statement, but that is not easy to create in MC15. Hence the changes in Prime. There are things I don't like about Prime, but IMHO the new conditional structure is a huge improvement.

Werner_E25-Diamond IAuthor
25-Diamond I
February 18, 2013

I only wonder why I didn't came across that descrepancy sooner, or did I?

You got lucky?

Seems so.

In MC15 the only ways to terminate a conditional block is with either an "otherwise", or a line with no conditional statement in it. Since the only conditional statements available are "if" and "otherwise", there had to be a choice between evaluating all consecutive if statements, or exiting the block when any one was true. I guess it was decided the lesser of two evils was to evaluate all of them (a decision I personally agree with).

So there is no other solution as adding that extra line - not necessary to search for another workaround.

I was assuming that mathcad would behave like any other programming language which only offer if and else: if an if-statement is not followed by an else it is finished. If you want a cascading evaluation you would have to put the second if in the else (otherwise) branch. MC 15 behaves differently - good to know (hope i will remember).

It's made even more confusing by the fact that you don't have a return statement on the first line. It means that in test1 the line actually has no effect at all.

You are talking of the "XXX" line. In the "real" program I was workin on there is some kind of evaluation there, a loop if i remember correct. I tried to track down the problem and simplified it as much as possible which helped me to find out what was going on. So i found that the first if (which had nothing to do with the last one) was responsible for the troubles.

There are things I don't like about Prime, but IMHO the new conditional structure is a huge improvement.

I tend to agree. The new modifiers of the lookup functions are an improvement, too. Its not all bad in Prime, but...

Thanks for your explanations!