Priority of operators
When more than one type of operator appears in an expression,
what operation does the language processor do first?
IF (A > 7**B) & (B < 3)
Like the priority of operators for the arithmetic operators, there is an overall priority that includes all operators. The priority of operators is as follows with the highest first.
| Operator symbol | Operator description |
|---|---|
| \ or ¬ - + | Prefix operators |
| ** | Power (exponential) |
| * / % // | Multiply and divide |
| + - | Add and subtract |
| blank || abuttal | Concatenation operators |
| == = >< and so on | Comparison operators |
| & | Logical AND |
| | && | Inclusive OR and exclusive OR |
Thus, given the following values
- A = 8
- B = 2
- C = 10
IF (A > 7**B) & (B < 3)
as
follows:
- Evaluate what is inside the first set of parentheses.
- Evaluate A to 8.
- Evaluate B to 2.
- Evaluate 7**2.
- Evaluate 8 > 49 is false ( 0).
- Evaluate what is inside the next set of parentheses.
- Evaluate B to 2.
- Evaluate 2 < 3 is true ( 1).
- Evaluate 0 & 1 is 0
Exercises: priority of operators
- What are the answers to the following examples?
- 22 + (12 * 1)
- -6 / -2 > (45 % 7 / 2) - 1
- 10 * 2 - (5 + 1) // 5 * 2 + 15 - 1
- In the example of the student and the college from the previous
exercise in
Logical (Boolean) Operators
, if the parentheses
were removed from the student's formula, what would be the outcome
for the college?
IF inexpensive | scholarship & reputable | nearby THEN SAY "I'll consider it." ELSE SAY "Forget it!"Remember the college is inexpensive, did not offer a scholarship, is reputable, but is 1000 miles away.
ANSWERS
- The results are as follows:
- 34 (22 + 12 = 34)
- 1 (true) (3 > 3 - 1)
- 32 (20 - 2 + 15 - 1)
- I'll consider it.
The & operator has priority, as follows, but the outcome is the same as the previous version with the parentheses.
IF inexpensive | scholarship & reputable | nearby THEN \_________/ \_________/ \_______/ \____/ true false true false \ \___________/ / \ false / \_________________/ / true / \____________________/ true