Relational Operators

D provides the binary relational operators shown in the following table for use in your programs. These operators all have the same meaning as they do in ANSI-C.

Table 2-7 D Relational Operators

Relational Operator Description

<

Left operand is less than right operand.

<=

Left operand is less than or equal to right operand.

>

Left operand is greater than right operand.

>=

Left operand is greater than or equal to right operand.

==

Left operand is equal to right operand.

!=

Left operand is not equal to right 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 may 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. Here are some example D string comparisons and their results:

  • "coffee" < "espresso" ... returns 1 (true)

  • "coffee" == "coffee" … returns 1 (true)

  • "coffee" >= "mocha" ... returns 0 (false)

Relational operators may also be used to compare a data object associated with an enumeration type with any of the enumerator tags defined by the enumeration. You can use enumeration to create named integer constants. For more information, see Type and Constant Definitions in DTrace.