Chapter 2 The DynaScript Language


Expressions

Expressions are combinations of variables, literal values, operators, and other expressions. The right side of an assignment statement is an expression.

The following statements illustrate various kinds of expressions:

surfaceArea = (2 * 3.14159 * radius * height ) ;
surfaceArea = surfaceArea + ( 2 * 3.14159 * radius * radius ) ;
quote = "Full fathom five" + " thy father lies" ;
address = 10 + " Downing Street" ;
Name = "Moonunit " + 2 ;

Expressions and datatypes

Every expression has a value and a datatype. The datatype is one of numeric , string , Boolean , function , object , or null .

In some cases, the value of an expression is obvious; in others, it is not. For example, (2 * 3) is clearly a numeric expression with value 6 , and "To be or not to be" is a character string. However, it is not quite so obvious what the result of adding a number and a string is, as in the expression 10 + " Downing Street" .

To make meaningful evaluation of expressions possible, DynaScript has rules for automatic conversion of datatypes. In the above example, 10 is converted to a string and concatenated with the other string to give the final value.

Operators and datatypes

An operator can act on expressions of certain datatypes. For example, the multiplication operator (*) can act on numeric expressions, but cannot act on string expressions:

// valid use of multiplication
( 2 * 3.14159 * radius )
// invalid use of multiplication
"What" * "happens now?"

Some operators can act on more than one datatype. The most common of these is the addition operator (+). The addition operator can add two numbers together, or concatenate two strings.

Ternary, binary and unary operators

Operators are either ternary, binary, or unary. Binary operators act on two operands, such as the arithmetic operators:

x + y
45 / 56

Unary operators act on only one expression. For example, the postfix ++ operator adds one to the value of a variable:

x++ ;

The less common ternary operators act on three expressions.

 


Copyright © 1999 Sybase, Inc. All rights reserved.