WMLScript » Statements » for


The for statement repeatedly executes the same block of statements as long as a stated test condition, based upon a counting mechanism, remains true.

A counting variable is created that allows the for loop to execute (iterate) a specified number of times. As long as the test condition remains true, for each iteration of the counter, the block of statements is executed. The first time that the test condition returns false, the for loop is terminated, the block of statements is not executed, and the program jumps to the first line of code (if any) after the for loop.

The test condition is defined using three optional expressions.

  • The first expression sets the starting value for a counting variable. You can use the var statement to create the counting variable inside this expression.
  • The second expression defines the ending value for the counting variable. For each iteration, a boolean true or false test is performed on the expression. As long as true is returned, the for loop continues to iterate. When false is returned, the for loop is immediately terminated.
  • The third expression is used to increment or decrement the counting variable.
Note that the for loop does not have to finish all of the prescribed iterations. For example, you can leave a for loop by executing a break statement or calling the Lang.exit function.

The block of statements executed by the for loop can be any legal WMLS code (including more for statements).


<?xml version="1.0"?>

<card id="card1">
   for example
   <do type="accept">
      <go href="ForExample.wmls#findfor()" />

<card id="card2">
   for loop is over


Code for ForExample.wml

Language(s): WML
extern function findfor()
   var end = 95;
   for(var count=0; count < end; count++;)
      // You can place any code you desire here

Code for ForExample.wmls

Language(s): WML

See Also: