13. WMLScript Looping Statements
Looping statements are used to repeat the execution of a block of statements. The looping continues or stops based on some conditions. The while statement and the for statement are used for looping in WMLScript.
13.1. while Statement
WMLScript's while statement is used to repeat the execution of a block of statements while a condition is true. It has the following syntax:
while
(condition)
{
WMLScript statement(s)
}
The statement(s) enclosed in the curly brackets {} will be executed again and again as long as condition is true. The loop stops when condition evaluates to false or invalid. The curly brackets can be omitted if there is only one statement in the while loop. The following WMLScript example demonstrates how to use the while statement:
var
counter = 0;
var result = 0;
while (counter <
10)
{
result += 2;
counter++;
}
In the above WMLScript example, the counter variable is first initialized to 0. Every time the code inside the while loop is executed, the value of counter is incremented by 1. After executing the while loop for 10 times, the value of counter becomes 10 and the expression "counter < 10" is false. This quits the while loop. The WMLScript statement "result += 2;" has been executed 10 times totally. Hence, the result variable contains the value 20 when the while loop stops.
Previous Page | Page 27 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