The `if' command

This is a discussion on The `if' command within the mumps forums in Programming Languages category; set i=1,j=2,k=3 if i<j&k>j write "yes",! i _is_ less than j AND k _is_ greater than j so why is _yes_ NOT printed? The logical operator & tells me that both relational expressions must be true in order for the _yes_ to be printed. That's exactly what it is, so what am I missing? TIA...... -- Duke Normandin...

Go Back   Application Development Forum > Programming Languages > mumps

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 04-29-2008, 11:42 PM
Duke Normandin
Guest
 
Default The `if' command

set i=1,j=2,k=3
if i<j&k>j write "yes",!

i _is_ less than j AND k _is_ greater than j

so why is _yes_ NOT printed?

The logical operator & tells me that both relational expressions must
be true in order for the _yes_ to be printed. That's exactly what it is,
so what am I missing? TIA......
--
Duke Normandin
Reply With Quote
  #2  
Old 04-30-2008, 12:04 AM
Maury Pepper
Guest
 
Default Re: The `if' command

There is no operator precedence in Mumps so the expression "if i<j&k>j" is
evaluated as:
if (((i<j)&k)>j)
Since 1 is less than 2, the first term is true, and "true" has the value 1.
Since 1 & 3 is true, the second term is true, ie = 1.
Since 1 is not greater than 2, the third term is false.


"Duke Normandin" <dukeofperl@ml1.net> wrote in message
newsURRj.3093$PM5.3083@edtnps92...
> set i=1,j=2,k=3
>write "yes",!
>
> i _is_ less than j AND k _is_ greater than j
>
> so why is _yes_ NOT printed?
>
> The logical operator & tells me that both relational expressions must
> be true in order for the _yes_ to be printed. That's exactly what it is,
> so what am I missing? TIA......
> --
> Duke Normandin


Reply With Quote
  #3  
Old 04-30-2008, 07:57 AM
Duke Normandin
Guest
 
Default Re: The `if' command

On 2008-04-30, Maury Pepper <mpepper_scram_spam@ieee.org> wrote:
> There is no operator precedence in Mumps so the expression "if i<j&k>j" is
> evaluated as:
> if (((i<j)&k)>j)
> Since 1 is less than 2, the first term is true, and "true" has the value 1.
> Since 1 & 3 is true, the second term is true, ie = 1.
> Since 1 is not greater than 2, the third term is false.


I see now! The moral of the story then is:

Put the parentheses where _you_ want/need them and _never_ rely on Mumps
to do it!
Thanks!!

> "Duke Normandin" <dukeofperl@ml1.net> wrote in message
> newsURRj.3093$PM5.3083@edtnps92...
>> set i=1,j=2,k=3
>>write "yes",!
>>
>> i _is_ less than j AND k _is_ greater than j
>>
>> so why is _yes_ NOT printed?
>>
>> The logical operator & tells me that both relational expressions must
>> be true in order for the _yes_ to be printed. That's exactly what it is,
>> so what am I missing? TIA......
>> --
>> Duke Normandin


--
Duke Normandin
Reply With Quote
  #4  
Old 04-30-2008, 11:12 AM
Philip Gage
Guest
 
Default Re: The `if' command

In message <pURRj.3093$PM5.3083@edtnps92>, Duke Normandin
<dukeofperl@ml1.net> writes
>set i=1,j=2,k=3
>if i<j&k>j write "yes",!
>
>i _is_ less than j AND k _is_ greater than j
>
>so why is _yes_ NOT printed?
>
>The logical operator & tells me that both relational expressions must
>be true in order for the _yes_ to be printed. That's exactly what it is,
>so what am I missing? TIA......

Left to right operator precedence.
i<j is true so evaluated to 1
1&k is true so evaluates to 1
1>j is false so yes is not printed.
to obtain the correct result the conditions should be in parentheses

if (i<j)&(j>j) write yes.

However an alternate would be

if i<j,k>j write yes

This is possibly preferred in that the command is broken into two
commands, and the second one only executed if the first one is true.

The reason for the preference is performance. The second condition is
only evaluated if the first is true, so by putting the least likely
condition first one can optimise the code.

Optimisation should only be done when the code retains clarity, and when
it is executed sufficiently often to justify the work.


--
Philip Gage
Reply With Quote
  #5  
Old 04-30-2008, 11:36 AM
Maury Pepper
Guest
 
Default Re: The `if' command

You are correct. Normally, I would write: if i<j&(k>j) write ... but the &
provides an opportunity to split the if into two parts as:
if i<j,k>j write ... Only if the first argument "i<j" is true does the
second argument "k>j" get evaluated. It has the effect of an AND operator
but there are a couple of differences you should be aware of: 1) it's
faster, and that's generally the appeal of splitting up the expression; and
2) because later expressions are not evaluated once an expression is
evaluated as false, you may not get the side effects that you were counting
on. For example:
IF X>Y&$D(^Z(W)) WRITE ^(W)
ELSE WRITE ^(V)
This counts on executing the $D(^Z(W)) in order to set up the naked
reference. If the & is replace by a comma and X is not greater than Y, then
the $D won't be executed and the ELSE will likely fail. (Please, no
comments from the PG regarding the use of naked references!)



"Duke Normandin" <dukeofperl@ml1.net> wrote in message
news:W8ZRj.3383$XI1.311@edtnps91...
> On 2008-04-30, Maury Pepper <mpepper_scram_spam@ieee.org> wrote:
>> There is no operator precedence in Mumps so the expression "if i<j&k>j"
>> is
>> evaluated as:
>> if (((i<j)&k)>j)
>> Since 1 is less than 2, the first term is true, and "true" has the value
>> 1.
>> Since 1 & 3 is true, the second term is true, ie = 1.
>> Since 1 is not greater than 2, the third term is false.

>
> I see now! The moral of the story then is:
>
> Put the parentheses where _you_ want/need them and _never_ rely on Mumps
> to do it!
> Thanks!!
>
>> "Duke Normandin" <dukeofperl@ml1.net> wrote in message
>> newsURRj.3093$PM5.3083@edtnps92...
>>> set i=1,j=2,k=3
>>>write "yes",!
>>>
>>> i _is_ less than j AND k _is_ greater than j
>>>
>>> so why is _yes_ NOT printed?
>>>
>>> The logical operator & tells me that both relational expressions must
>>> be true in order for the _yes_ to be printed. That's exactly what it is,
>>> so what am I missing? TIA......
>>> --
>>> Duke Normandin

>
> --
> Duke Normandin


Reply With Quote
  #6  
Old 04-30-2008, 04:27 PM
Rob Tweed
Guest
 
Default Re: The `if' command

If PG is forbidden, let me comment. Naked references are at best a
daft invention and at worst just plain evil, and are to be avoided at
all costs. Stick to explicit global references or you'll end up with
an incomprehensible, unmaintainable mess. I'm sure someone somewhere
will wade in with a wall of flame to extol their arcane virtues but
I've never encountered a single situation in 28 years where they made
any sense whatsoever.

On Wed, 30 Apr 2008 10:36:19 -0500, "Maury Pepper"
<mpepper_scram_spam@ieee.org> wrote:

>You are correct. Normally, I would write: if i<j&(k>j) write ... but the &
>provides an opportunity to split the if into two parts as:
>if i<j,k>j write ... Only if the first argument "i<j" is true does the
>second argument "k>j" get evaluated. It has the effect of an AND operator
>but there are a couple of differences you should be aware of: 1) it's
>faster, and that's generally the appeal of splitting up the expression; and
>2) because later expressions are not evaluated once an expression is
>evaluated as false, you may not get the side effects that you were counting
>on. For example:
> IF X>Y&$D(^Z(W)) WRITE ^(W)
> ELSE WRITE ^(V)
>This counts on executing the $D(^Z(W)) in order to set up the naked
>reference. If the & is replace by a comma and X is not greater than Y, then
>the $D won't be executed and the ELSE will likely fail. (Please, no
>comments from the PG regarding the use of naked references!)
>
>
>
>"Duke Normandin" <dukeofperl@ml1.net> wrote in message
>news:W8ZRj.3383$XI1.311@edtnps91...
>> On 2008-04-30, Maury Pepper <mpepper_scram_spam@ieee.org> wrote:
>>> There is no operator precedence in Mumps so the expression "if i<j&k>j"
>>> is
>>> evaluated as:
>>> if (((i<j)&k)>j)
>>> Since 1 is less than 2, the first term is true, and "true" has the value
>>> 1.
>>> Since 1 & 3 is true, the second term is true, ie = 1.
>>> Since 1 is not greater than 2, the third term is false.

>>
>> I see now! The moral of the story then is:
>>
>> Put the parentheses where _you_ want/need them and _never_ rely on Mumps
>> to do it!
>> Thanks!!
>>
>>> "Duke Normandin" <dukeofperl@ml1.net> wrote in message
>>> newsURRj.3093$PM5.3083@edtnps92...
>>>> set i=1,j=2,k=3
>>>>write "yes",!
>>>>
>>>> i _is_ less than j AND k _is_ greater than j
>>>>
>>>> so why is _yes_ NOT printed?
>>>>
>>>> The logical operator & tells me that both relational expressions must
>>>> be true in order for the _yes_ to be printed. That's exactly what it is,
>>>> so what am I missing? TIA......
>>>> --
>>>> Duke Normandin

>>
>> --
>> Duke Normandin


---

Rob Tweed
Company: M/Gateway Developments Ltd
Registered in England: No 3220901
Registered Office: 58 Francis Road,Ashford, Kent TN23 7UR

Web-site: http://www.mgateway.com

SlipstreamUSA: April 2, Renaissance Hotel, Orlando
http://www.OutOfTheSlipstream.com
Reply With Quote
  #7  
Old 04-30-2008, 11:00 PM
Duke Normandin
Guest
 
Default Re: The `if' command

On 2008-04-30, Maury Pepper <mpepper_scram_spam@ieee.org> wrote:
> You are correct. Normally, I would write: if i<j&(k>j) write ... but the &
> provides an opportunity to split the if into two parts as:
> if i<j,k>j write ... Only if the first argument "i<j" is true does the
> second argument "k>j" get evaluated. It has the effect of an AND operator
> but there are a couple of differences you should be aware of: 1) it's
> faster, and that's generally the appeal of splitting up the expression; and
> 2) because later expressions are not evaluated once an expression is
> evaluated as false, you may not get the side effects that you were counting
> on. For example:
> IF X>Y&$D(^Z(W)) WRITE ^(W)
> ELSE WRITE ^(V)
> This counts on executing the $D(^Z(W)) in order to set up the naked
> reference. If the & is replace by a comma and X is not greater than Y, then
> the $D won't be executed and the ELSE will likely fail. (Please, no
> comments from the PG regarding the use of naked references!)


I understand everything you've said. However, would you explain what a
"naked reference" is -- I have no clue what it refers to.
--
Duke Normandin
Reply With Quote
  #8  
Old 04-30-2008, 11:09 PM
Duke Normandin
Guest
 
Default Re: The `if' command

On 2008-04-30, Rob Tweed <rtweed@mgateway.com> wrote:
> If PG is forbidden, let me comment. Naked references are at best a


What do you mean by PG?

> daft invention and at worst just plain evil, and are to be avoided at
> all costs. Stick to explicit global references or you'll end up with
> an incomprehensible, unmaintainable mess. I'm sure someone somewhere
> will wade in with a wall of flame to extol their arcane virtues but
> I've never encountered a single situation in 28 years where they made
> any sense whatsoever.


I believe you! Once I know what "naked references" are, I'll be in a
better position to judge for myself.
--
Duke Normandin
Reply With Quote
  #9  
Old 04-30-2008, 11:19 PM
Duke Normandin
Guest
 
Default Re: The `if' command

On 2008-04-30, Philip Gage <philip@junk.pgage.demon.co.uk> wrote:
> In message <pURRj.3093$PM5.3083@edtnps92>, Duke Normandin
><dukeofperl@ml1.net> writes
>>set i=1,j=2,k=3
>>if i<j&k>j write "yes",!
>>
>>i _is_ less than j AND k _is_ greater than j
>>
>>so why is _yes_ NOT printed?
>>
>>The logical operator & tells me that both relational expressions must
>>be true in order for the _yes_ to be printed. That's exactly what it is,
>>so what am I missing? TIA......

> Left to right operator precedence.
> i<j is true so evaluated to 1
> 1&k is true so evaluates to 1
> 1>j is false so yes is not printed.
> to obtain the correct result the conditions should be in parentheses
>
> if (i<j)&(j>j) write yes.
>
> However an alternate would be
>
> if i<j,k>j write yes
>
> This is possibly preferred in that the command is broken into two
> commands, and the second one only executed if the first one is true.
>
> The reason for the preference is performance. The second condition is
> only evaluated if the first is true, so by putting the least likely
> condition first one can optimise the code.
>
> Optimisation should only be done when the code retains clarity, and when
> it is executed sufficiently often to justify the work.


Thanks for the great explanation! What is _your opinion_ of
"naked references" -- if you dare to go there.
--
Duke Normandin
Reply With Quote
  #10  
Old 05-01-2008, 01:01 AM
Maury Pepper
Guest
 
Default Re: The `if' command

"Duke Normandin" <dukeofperl@ml1.net> wrote in message
news:0waSj.3316$PM5.2929@edtnps92...
> On 2008-04-30, Rob Tweed <rtweed@mgateway.com> wrote:
>> If PG is forbidden, let me comment. Naked references are at best a

>
> What do you mean by PG?


See: http://en.wikipedia.org/wiki/Peanut_gallery
and: http://ask.yahoo.com/ask/20050720.html

>
>> daft invention and at worst just plain evil, and are to be avoided at
>> all costs. Stick to explicit global references or you'll end up with
>> an incomprehensible, unmaintainable mess. I'm sure someone somewhere
>> will wade in with a wall of flame to extol their arcane virtues but
>> I've never encountered a single situation in 28 years where they made
>> any sense whatsoever.

>
> I believe you! Once I know what "naked references" are, I'll be in a
> better position to judge for myself.
> --
> Duke Normandin


See: http://71.174.62.16/Demo/AnnoStd "The prefix ^ uniquely denotes a
global variable name. A global variable name is either unsubscripted or
subscripted; if it is subscripted, any number of subscripts separated by
commas is permitted. Except where otherwise specified, a subscript may not
equal the empty string. An abbreviated form of subscripted gvn is permitted,
called the naked reference, in which the prefix is present but the
environment, name and an initial (possibly empty) sequence of subscripts is
absent but implied by the value of the naked indicator. An unsubscripted
occurrence of gvn may carry a different value from any subscripted
occurrence of gvn."

If, instead of using naked reference for my example, I had used a function
with side-effects, it would probably have provoked a similar rant.

Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 04:13 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.