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

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 ...

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