- Relational Operators
- Loops
- Decisions
- Logical Operators
- Precedence Summary
- Other Control Statements
- Summary
- Questions
- Exercises
Precedence Summary
Let's summarize the precedence situation for the operators we've seen so far. The operators higher on the list have higher precedence than those lower down. Operators with higher precedence are evaluated before those with lower precedence. Operators on the same row have equal precedence. You can force an expression to be evaluated first by placing parentheses around it.
You can find a more complete precedence table in Appendix B, "C++ Precedence Table and Keywords."
Operator type |
Operators |
Precedence |
Unary |
!, ++, , +, |
Highest |
Arithmetic |
Multiplicative *, /, % Additive +, |
|
Relational |
Inequality <, >, <=, >= Equality ==, != |
|
Logical |
And && Or || |
|
Conditional |
?: |
|
Assignment |
=, +=, =, *=, /=, %= |
Lowest |
We should note that if there is any possibility of confusion in a relational expression that involves multiple operators, you should use parentheses whether they are needed or not. They don't do any harm, and they guarantee the expression does what you want, even if you've made a mistake with precedence. Also, they make it clear to anyone reading the listing what you intended.