Relational and Boolean Expressions
In addition to arithmetic expressions, programming languages support relational and Boolean expressions.
A relational operator is an operator that compares the values of its two operands. A relational expression has two operands and one relational operator. The value of a relational expression is Boolean, except when Boolean is not a type included in the language. The relational operators are often overloaded for a variety of types. The operation that determines the truth or falsehood of a relational expression depends on the operand types. It can be simple, as for integer operands, or complex, as for character string operands. Typically, the types of the operands that can be used for relational operators are numeric types, strings, and ordinal types.
The syntax of the relational operators for equality and in equality differs among some programming languages. For example, for inequality, the C-based languages use !=, Ada uses /=, Lua uses ~=, Fortran 95 uses .NE. or <>, and ML and F# use <>.
"7" == 7
"7" === 7
is false, because no coercion is done on the operands of this operator.
Ruby uses == for the equality relational operator that uses coercions, and eql? for equality with no coercions. Ruby uses === only in the when clause of its case statement.
The relational operators always have lower precedence than the arithmetic operators, so that in expressions such as
a 1 > 2 * b
the arithmetic expressions are evaluated first.
Boolean expressions consist of Boolean variables, Boolean constants, relational expressions, and Boolean operators. The operators usually include those for the AND, OR, and NOT operations, and sometimes for exclusive OR and equivalence. Boolean operators usually take only Boolean operands (Boolean variables, Boolean literals, or relational expressions) and produce Boolean values.
In the mathematics of Boolean algebras, the OR and AND operators must have equal precedence. In accordance with this, Ada’s AND and OR operators have equal precedence. However, the C-based languages assign a higher precedence to AND than OR. Perhaps this resulted from the baseless correlation
of multiplication with AND and of addition with OR, which would naturally assign higher precedence to AND.
Because arithmetic expressions can be the operands of relational expressions, and relational expressions can be the operands of Boolean expressions,the three categories of operators must be placed in different precedence levels, relative to each other.
The precedence of the arithmetic, relational, and Boolean operators in the C-based languages is as follows:
Versions of C prior to C99 are odd among the popular imperative languages in that they have no Boolean type and thus no Boolean values. Instead, numeric values are used to represent Boolean values. In place of Boolean operands, scalar variables (numeric or character) and constants are used, with zero considered false and all nonzero values considered true. The result of evaluating such an expression is an integer, with the value 0 if false and 1 if true. Arithmetic expressions can also be used for Boolean expressions in C99 and C .
One odd result of C’s design of relational expressions is that the following expression is legal:
a > b > c
The leftmost relational operator is evaluated first because the relational operators of C are left associative, producing either 0 or 1. Then, this result is compared with the variable c. There is never a comparison between b and c in this expression.