O'Reilly logo

Learning WML, and WMLScript by Martin Frost

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Comma Operator

The , operator in WMLScript isn’t used very often. Its effect is to evaluate its left operand, throw away the result, then evaluate its right operand. The result of the , operator is simply its right operand.

This behavior may seem bizarre if you haven’t come across this operator before. It’s used only when the left operand has some sort of side effect (such as an assignment to a variable or a function call). The most common use is in the initializer and increment sections of a for loop (see Chapter 12). For example:

for (i=0, order=1, total=0; i<count; ++i, order *= 10) {
    total += foo (i, order);
}

In this loop, the initializer uses the , operator to initialize all three variables i, order, and total. In addition, the increment part uses the , operator not only to increment i, but also to multiply order by 10.

If the , operator is used in an argument to a function call or in a variable initializer, there can be some ambiguity. (Does the , represent the operator, or simply the comma before the next argument or variable declaration?) To avoid this, the whole expression containing the , must be parenthesized, but it’s probably clearer to simply avoid using the , operator at all in these situations. Instead, simply add another statement. For example:

var x = foo(), bar( );

is wrong, because there are no parentheses around the , expression. It should instead be written as:

var x = (foo (), bar ( ));

However, it’s much clearer to write instead:

foo ( );
var x = bar ( );

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required