This chapter presents expressions, keywords, and syntax used in WMLScript 1.1.
Included in this chapter are the following sections:
Used when a statement is needed but no operation is required.
The following is an example of an empty statement:
while (!poll(device)); // Wait until poll() is true
Assign values to variables, calculate mathematical expressions, make function calls, and so on.
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);
A collection of statements enclosed in curly brackets that are treated as a single statement. Also known as compound statements.
The following is an example of a block statement:
{
var i = 0;
var x = Lang.abs(b);
popUp("Remember!");
}
A block statement can be used anywhere a single statement is needed.
Initialize variables.
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
};
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 ("").
A condition and one or two statements that are executed depending on the boolean value of the condition.
The following is an example of an if statement:
if (sunShines) {
myDay = "Good"
goodDays++;
} else
myDay = "Oh well...";
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.
Create a loop that evaluates an expression and, if it is true, executes a statement.
The following is an example of a while statement:
var counter = 0;
var total = 0;
while (counter < 3) {
counter++;
total += c;
};
The loop repeats as long as the specified condition is true.
Create loops that execute as long as a stated condition is true.
The following is an example of a for statement:
for (var index = 0; index < 100; index++) {
count += index;
myFunc(count);
};
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.
Terminate the current while or for loop and continue the program execution from the statement following the terminated loop.
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;
};
Using a break statement outside a while or a for statement generates an error.
Terminate execution of a block of statements in a while or for loop and continue execution of the loop with the next iteration.
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;
};
The continue statement does not terminate the execution of the loop:
In a while loop, it returns to the condition
In a for loop, it jumps to the update expression
NOTE Using a continue statement outside of a while or a for statement generates an error.
Used inside the function body to specify the function return value.
The following is an example of a return statement:
function square (x) {
if (!(Lang.isFloat(x))) return invalid;
return x * x;
If no return statement is specified or none of the function return statements is executed, the function returns an empty string.