WMLScript Developer's Guide

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

Current chapter: Chapter 4 - Operators and Expressions
Section 21 out of 57 total sections , Section 2 out of 3 sections in this chapter


Operators

Operators behave like predefined functions that are part of the WMLScript language. For example, the expression (x + y) is composed of the variables x and y combined with the + operator. WMLScript uses the + operator to return the sum of the two operands, x and y.

This section covers the following topics:



Assignment Operators

An assignment operator is a character that assigns a value to an operand. Assignment operators do not necessarily imply sharing of structure, nor does changing the value assignment of one operand change the binding of any other operand. WMLScript supports assignment with the following operators:

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 or modulus (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 

Example uses of assignment operators:



Arithmetic Operators

An arithmetic operator is a character that performs arithmetic operations. WMLScript supports basic, complex, and unary arithmetic operators.

Example uses of arithmetic operators:

Basic binary arithmetic operations:

Operator  Operation 
+  

add (numbers)/concatenation (strings) 

-  

subtract 

*  

multiply 

/  

divide 

div  

integer division 

Complex binary operations:

Operator  Operation 
%  

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

<<  

bitwise left shift 

>>  

bitwise right shift 

>>>  

bitwise shift right with zero fill 

&  

bitwise AND 

|  

bitwise OR 

^  

bitwise XOR 

Basic unary operations:

Operator  Operation 
+  

plus 

-  

minus 

--  

pre-or-post decrement 

++  

pre-or-post increment 

~  

bitwise NOT 



Logical Operators

Logical operators combine or negate relational expressions.

The basic logical operators 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 requires a Boolean value for logical operations and supports automatic conversion from other types to Boolean types.

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

String operators concatenate string variables. The + and += operators concatenate the strings. The standard String library supports other string operations.

Example uses of string operators:



Comparison Operators

Comparison operators compare values within a variable. Comparison operators are also known as relational operators.

WMLScript supports the following comparison operators:

Operator  Operation 
<  

less than 

<=  

less than or equal 

==  

equal 

>=  

greater than or equal 

>  

greater than 

!=  

inequality 

Comparison operators follow these rules:

Example uses of comparison operators:

In comparisons, the following hierarchy promotions occur:

Operand type  Conversion rules  Examples 

Boolean 

  • 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
 

Integer 

  • 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-point 

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

 

String 

  • 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

WMLScript does not support conventional arrays. However, the standard String library supports functions that mimic the behavior of conventional arrays.

The String library contains functions that create and manage character and string 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,"");  
}  


Comma Operators

Comma operators combine multiple evaluations into one expression. The result of the comma operator is the value of the second operand.

The following is an example of a comma operator:

for (a=1, b=100; a < 10; a++,b++) {  
    //... do something ...  
}  

Commas used in a function call to separate parameters or in a variable declaration to separate multiple variable declarations are not comma operators. In these cases, the comma operator must be placed inside parentheses as shown in the following example:

var a = 2;  
var b = 3, c = 3;  
myFunction("Name", 3*(b* a, c));  
    // Two parameters: "Name", 9  


Conditional Operators

Conditional operators assign a value to an expression based on the Boolean result of an initial statement. Conditional operators (?:) are essentially if-then-else statements that take three operands, 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.

The following is an example of a conditional operator:

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


typeof Operator

The typeof operator returns an integer value that describes the type of the given expression. The typeof operator does not convert the result from one type to another.

typeof returns a value based the data type as shown in the following table:

Type  typeof return code 

Integer 

Floating-point 

String 

Boolean 

Invalid 

The following is an example of a typeof operator:

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


isvalid Operator

The isvalid operator checks the validity of a variable or expression. The isvalid operator returns a Boolean value of false if the result type of an expression is invalid or if a variable not a valid type. If the result type of an expression is not invalid or if a variable is of a valid type, isvalid returns true. The isvalid operator does not convert the result from one type to another.

The following is an example of an isvalid operator:

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

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


Part Number DKDS-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.