Conditions

Conditional statements in WMLScript behave just like they do in C. The simplest form looks like:

if (condition) statement-when-true

The condition is simply any expression. It’s evaluated, and if the result can be converted to Boolean true, the statement-when-true is executed. For example:

if (x > max)
    max = x;

An else clause can also be added, looking like:

if (condition) statement-when-true
else
    statement-when-false

If the condition can be converted to Boolean true, statement-when-true is executed, but if the condition converts to Boolean false or can’t be converted, statement-when-false is executed. For example:

if (number == 1)
    result = "1 apple";
else
    result = number + " apples";

Sometimes, the statement-when-false is itself another if. There is nothing special about this: the first condition is evaluated, and if it’s false or can’t be converted, the second if is executed. For example:

if (x > max)
    max = x;
else if (x < min)
    min = x;

Matters are more interesting if the statement-when-true contains an if with an else clause. For example, consider:

if (x)
    if (y)
        foo ( );
    else
        bar ( );

What exactly does this mean? The indentation suggests that the else belongs with the second if, but the compiler doesn’t look at the indentation: all spaces are the same to it. The same code could equally be interpreted as:

if (x)
    if (y)
        foo ( );
else
    bar ( );

(That is, with the else attached to the first if.)

This ambiguity is known as the dangling else problem and affects many different programming ...

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.