8.7 If

  1. if
  2. GuardSeq1 ->
  3. Body1;
  4. ...;
  5. GuardSeqN ->
  6. BodyN
  7. end

The branches of an if-expression are scanned sequentially until a guard sequence GuardSeq that evaluates to true is found. Then the corresponding Body (sequence of expressions separated by ',') is evaluated.

The return value of Body is the return value of the if expression.

If no guard sequence is evaluated as true, an if_clause run-time error occurs. If necessary, the guard expression true can be used in the last branch, as that guard sequence is always true.


  1. is_greater_than(X, Y) ->
  2. if
  3. X>Y ->
  4. true;
  5. true -> % works as an 'else' branch
  6. false
  7. end