WMLScript Reference

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

Current chapter: Chapter 5 - Operators and Expressions
Section 18 out of 34 total sections


Chapter 5
Operators and Expressions


This chapter presents the operators and expressions used by WMLScript 1.1 to form complex expressions.

Included in this chapter are the following sections:



Assignment Operators


Definition

A character that assigns a value to a variable.


Examples

Examples of assignment operators are:

var a = "abc";  
var b = a;  
b = "def";      // Value of a is "abc"  

Comments

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.

Operator  Operation 
=  

assign 

+=  

add (numbers)/concatenate (strings) and assign 

-=  

subtract and assign 

*=  

multiply and assign 

/=  

divide and assign 

div=  

divide (integer division) and assign 

%=  

remainder (the sign of the result equals the sign of the dividend) and assign 

<<=  

bitwise left shift and assign 

>>=  

bitwise right shift with sign and assign 

>>>=  

bitwise right shift zero file and assign 

&=  

bitwise AND and assign 

^=  

bitwise XOR and assign 

|=  

bitwise OR and assign 



Arithmetic Operators


Definition

A basic binary character that performs arithmetic operations.


Examples

Examples of arithmetic operators are:

var y = 1/3;          // y = 0.333  
var x = y*3+(++b);  

Comments

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:

Operator  Operation 
%  

remainder, the sign of the result equals the sign of the dividend 

<<  

bitwise left shift 

>>  

bitwise right shift and sign 

>>>  

bitwise shift right with zero fill 

&  

bitwise AND 

|  

bitwise OR 

^  

bitwise XOR 

The basic unary operations supported by WMLScript 1.1 are:

Operator  Operation 
+  

plus 

-  

minus 

--  

pre-or-post decrement 

++  

pre-or-post increment 

~  

bitwise NOT 

In comparisons, the following hierarchy promotions occur:

Operand type  Conversion rules  Examples 

Booleans 

  • If the operand is of type boolean or can be converted into a boolean value, perform a boolean operation and return its value.

true && 3.4 => boolean
1 && 0 => boolean
"A" | | "" => boolean
!42 => boolean
 

  • Return invalid.

!invalid => invalid
3 && invalid => invalid
 

Integers 

  • If the operand is of type integer or can be converted into an integer value, perform an integer operation and return its value.

"7" << 2 => integer
true << 2 => integer
 

  • Return invalid.

7.2 >> 3 => invalid
2.1 div 4 => invalid
 

Floating-points 

  • If the operand is of type floating-point or can be converted into a floating-point value, perform a floating-point operation and return its value.

 

  • Return invalid.

 

Strings 

  • If the operand is of type string or can be converted into a string value, perform a string operation and return its value.

 

  • Return invalid.

 

Integer or floating-point (unary) 

  • If the operand is of type integer or can be converted into an integer value, perform an integer operation and return its value.

+10 => integer
-"33" => integer
+true => integer 1
-false => integer 0
 

 

  • If the operand is of type floating-point or can be converted into a floating-point value, perform a floating-point operation and return its value.

-10.3 => float
+"47.3" => float
 

  • Return invalid.

-"ABC" => invalid
-"9e9999" => invalid
 

Integers or floating points 

  • If at least one of the operands is of type floating-point, convert the remaining operand to a floating-point value, perform a floating-point operation, and return its value.

100/10.3 => float
3.4*"4.3" => float
"2.3"*"3" => float
 

  • If the operands are of type integer or can be converted into integer values, perform an integer operation and return its value.

33*44 => integer
"10"*3 => integer
"10"-"2" => integer
 

  • If the operands can be converted into floating-point values, perform a floating-point operation and return its value.

 

  • Return invalid.

3.2*"A" => invalid
.9*"9e999" =>invalid
invalid*1 => invalid
 

Integers, floating points, or strings 

  • If the operands are of type integer or can be converted into integer values, perform an integer operation and return its value.

12+3 => integer
3<false => integer
 

  • If at least one of the operands is of type floating-point, convert the remaining operand to a string value, perform a string operation, and return its value.

32.4+65 => float
43.2<77 => float
9.9+true => float
 

  • If at least one of the operands is of type string, convert the remaining operand to a string value, perform a string operation, and return its value.

"12"+5.4 => string
"Hey"<56 => string
2.7+"4.2" => string
 

  • Return invalid.

"A"+invalid => invalid 

Any 

Any type is accepted. 

a = 37.3 => float
b = typeof "s" => string
 



Logical Operators


Definition

Logical operators combine or negate relational expressions.


Comments

The basic logical operations are:

Operator  Operations 
&&  

logical AND 

||  

logical OR 

!  

logical NOT (unary) 

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  


String Operators


Definition

Characters that concatenate string variables.


Examples

Examples of string operators are:

var str = "Beginning" + "End";  
var chr = String.chartAt (str,10)        // chr = "E"  

Comments

The + and += operators concatenate the strings. Other string operations are supported by the standard String library (see String Library in Chapter 8, Libraries).



Comparison Operators


Definition

Characters that compare values within a variable. Also known as a "relational operator."


Examples

The following are examples of comparison operators:

var res = (myAmount > yourAmount);  
var val = (( 1/0) == invalid);        // val = invalid  

Comments

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:

In comparisons, the following hierarchy promotions occur:

Operand type  Conversion rules  Examples 

Booleans 

  • If the operand is of type boolean or can be converted into a boolean value, perform a boolean operation and return its value.

true && 3.4 => boolean
1 && 0 => boolean
"A" | | "" => boolean
!42 => boolean
 

  • Return invalid.

!invalid => invalid
3 && invalid => invalid
 

Integers 

  • If the operand is of type integer or can be converted into an integer value, perform an integer operation and return its value.

"7" << 2 => integer
true << 2 => integer
 

  • Return invalid.

7.2 >> 3 => invalid
2.1 div 4 => invalid
 

Floating-points 

  • If the operand is of type floating-point or can be converted into a floating-point value, perform a floating-point operation and return its value.

 

  • Return invalid.

 

Strings 

  • If the operand is of type string or can be converted into a string value, perform a string operation and return its value.

 

  • Return invalid.

 

Integer or floating-point (unary) 

  • If the operand is of type integer or can be converted into an integer value, perform an integer operation and return its value.

+10 => integer
-"33" => integer
+true => integer 1
-false => integer 0
 

 

  • If the operand is of type floating-point or can be converted into a floating-point value, perform a floating-point operation and return its value.

-10.3 => float
+"47.3" => float
 

  • Return invalid.

-"ABC" => invalid
-"9e9999" => invalid
 

Integers or floating points 

  • If at least one of the operands is of type floating-point, convert the remaining operand to a floating-point value, perform a floating-point operation, and return its value.

100/10.3 => float
3.4*"4.3" => float
"2.3"*"3" => float
 

  • If the operands are of type integer or can be converted into integer values, perform an integer operation and return its value.

33*44 => integer
"10"*3 => integer
"10"-"2" => integer
 

  • If the operands can be converted into floating-point values, perform a floating-point operation and return its value.

 

  • Return invalid.

3.2*"A" => invalid
.9*"9e999" =>invalid
invalid*1 => invalid
 

Integers, floating points, or strings 

  • If the operands are of type integer or can be converted into integer values, perform an integer operation and return its value.

12+3 => integer
3<false => integer
 

  • If at least one of the operands is of type floating-point, convert the remaining operand to a string value, perform a string operation, and return its value.

32.4+65 => float
43.2<77 => float
9.9+true => float
 

  • If at least one of the operands is of type string, convert the remaining operand to a string value, perform a string operation, and return its value.

"12"+5.4 => string
"Hey"<56 => string
2.7+"4.2" => string
 

  • Return invalid.

"A"+invalid => invalid 

Any 

Any type is accepted. 

a = 37.3 => float
b = typeof "s" => string
 



Array Operators


Definition

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,"");
};  


Comments

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.



Comma Operators


Definition

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 ...
};  


Comments

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  



Conditional Operators


Definition

Assign a value to an expression based on the boolean result of an initial statement.


Examples

The following is an example of a conditional operator:

myResult = flag ? "Off" : "On (value=" + level + ")";  

Comments

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.



typeof Operators


Definition

Return an integer value that describes the type of the given expression.


Examples

The following are examples of typeof operators:

var str = "123";  
var mytype = typeof str;        // myType = 2  

Comments

WMLScript 1.1 internally supports the following basic data types:

Type  Return code 

Integer 

Floating-point 

String 

Boolean 

Invalid 

The typeof operator does not convert the result from one type to another.



isvalid Operators


Definition

Check the validity of the given expression.


Examples

The following are examples of isvalid operators:

var str = "123";  
var ok = isvalid str;        // true  
var tst = isvalid (1/0);     // false  

Comments

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.



Expressions


Definition

Any combination of operators, constants, literal values, functions, or variable names.


Examples

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);  

Comments

WMLScript 1.1 supports most of the expressions common to other programming languages.


[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.