WMLScript Reference

[Cover] [Previous Section] [Next Section] [Index]

Current chapter: Chapter 7 - Statements
Section 22 out of 34 total sections


Chapter 7
Statements


This chapter presents expressions, keywords, and syntax used in WMLScript 1.1.

Included in this chapter are the following sections:



Empty Statements


Definition

Used when a statement is needed but no operation is required.


Examples

The following is an example of an empty statement:

while (!poll(device));      // Wait until poll() is true  


Expression Statements


Definition

Assign values to variables, calculate mathematical expressions, make function calls, and so on.


Examples

The following are examples of an expression statement:

str = "Hey" + yourName;  
 
val3 = prevVal + 4;  
 
counter++;  
 
myValue1 = counter, myValue 2 = val13;  
 
alert("Watch out!");  
 
retVal = 16*Lang.max(val3, counter);  


Block Statements


Definition

A collection of statements enclosed in curly brackets that are treated as a single statement. Also known as compound statements.


Examples

The following is an example of a block statement:

{
    var i = 0;
    var x = Lang.abs(b);
    popUp("Remember!");
}  


Comments

A block statement can be used anywhere a single statement is needed.



Variable Statements


Definition

Initialize variables.


Examples

The following are examples of variable statements:

function count(str) {
    var result = 0;        // Initialized once
    while(str != "") {
        var ind = 0;       // Initialized every time
            // modify string
        };
return result
};  

 
function example(param) {
    var a = 0;
    if (param > a) {
        var b = a+1;  // Variables a and b can be used
    } else {
        var c = a+2;  // Variables a, b, and c can be used
    };
    return a:         // Variables a, b, and c are accessible
};  


Comments

Variable names, which can be any legal identifier, must be unique within a single function. The scope of the declared variable is the rest of the current function; the expression is evaluated every time the variable statement is executed.

Variables can be initialized either to a specified value or, by default, to an empty
string ("").



If Statements


Definition

A condition and one or two statements that are executed depending on the boolean value of the condition.


Examples

The following is an example of an if statement:

if (sunShines) {
    myDay = "Good"
    goodDays++;
} else
    myDay = "Oh well...";  


Comments

An if statement consists of a condition and one or two statements (or block statements); the first statement is executed if the specified condition is true; if the condition is false or invalid, the second (optional) statement is executed. The statements can be any WMLScript 1.1 statements, including other nested if statements.



While Statements


Definition

Create a loop that evaluates an expression and, if it is true, executes a statement.


Examples

The following is an example of a while statement:

var counter = 0;
var total = 0;
while (counter < 3) {
    counter++;
    total += c;
};  


Comments

The loop repeats as long as the specified condition is true.



For Statements


Definition

Create loops that execute as long as a stated condition is true.


Examples

The following is an example of a for statement:

for (var index = 0; index < 100; index++) {
    count += index;
    myFunc(count);
};  


Comments

The for statement consists of three optional expressions enclosed in parentheses and separated by semicolons followed by a statement-executed loop.

Typically, the first expression (var index = 0 in the example above) is used to initialize a counter variable. This expression can declare a new variable with the var keyword. The scope of the declared variable is the rest of the function.

The second expression can be any WMLScript 1.1 expression that evaluates to a boolean or an invalid value. This condition is evaluated on each pass through the loop. If the condition is true, the statement is performed. This conditional test is optional. If omitted, the condition always evaluates to true.

The third expression is generally used to update or increment the counter variable. This statement is executed as long as the condition is true.



Break Statements


Definition

Terminate the current while or for loop and continue the program execution from the statement following the terminated loop.


Examples

The following is an example of a break statement.

function testBreak(x) {
    var index = 0;
    while (index < 6) {
        if (index == 3) break;
        index ++
    };
    return index*x;
};  


Comments

Using a break statement outside a while or a for statement generates an error.



Continue Statements


Definition

Terminate execution of a block of statements in a while or for loop and continue execution of the loop with the next iteration.


Examples

The following is an example of a continue statement:

var index = 0
var count = 0
while (index < 5) {
    index++;
    if (index == 3)
        continue;
    count += index;
};  


Comments

The continue statement does not terminate the execution of the loop:

NOTE   Using a continue statement outside of a while or a for statement generates an error.



Return Statements


Definition

Used inside the function body to specify the function return value.


Examples

The following is an example of a return statement:

function square        (x) {
    if (!(Lang.isFloat(x))) return invalid;
    return x * x;  


Comments

If no return statement is specified or none of the function return statements is executed, the function returns an empty string.


[Cover] [Previous Section] [Next Section] [Index]


Part Number DKWS-41-002, UP.SDK Release 4.1, December 2000

Copyright © 1994-2000 Openwave Systems Inc. All rights reserved.
Please send comments and questions to sdk-doc-comments@openwave.com.