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

Community Tip - You can change your system assigned username to something more personal in your community settings. X

Comment changing program result, why ?

drgriffin
1-Visitor

Comment changing program result, why ?

Why does the comment in the second version of the program below change the result ?
6 REPLIES 6
StuartBruff
23-Emerald III
(To:drgriffin)

On 12/5/2009 12:07:32 PM, dmg wrote:
== Why does the comment in the second version of
the program below change the result

If you have a sequence of contiguous sequential if
statements, with no other expressions in between,
then otherwise only comes into force if none of if
statements is true. The 'comment' means that the
first if statement is isolated from the otherwise,
so the otherwise doesn't get invoked.

Stuart
LouP
12-Amethyst
(To:StuartBruff)

I would never have thought of this behaviour (spelled this way in your honour 🙂 from a comment, which Iwould think should act as if never present re: execution. I would call this a design flaw.

Lou

There is no such thing as a comment. What you have is a statement (expression) consisting of just a character string. This is an expression like any other, it is evaluated, and contributes (possibly to the result. The effect of this expression is that the three if statements are no longer contiguous. Thus the otherwise clause applies only to the two immediately preceding if's (as the statement preceding them is not an if) and the otherwise expression is evaluated (and becomes the overall result) when these two if's are false.

The first if has become irrelevant -- whether it is evaluated or not, there is always a subsequent statement evaluated (one of the other two if's or the otherwise, and, in fact, the string expression) and so that first if can never generate the overall result.
__________________
� � � � Tom Gutman


O.K. - why does this version work ?
StuartBruff
23-Emerald III
(To:drgriffin)

On 12/5/2009 4:49:57 PM, dmg wrote:
== O.K. - why does this version work ?

Because the if statements are still contiguous.
The expressions (which are programs) on the right
hand side are part of their corresponding if
statements, so don't intrude between successive
ifs.

Mathcad evaluates each of the conditional
expressions in standard manner, the last being
actual comparison you want. If you'd put a
comments after the true conditional expression, the
if statement would fail.

Stuart

Different levels. Here the "comments" are part of the condition expressiions in the if statements. They are at a different level (nested one deeper) than the if statements themselves, which are, as Stuart notes, contiguous. Remember. in Mathcad a program structure just an expression and can be used wherever an expression can be used. While most commonly nested program structures appear as the bodies of control statements (if's, for's, while's), they can actually appear anywhere.
__________________
� � � � Tom Gutman
Announcements

Top Tags