Comparison Operators

WMLScript provides the standard six comparison operators: equal to represented by == , not equal to represented by != , less than represented by < , less than or equal to represented by <= , greater than represented by > , and greater than or equal to represented by >= .

Warning

Expression the equal to comparison is the classic gotcha of C-like languages. You need to be careful to write == rather than = if you mean the “equal to” comparison operation. If by mistake you write:

if (a = b)

rather than:

if (a == b)

the result is still a valid WMLScript statement! However, rather than comparing a and b, it copies b into a and then tests if a can be converted to Boolean true. This can lead to some subtle bugs, so be careful.

These comparison operators all work by first checking if either operand is a string. If this is the case, they convert the other operand to a string and compare the two strings in dictionary order. Uppercase letters compare as less than lowercase, so "one" comes before "two", but "THREE" comes before "four". If one string is a prefix of another, the shorter one comes first, so "six" comes before "sixteen".

If neither operand is a string, the operators try to convert both operands to integers. If this works, the comparison is done on integers.

If the operands can’t both be converted to integers, the operators try to convert them to floating point: if this works, the comparison is done on floating-point values.

If all the attempts fail (this is the case if ...

Get Learning WML, and WMLScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.