This chapter presents the operators and expressions used by WMLScript 1.1 to form complex expressions.
Included in this chapter are the following sections:
A character that assigns a value to a variable.
Examples of assignment operators are:
var a = "abc";
var b = a;
b = "def"; // Value of a is "abc"
Assignment operators do not necessarily imply sharing of structure, nor does changing the value assignment of one variable change the binding of any other variable. The simplest form of assigning an operator is the regular assignment (=); but, assignment with the following operations is also supported.
A basic binary character that performs arithmetic operations.
Examples of arithmetic operators are:
var y = 1/3; // y = 0.333
var x = y*3+(++b);
WMLScript 1.1 supports all of the basic binary arithmetic operations:
| Operator | Operation |
|---|---|
+
|
add (numbers)/concatenation (strings) |
-
|
subtract |
*
|
multiply |
/
|
divide |
div
|
integer division |
The complex binary operations supported by WMLScript 1.1 are:
The basic unary operations supported by WMLScript 1.1 are:
In comparisons, the following hierarchy promotions occur:
Logical operators combine or negate relational expressions.
The basic logical operations are:
The logical AND operator evaluates the first operand and tests the result. If the result is false or invalid, the result of the operation is false or invalid, and the second operand is not evaluated. If the first operand is true, the result of the operation is the result of the second operand.
Similarly, the logical OR evaluates the first operand and tests the result. If the result is true or invalid, the result of the operation is true or invalid, and the second operand is not evaluated. If the first operand is false, the result of the operation is the result of the second operand.
weAgree = (iAmRight && youAreRight) ||
(!iAmRight && !youAreRight);
WMLScript 1.1 requires a boolean value for logical operations. Automatic conversions from other types to boolean types are supported.
NOTE
If the value of the first operand for logical AND or OR is invalid, the second operand is not evaluated, and the result of the operand is invalid.
var a = (1/0) || foo ();
// result: invalid, no call to foo ()
var b = true || (1/0); // true
var c = false || (1/0); // invalid
Characters that concatenate string variables.
Examples of string operators are:
var str = "Beginning" + "End";
var chr = String.chartAt (str,10) // chr = "E"
The + and += operators concatenate the strings. Other string operations are supported by the standard String library (see String Library in Chapter 8, Libraries).
Characters that compare values within a variable. Also known as a "relational operator."
The following are examples of comparison operators:
var res = (myAmount > yourAmount);
var val = (( 1/0) == invalid); // val = invalid
WMLScript 1.1 supports the following comparison operations:
| Operator | Operation |
|---|---|
<
|
less than |
<=
|
less than or equal |
==
|
equal |
>=
|
greater than or equal |
>
|
greater than |
!=
|
inequality |
Comparison operators use the following rules:
Boolean: true is larger than false
Integer: Comparison is based on the given integer values
Floating-point: Comparison is based on the given floating-point values
String: Comparison is based on the order of character codes of the given string values. Character codes are defined by the character set supported by the WMLScript 1.1 Interpreter
Invalid: If at least one of the operands is invalid, the result of the comparison is invalid
In comparisons, the following hierarchy promotions occur:
String library functions that mimic the behavior of conventional arrays.
The following is an example of a string function:
function dummy() {
var str = "Mary had a little lamb";
var word = String.elementAt (str,4,"");
};
WMLScript 1.1 does not support conventional arrays. However, the standard String library (see String Library in Chapter 8, Libraries) supports functions that mimic the behavior of conventional arrays.
A string can contain elements that are separated by a separator specified by the application programmer. The String library contains functions that create and manage character and string arrays.
Combine multiple evaluations into one expression.
The following is an example of a comma operator:
for (a=1, b=100; a < 10; a++,b++) {
... do something ...
};
The result of the comma operator is the value of the second operand.
Commas used in the function call to separate parameters and in the variable declaration to separate multiple variable declaration are not comma operators. In these cases, the comma operator must be placed inside the parentheses:
var a = 2;
var b = 3, c = 3;
myFunction("Name", 3*(b* a, c));
// Two parameters: "Name", 9
Assign a value to an expression based on the boolean result of an initial statement.
The following is an example of a conditional operator:
myResult = flag ? "Off" : "On (value=" + level + ")";
Conditional (?:) operators are essentially if-then statements. They take three operands, which are arranged as follows:
d = operand1 ? operand2 : operand3
operand1 is the condition being evaluated. If the condition is true, expression d results in the value or result of operand2. If the condition is false or invalid, expression d results in the value or result of operand3.
Return an integer value that describes the type of the given expression.
The following are examples of typeof operators:
var str = "123";
var mytype = typeof str; // myType = 2
WMLScript 1.1 internally supports the following basic data types:
The typeof operator does not convert the result from one type to another.
Check the validity of the given expression.
The following are examples of isvalid operators:
var str = "123";
var ok = isvalid str; // true
var tst = isvalid (1/0); // false
The isvalid operator returns a boolean value of false if the type of the expression is invalid, otherwise true is returned. isvalid does not convert the result from one type to another.
Any combination of operators, constants, literal values, functions, or variable names.
Expressions are either the single value of the constant or a variable.
567
66.77
"This is too simple"
'This works too'
true
myAccount
Expressions that are more complex can be defined by using simple expressions, operators, and function calls.
myAccount + 3
(a + b) / 3
initialValue + nextValue(myValues);
WMLScript 1.1 supports most of the expressions common to other programming languages.