1. Short Circuit Question
This is a multi-part message in MIME format.
------=_NextPart_000_0005_01C3A9B4.70253580
charset="iso-8859-1"
Can someone exlain why line to of the output actually tests both 1 and 3,
shouldn't just test 1?
is it interpreted as ((1 or 2) and 3) or as (1 or (2 and 3)) ?
Daniel Kluss
----------output
1 2 3 true
1 3 true
1 true
1 2 true
-----------program
without warning
function ptr(atom num)
printf(1,"%d ",{num})
return num
end function
if ptr(1) and ptr(2) and ptr(3) then puts(1,"true\n") else puts(1,"false\n") end
if
if ptr(1) or ptr(2) and ptr(3) then puts(1,"true\n") else puts(1,"false\n") end
if
if ptr(1) or ptr(2) or ptr(3) then puts(1,"true\n") else puts(1,"false\n") end
if
if ptr(1) and ptr(2) or ptr(3) then puts(1,"true\n") else puts(1,"false\n") end
if
------=_NextPart_000_0005_01C3A9B4.70253580
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: 8bit
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2800.1264" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>Can someone exlain why line to of the output
actually tests both 1 and 3, shouldn't just test 1?</FONT></DIV>
<DIV><FONT face=Arial size=2>is it interpreted as ((1 or 2) and 3) or as (1
or (2 and 3)) ?</FONT></DIV>
<DIV><FONT face=Arial size=2>Daniel Kluss</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>----------output</FONT></DIV>
<DIV><FONT face=Arial size=2>1 2 3 true<BR>1 3 true<BR>1 true<BR>1 2
true<BR>-----------program</DIV></FONT>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>without warning<BR>function ptr(atom
num)<BR> printf(1,"%d
",{num})<BR> return num<BR>end
function</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>if ptr(1) and ptr(2) and ptr(3) then
puts(1,"true\n") else puts(1,"false\n") end if</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>if ptr(1) or ptr(2) and ptr(3) then
puts(1,"true\n") else puts(1,"false\n") end if</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>if ptr(1) or ptr(2) or ptr(3) then puts(1,"true\n")
else puts(1,"false\n") end if</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>if ptr(1) and ptr(2) or ptr(3) then
------=_NextPart_000_0005_01C3A9B4.70253580--
2. Re: Short Circuit Question
On Thu, 13 Nov 2003 07:04:53 -0800, Daniel Kluss
<codepilot at netzero.net> wrote:
>
>Can someone exlain why line to of the output actually tests both 1 and 3,
>shouldn't just test 1?
>is it interpreted as ((1 or 2) and 3) or as (1 or (2 and 3)) ?
Like most operators, "and" & "or" are left-associative, which means:
a and b or c or d and e and f or g and h and i
is
((((((((a and b) or c) or d) and e) and f) or g) and h) and i)
Since it is confusing, always use parenthesis.
Pete
http://palacebuilders.pwp.blueyonder.co.uk/euphoria.html
3. Re: Short Circuit Question
Daniel Kluss wrote:
> Can someone exlain why line to of the output actually tests both 1 and
> 3, shouldn't just test 1?
> is it interpreted as ((1 or 2) and 3) or as (1 or (2 and 3)) ?
It's interpreted as ((1 or 2) and 3).
Since 1 is true, 1 and 3 are tested while 2 is skipped.
I know that some languages give higher precedence
to "and" vs "or", but in Euphoria "and" and "or"
have the same precedence. I didn't want to have too many
different precedence levels. C has about 15 levels.
Euphoria has about half that. After a couple of decades
of programming in C, I still find myself checking the
book to confirm the precedence of some operators.
You can use brackets to get any order of evaluation
that you require.
> ----------output
> 1 2 3 true
> 1 3 true
> 1 true
> 1 2 true
> -----------program
>
> without warning
> function ptr(atom num)
> printf(1,"%d ",{num})
> return num
> end function
>
> if ptr(1) and ptr(2) and ptr(3) then puts(1,"true\n") else
> puts(1,"false\n") end if
>
> if ptr(1) or ptr(2) and ptr(3) then puts(1,"true\n") else
> puts(1,"false\n") end if
>
> if ptr(1) or ptr(2) or ptr(3) then puts(1,"true\n") else
> puts(1,"false\n") end if
>
> if ptr(1) and ptr(2) or ptr(3) then puts(1,"true\n") else
> puts(1,"false\n") end if
Regards,
Rob Craig
Rapid Deployment Software
http://www.RapidEuphoria.com
4. Re: Short Circuit Question
>From: Daniel Kluss <codepilot at netzero.net>
>Subject: Short Circuit Question
>
>Can someone exlain why line to of the output actually tests both 1 and 3,
>shouldn't just test 1?
>is it interpreted as ((1 or 2) and 3) or as (1 or (2 and 3)) ?
>Daniel Kluss
>
It is interpreted as ((1 or 2) and 3). Logical AND and OR are at the same
precedence level, so they are interpreted left-to-right. Of course, even if
AND was higher than OR, then all three would have to be tested.
>----------output
>1 2 3 true
>1 3 true
>1 true
>1 2 true
>-----------program
>
>without warning
>function ptr(atom num)
> printf(1,"%d ",{num})
> return num
>end function
>
>if ptr(1) and ptr(2) and ptr(3) then puts(1,"true\n") else
>puts(1,"false\n") end if
>
>if ptr(1) or ptr(2) and ptr(3) then puts(1,"true\n") else puts(1,"false\n")
>end if
>
>if ptr(1) or ptr(2) or ptr(3) then puts(1,"true\n") else puts(1,"false\n")
>end if
>
>if ptr(1) and ptr(2) or ptr(3) then puts(1,"true\n") else puts(1,"false\n")
>end if
>
5. Re: Short Circuit Question
This is a multi-part message in MIME format.
------=_NextPart_000_0016_01C3AB94.9DD38360
charset="iso-8859-1"
Thanks that answers it for me
------=_NextPart_000_0016_01C3AB94.9DD38360
charset="iso-8859-1"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2800.1264" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>Thanks that answers it for
------=_NextPart_000_0016_01C3AB94.9DD38360--