Home All Groups Group Topic Archive Search About
Author
2 Feb 2006 12:09 AM
Richard Lewis Haggard
I'm porting some VB to C# and have run into a conditional that seems odd.
Would one of you who understands VB conditionals better than I please
confirm if the logic is correct? I can't figure out where the final AND
binds to in the conditional.

Here's the problem conditional:
If Not IsNumeric(str1) Or (Len(str1)>16 Or (Len(str1)<14 And Len(str1)<>5)
And str2="L") Then
    ' stuff
End If

The above, simplified, boils down to

if A Or (B Or (C And D) And E) then....

Given the above logic, which of these conditions resolve to true or false?
if 1 or (0 or (0 and 0) and 0) then true or false?
if 0 or (1 or (0 and 0) and 0) then true or false?
if 0 or (0 or (1 and 0) and 0) then true or false?
if 0 or (0 or (0 and 0) and 1) then true or false?

if 1 or (0 or (0 and 0) and 1) then true or false?
if 0 or (1 or (0 and 0) and 1) then true or false?
if 0 or (0 or (1 and 0) and 1) then true or false?
--
Richard Lewis Haggard
www.Haggard-And-Associates.com

Author
2 Feb 2006 4:08 AM
Dmitriy Antonov
Show quote Hide quote
"Richard Lewis Haggard" <HaggardAtWorldDotStdDotCom> wrote in message
news:O6$q%23z4JGHA.1532@TK2MSFTNGP12.phx.gbl...
> I'm porting some VB to C# and have run into a conditional that seems odd.
> Would one of you who understands VB conditionals better than I please
> confirm if the logic is correct? I can't figure out where the final AND
> binds to in the conditional.
>
> Here's the problem conditional:
> If Not IsNumeric(str1) Or (Len(str1)>16 Or (Len(str1)<14 And Len(str1)<>5)
> And str2="L") Then
>    ' stuff
> End If
>
> The above, simplified, boils down to
>
> if A Or (B Or (C And D) And E) then....
>
> Given the above logic, which of these conditions resolve to true or false?
> if 1 or (0 or (0 and 0) and 0) then true or false?
> if 0 or (1 or (0 and 0) and 0) then true or false?
> if 0 or (0 or (1 and 0) and 0) then true or false?
> if 0 or (0 or (0 and 0) and 1) then true or false?
>
> if 1 or (0 or (0 and 0) and 1) then true or false?
> if 0 or (1 or (0 and 0) and 1) then true or false?
> if 0 or (0 or (1 and 0) and 1) then true or false?
> --
> Richard Lewis Haggard
> www.Haggard-And-Associates.com
>

I don't think there can be any difference in the rules of precedence in both
languages. So precedence is (only those operators, used in given
expressions): parentheses, NOT, AND, OR. Same level of precedence evaluated
from left to right.

To simplify task, I've replaced 0s/1s with False/True and run them in VB

Debug.Print True Or (False Or (False And False) And False)
Debug.Print False Or (True Or (False And False) And False)
Debug.Print False Or (False Or (True And False) And False)
Debug.Print False Or (False Or (False And False) And True)
Debug.Print True Or (False Or (False And False) And True)
Debug.Print False Or (True Or (False And False) And True)
Debug.Print False Or (False Or (True And False) And True)

Results (in the same order):

True
True
False
False
True
True
False


Dmitriy
Author
2 Feb 2006 6:12 PM
Karl E. Peterson
Dmitriy Antonov wrote:
Show quoteHide quote
> "Richard Lewis Haggard" <HaggardAtWorldDotStdDotCom> wrote ...
>> I'm porting some VB to C# and have run into a conditional that seems
>> odd. Would one of you who understands VB conditionals better than I
>> please confirm if the logic is correct? I can't figure out where the
>> final AND binds to in the conditional.
>>
>> Here's the problem conditional:
>> If Not IsNumeric(str1) Or (Len(str1)>16 Or (Len(str1)<14 And
>> Len(str1)<>5) And str2="L") Then
>>    ' stuff
>> End If
>>
>> The above, simplified, boils down to
>>
>> if A Or (B Or (C And D) And E) then....
>>
>> Given the above logic, which of these conditions resolve to true or
>> false? if 1 or (0 or (0 and 0) and 0) then true or false?
>> if 0 or (1 or (0 and 0) and 0) then true or false?
>> if 0 or (0 or (1 and 0) and 0) then true or false?
>> if 0 or (0 or (0 and 0) and 1) then true or false?
>>
>> if 1 or (0 or (0 and 0) and 1) then true or false?
>> if 0 or (1 or (0 and 0) and 1) then true or false?
>> if 0 or (0 or (1 and 0) and 1) then true or false?
>
> I don't think there can be any difference in the rules of precedence
> in both languages.

But there _is_ a difference in the "value of True" and how boolean operators
work.

Show quoteHide quote
> So precedence is (only those operators, used in
> given expressions): parentheses, NOT, AND, OR. Same level of
> precedence evaluated from left to right.
>
> To simplify task, I've replaced 0s/1s with False/True and run them in
> VB
>
> Debug.Print True Or (False Or (False And False) And False)
> Debug.Print False Or (True Or (False And False) And False)
> Debug.Print False Or (False Or (True And False) And False)
> Debug.Print False Or (False Or (False And False) And True)
> Debug.Print True Or (False Or (False And False) And True)
> Debug.Print False Or (True Or (False And False) And True)
> Debug.Print False Or (False Or (True And False) And True)
>
> Results (in the same order):
>
> True
> True
> False
> False
> True
> True
> False

That's a far better way to think of it, yes!
--
Working without a .NET?
http://classicvb.org/