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

Conditional Expression

A conditional expression selects between two branch expressions based on a condition.

Process

At runtime:

  1. the condition is evaluated;
  2. if it yields true, the then-expression is evaluated, and its value is the conditional's value; and
  3. otherwise, the else-expression is evaluated, and its value is the conditional's value.

Only the selected branch is evaluated.

Assertions

  1. The condition's type is bool.
  2. The branches have a common type; the conditional expression has that type.
  3. When an expected type exists, each branch's expected type is that type.

Syntax

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