12. WMLScript Conditional Statements

Conditional statements enable your script to make decisions. With conditional statements, you can specify different actions to be done when different conditions occur.


12.1. if Statement

WMLScript's if statement uses the following syntax. The part inside brackets [] is optional. The syntax is the same as that of C++, Java and JavaScript. You should be very familiar with the syntax if you have some programming experience with these languages.


if (condition)
{
  WMLScript statement(s)
}
[else
{
  WMLScript statement(s)
}]


If condition is the Boolean value true, the statement(s) enclosed in the first curly brackets {} will be executed; if condition is false or invalid, the statement(s) enclosed in the second curly brackets {} will be executed. The following WMLScript example demonstrates how to use the if statement:


...
var wmlscript_tutorial_partnum;
if (page_title=="WMLScript Tutorial Part 1")
{
  wmlscript_tutorial_partnum = 1;
}
else
{
  wmlscript_tutorial_partnum = 2;
}


If the value of the page_title variable is equal to the string "WMLScript Tutorial Part 1", the script "wmlscript_tutorial_partnum = 1;" will be executed. Otherwise the script "wmlscript_tutorial_partnum = 2;" will be executed.

In the above WMLScript example, there is only one statement enclosed within the curly brackets. In this case, the curly brackets can be omitted, which means the WMLScript example can be written like this:


...
var wmlscript_tutorial_partnum;
if (page_title=="WMLScript Tutorial Part 1")
  wmlscript_tutorial_partnum = 1;
else
  wmlscript_tutorial_partnum = 2;


Previous Page Page 26 of 71 Next Page


Feedback Form (ExpandCollapse)

What do you think about this web page?






(Optional) Please provide us more details. For example, suppose you select option 2 above, can you tell us specifically what information is missing? You can also suggest anything that can help us improve this web page.

(Optional) Your name:

(Optional) Your email address:

Please enter again to confirm:

Due to the amount of messages we received, we may not be able to reply to all messages.