Laurel User Guide

4.6. Conditionals🔗

An if is usable both as a statement and as an expression:

procedure conditionals(x: int) returns (y: int)
  opaque
{
  if x > 0
  then {
    y := 1
  }
  else {
    y := 2
  };
  var sign: int :=
    if x > 0 then 1 else if x == 0 then 0 else -1;
  y := y + sign
};

With an else, both branches must agree with the expected type, or synthesize compatible types. Without one, the if produces nothing and can only be used where its value is discarded. Because if binds loosely, parenthesise it when it is an operand: (if c then 1 else 2) == y.