2.8.5 Relational Operators

D provides the binary relational operators that are described in the following table for use in your programs. These operators all have the same meaning that they do in ANSI C.

Table 2.8 D Relational Operators

Operator

Description

<

Left-hand operand is less than right-operand

<=

Left-hand operand is less than or equal to right-hand operand

>

Left-hand operand is greater than right-hand operand

>=

Left-hand operand is greater than or equal to right-hand operand

==

Left-hand operand is equal to right-hand operand

!=

Left-hand operand is not equal to right-hand operand


Relational operators are most frequently used to write D predicates. Each operator evaluates to a value of type int, which is equal to one if the condition is true, or zero if it is false.

Relational operators can be applied to pairs of integers, pointers, or strings. If pointers are compared, the result is equivalent to an integer comparison of the two pointers interpreted as unsigned integers. If strings are compared, the result is determined as if by performing a strcmp() on the two operands. The following table shows some example D string comparisons and their results.

D string comparison

Result

"coffee" < "espresso"

Returns 1 (true)

"coffee" == "coffee"

Returns 1 (true)

"coffee"" >= "mocha"

Returns 0 (false)

Relational operators can also be used to compare a data object associated with an enumeration type with any of the enumerator tags defined by the enumeration. Enumerations are a facility for creating named integer constants and are described in more detail in Section 2.13, “Type and Constant Definitions”.