The Hyper Programming Language

Conditional Expressions

General

A conditional expression consists of a condition expression, followed by a question mark, an expression, a colon and another expression.

A conditional expression c ? a : b returns the value of a if the condition c evaluates to true, and returns the value of b otherwise. The type of c must be implicitly convertable to bool. The types of a and b must be either equal, or an implicit type conversion must be available in exactly one direction. The condition expression is guaranteed to be evaluated first, followed by the expression that is to be returned as a result. The expression that is not returned is not evaluated.

Example

procedure p()
  var a & b : int
  var c : byte = 11
  var s : string = "hello"
  a = 10
  b = -3
  a += (a = b) ? b : c  # c converted to int; a becomes 21
  a -= (a > b) ? a : s  # ERROR: the types of a and s are incompatible
  a = ((a != c) ? s : "bye").length  # a becomes 3
end

See also


Valid HTML 4.01 Strict Valid CSS!