Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
AND and OR evaluations in conditional statements
Message
 
 
To
09/04/1999 15:04:37
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00206066
Message ID:
00207113
Views:
16
Jim,

Logical operators have precedence just like algebraic operators. Logic has an algebra just like numbers, there are a lot of the same theorems like Distributive X and (Y or Z) = X and Y or X and Z, Associative ( X or Y ) or Z = X or ( Y or Z ) and others like DeMorgan's !( X and Y ) = !X or !Y that are used to simply complex logical expressions. Click here for a reference Just like algebraic operators they work left to right, and you use parens to specify sub expressions that should have higher than normal precedence.

In numeric algebra w = x * y + z can be evaluated left to right, if the intent is to add y to z before multiplying parens can be used to increase the precedence of the sub expression: w = x * ( y + z )

In logic algebra w = x and y or z also can be evaluated left to right, but likewise if y or z is a sub expression parens are needed: w = x and ( y or z )

The VFP6 help accurately lists the precedence under the topics "Logical Operators" and "Numeric Operators"

The whole issue is somewhat clouded by the compiler optimizations that occur which will cause parts of the expression to not be evaluated, if they can have no effect on ultimate .T. or .F. value of the expression. Once a .T. condition is found (left to right) in a group of ORed sub expressions the result can not be any more true than it already is so no other sub expressions need be evaluated. Likewise once a .F. condition is found (left to right) in a group of ANDed sub expressions it can not turn .T. so the other sub expressions will not be evaluated.

>It looks to me like the word "precedence" is the problem here... where we are applying logic rather than mathematics:
>
>A "or" means that 'we can start again' whereas a "and" means that that which is to the left of the "and" is directly connected to what is to the right of the "and". Logic, it seems to me, can *only* be read/executed from left to right if it is going to come anywhere near what the writer intends. And, indeed, parantheses makes the reading much more straightforward while helping to govern the 'sequence' of execution.
df (was a 10 time MVP)

df FoxPro website
FoxPro Wiki site online, editable knowledgebase
Previous
Reply
Map
View

Click here to load this message in the networking platform