Understanding operator precedence – Pitney Bowes MapInfo Professional User Manual

Page 445

Advertising
background image

In formulating expressions using logical operators you have to be careful how you use them. The following
expression, while it seems OK, does not work:

1. COUNTY="Columbia" or "Greene"

Judging from its English translation-COUNTY equals Columbia or Greene-this expression should operate
just like expression 36 and give us any record containing Columbia County or Greene County. But the
rules of computational logic and the rules of English are a bit different.

When MapInfo Professional reads expressions it reads them from left to right. One of the things it has
to do is to determine how the items in the expression are grouped. Think of this operation as inserting
parentheses into the expression. MapInfo Professional reads expression 35 as though it were grouped
like expression 39, which is what we intend.

It reads expression 38 as though it were grouped like 40, which is not at all what we want.

1. (COUNTY="Columbia") or (COUNTY="Greene")

2. (COUNTY="Columbia") or ("Greene")

Both 39 and 40 have the same first clause. But their second clauses (after the "or") are quite different.
The second clause of 40 is simply a literal string, "Greene." By convention, MapInfo Professional evaluates
a record against a literal string as being true if that record is not blank. When MapInfo Professional
evaluates records against expression 40, all non-blank records are evaluated as true and be accepted
into the selection. It does not make any difference how a record evaluates on the first clause. Any
non-blank record evaluates as true on the second clause, and one "true" is all it takes to evaluate the
entire expression as true.

Let us consider one final example, which is the negation of expression 36:

1. not (COUNTY="Greene" or COUNTY="Columbia")

Expression 39 is satisfied if the county is Greene or if it is Columbia, but not if it is Montgomery or Warren.
Expression 41 is satisfied by any county other than Greene or Columbia, including Montgomery and
Warren.

The Help System contains these related topics:

Numeric Clauses

String Clauses

Date Clauses

Keyword Clauses

Understanding Operator Precedence

When MapInfo Professional evaluates expressions it needs to know which components of an expression
to evaluate first. This is called precedence. By convention, certain operators are assigned different levels
of precedence. Those with the highest level are evaluated first. The following table lists MapInfo
Professional's operators in the order in which they are evaluated. Exponentiation evaluates from the
right. This affects expressions with multiple exponents: 2 ^ -3 ^ -4 = 2 ^ ( - (3 ^ (-4)))

Operators at the same level of precedence are evaluated from left to right.

Parenthesis

Highest Priority:

Exponentiation

Negation

Multiplication, Division

Addition, Subtraction

Geographic operators, Comparison operators

Not

445

MapInfo Professional User Guide

Chapter 17: Specialized Topics in MapInfo Professional

Advertising