Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparison operators order?

Status
Not open for further replies.

Bontebok

Programmer
Nov 18, 2002
63
US
Greetings,

In regards to order of operations: How would the following comparison be calculated?

( {TYPE} = 'A' AND {CATEGORY} = 'ED-F' OR {CATEGORY} = 'EDN-F'

Would it be read from left to right as in VBScript? Or would the ORs be grouped first and evalulated against the and?

Thanks,

Jeremy
 
You would have to group the ORs:

{TYPE} = 'A' AND ({CATEGORY} = 'ED-F' OR {CATEGORY} = 'EDN-F'(

or you could do :

{TYPE} = 'A' AND {CATEGORY} in ['ED-F','EDN-F']

Reebo
Scotland (Sunny with a Smile)
 
I think it's better to use () then to know the answer to this one... :eek:)

Cheers,
- Ido

CUT, Visual CUT, and DataLink Viewer:
view, e-mail, export, burst, distribute, and schedule Crystal Reports.
 
I determined the following by creating a new report with every state of possible results from the following:

( Boolean AND Boolean OR Boolean )
and
( Boolean AND ( Boolean OR Boolean ) )

Out of every state, there are only two that evaluate incorrectly.

Incorrect comparison:
1. ( False AND True OR True ) = True
2. ( False AND False OR True ) = True

Correct comparison:
1. ( False AND ( True OR True ) = False
2. ( False AND ( False OR True ) = False


In this case, I have determined that there is a 1/6th chance that the statement could evaluate wrong.

I immediately rulled out the possibility of the first because it is impossible for both values on the left and right side of the OR to be True.

This is why I always use parenthesis around multiple comparison operators like this. This most likely produced bad reports. I will be evaluating the data shortly.

Jeremy
 
I'd not trust any language to work 100% for a complex set of commands. Years ago, I found a case in Cobol where a command should have been correct with its mix of AND and OR commands, but was in fact failing. Good practice in Cobol was always to avoid complex IF commands (as distinct from nested IFs, which do work).

Follow Reebo and Ido's advice, which also makes the command easier to understand if anyone else has to change it later on.

Madawc Williams
East Anglia, Great Britain
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top