operator precedence

The rules that decide which operators in an expression are applied first, such as multiplication before addition.

In more detail

Most languages follow the usual maths order for arithmetic, then apply comparisons, then logical operators, with and binding tighter than or. Parentheses override the default order, so an expression written without them can mean something other than it reads. Adding parentheses for clarity is always allowed, and is wise whenever a condition mixes and with or.

Examples

2 + 3 * 4   # 14
(2 + 3) * 4 # 20
WHERE (category_id = 1 OR category_id = 2) AND price_cents < 4000

Related terms

Taught in

References

Certification courses