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 |
- 1. WMLScript Introduction
- 2. Hello World WMLScript Example
- 3. Compiling WMLScript Code
- 4. WMLScript Language Rules
- 5. Defining WMLScript Functions
- 6. Calling WMLScript Functions
- 7. WMLScript Variables
- 8. WMLScript Data Types
- 9. WMLScript Variables Vs WML Variables
- 10. Passing Arguments to Functions By Value and By Reference
- 11. WMLScript Operators
- 12. WMLScript Conditional Statements
- 13. WMLScript Looping Statements
- 14. WMLScript Standard Libraries Overview
- 15. WMLScript WMLBrowser Standard Library
- 16. WMLScript Dialogs Standard Library
- 17. WMLScript String Standard Library
- 18. WMLScript Float Standard Library
- 19. WMLScript Lang Standard Library
- 20. WMLScript URL Standard Library
- 21. WMLScript Example: Validating Form Data