Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Precedence and Associativity

This page defines the precedence and associativity rules that determine how expressions are grouped and evaluated.

Precedence

Expressions bind, tightest first, as follows:

  1. postfix operations,
  2. unary prefix operators and the type-forming unary prefixes (&T, []T),
  3. multiplicative operators,
  4. additive operators,
  5. comparison operators,
  6. logical conjunction (&&),
  7. logical disjunction (||), and
  8. conditional expressions.

Associativity

Binary operators are left-associative: repeated operators at the same precedence level group from left to right.

Conditional expressions are right-associative: nested conditional expressions group from right to left.

Syntax

This section restates the precedence-relevant productions; each production is defined canonically on its own page, and the complete grammar collects them in one place.

conditional_expression = logical_or_expression, [ question_mark, conditional_expression, colon, conditional_expression ] ;

logical_or_expression = logical_and_expression, { logical_disjunction_operator, logical_and_expression } ;

logical_and_expression = comparison_expression, { logical_conjunction_operator, comparison_expression } ;

comparison_expression = additive_expression, { comparison_operator, additive_expression } ;

additive_expression = multiplicative_expression, { additive_operator, multiplicative_expression } ;

multiplicative_expression = unary_expression, { multiplicative_operator, unary_expression } ;

unary_expression = { unary_operator | ampersand | left_bracket, right_bracket }, postfix_expression ;

unary_operator = negation_operator | logical_not_operator | length_operator ;

postfix_expression = primary_expression, { postfix_operation } ;

postfix_operation = call_operation | static_access_operation | dynamic_access_operation | subscript_operation | subslice_operation | construction_operation ;