WAP - WML Control Statements



WML Script if...else Statement

WMLScript's if…else statement uses the following syntax. The part inside brackets [] is optional. The syntax is the same as that of C++, Java, and JavaScript.

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.

WML Script 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.

WML Script for Statement

Like a while loop, a for loop is executed repeatedly as long as a condition is satisfied. If the number of times to be repeated is certain, using the for statement will be more convenient than using the while statement. The for statement has the following syntax in WMLScript. The parts enclosed in brackets [] are optional.

for ([expression1]; [expression2]; [expression3]) {
  WMLScript statement(s)
}

expression1 is the initialization expression. It is executed before any WMLScript statements in the for loop are executed. expression1 is executed once only in the lifetime of the for statement. expression2 is the conditional expression that determines whether the for loop should continue or stop. expression3 is executed after each iteration.

WML Script break Statement

The break statement is used to quit a loop. It must be put inside while loops or for loops. The following WMLScript example demonstrates how to use the break statement −

var result = 0;
for (var counter=0; counter<10; counter++) {
  break;
  result += 5;
}

After the execution the above WMLScript code, the value of result is 0. This is because the break statement exits the for loop. The statement "result += 5;" is never executed.

WML Script continue Statement

The continue statement is used to quit the current iteration of a loop in WMLScript. The next iteration will be started if the loop's conditional expression evaluates to true. The continue statement must be put inside while loops or for loops. The following script demonstrates how to use the continue statement −

var result1 = 0;
var result2 = 0;
for (var counter=0; counter<10; counter++) {
  result1 += 5;
  continue;
  result2 += 5;
}

After the execution of the above WMLScript code, the value of result1 is 20 and that of result2 is 0. This is because when the WMLScript interpreter encounters the continue statement, it will end the current iteration. Hence, the statement "result2 += 5;" is never executed.

wap_wml_script.htm
Advertisements