Case Expressions

Like its counter-part the case Statement, the case expression can return one (or none, i.e. nil) of a set of possible expressions, depending on the provided input:

Case expressions make it possible to use case in an expression instead of a standalone statement. Instead of statements, case requires a (single) expression for each element and its else clause:

var i: Integer;
var s := case i of
  0: 'none';
  1: 'one';
  2: 'two';
  3..5 : 'a few';
  else 'many';
end;

The result type of a case expression is the closest common type that can be inferred from all individual statements. For numerical values, it will be the closest type that can hold any of the returned (e.g. Double, if some cases return an Integer and others a Double).

If the else clause is omitted and no other case matches, the case expression returns the default value for its type (nil for a reference or nullable type, or 0-equivalent for a value type).

Sae Also