The Hyper Programming Language

Chained Compare Operator Expressions

General

This is a type of expression you don't see in C++ or in Java. It actually allows you to write a < b < c, without it being interpreted as (a < b) < c. (If you actually want to mean the latter, then you will have to write it explicitly like that by using the parentheses)

Semantics

A chained compare operator expression is a chain of expressions that are separated by comparison operators. It is a more natural notation for comparisons than having to compare a number of values two by two. Such an expression, like a < b < c, is almost equivalent to its version without chained compare operators, (a < b) & (b < c). There is one difference: the latter has guaranteed left-to-right evaluation while the former does not. This allows a compiler to evaluate the commonly used parts first when chains are used extensively in things like if-else constructs. The pointer comparison operators $$ and !$ are not allowed in a chain.

Comparison operators
Operator Meaning
= Equal
!= Not equal
< Less than
<= Less than or equal
> Greater than
>= Greater than or equal

Example

procedure p(a & b & c & d & e : int)
  var x : bool = a = b = c = d = e # x is true if all five parameters have the same value
  if a < b < c then
    # (a < b) & (b < c)
  else if b < c < a then
    # (b < c) & (c < a)
  else
    # The compiler will evaluate (b < c) first, and when it's false execution can
    # branch to here without first needing to evaluate (a < b) and (c < a).
  end
end

See also


Valid HTML 4.01 Strict Valid CSS!