Arithmetic Operators

WMLScript provides all the normal arithmetic operators. The syntax for these is exactly the same as in C or Java.

The simplest two arithmetic operators are unary plus, represented by +, and unary minus, represented by - . Unary plus simply converts the value to a number,[12] and unary minus converts the value to a number, then negates it (subtracts it from zero).

For example:

+2 is integer 2
-2 is integer -2
-"-2" is integer 2
-"2e0" is integer -2.0

After the unary operators, there are six binary arithmetic operators. Addition is represented by +, subtraction by - , and multiplication by * . There are two division operators: / , which operates on floating-point numbers and gives a floating-point result, and div , which operates on integers and gives an integer result. The final operator is the modulo or remainder operator, which gives the remainder after integer division and is represented by % .

All of these, other than div, should be familiar to you if you know C or Java. (C and Java use the datatypes of the operands and the result to determine whether to perform integer or floating-point division, so there’s no need for the div operator.)

There’s one slight complication to these operators: as mentioned in Operand Conversions earlier in this chapter, the + operator performs string concatenation rather than addition if either of its operands is a string. This can introduce subtle problems.

Examples:

3 * 2 gives integer 6
"3" * 2 gives integer 6
3 / 2

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.