WMLScript Looping Statements

Preferences - Do not show ads

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.

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;

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

Print this Web Page | | Back to Top

Feedback Form (Expand)

What do you think about this web page?
