Futurebasic/Language/numeric expressions

Numeric Expressions
Revised: May 30, 2000 (Release 3)

A numeric expression is anything that can be evaluated as a number. A number can be expressed in the following ways:

Simple expressions
 A numeric literal, a symbolic constant, or a numeric variable. See Appendix C:Data Types and Data Representation, for more information. Examples: 17.3     _true      x&amp;      Z%(14) A reference to any user-defined function or Toolbox function that returns a numeric value. Examples: FN theSum#     FN GETCATINFO(@pb) A value returned by any built-in FB function whose name does not end with " ". (Note: the two exceptions to this are the  function and the   function, both of which return a string value.) Examples: LEN(&quot;Hello&quot;)     DIALOG(0) 

Data comparison expressions
Data comparison expressions always return the value -1 or 0. In many contexts, these values are interpreted as meaning "true" or "false," respectively. Data comparison expressions have the following forms:

Equality comparisons
An equality comparison consists of two expressions of "compatible types" separated by the " " operator or the " " operator (you can also use " " as a synonym for " ", and " " as a synonym for " "). The two operands must fall into one of the following categories:

 A pair of numeric expressions; A pair of string expressions (see Appendix E: String Expressions); Any pair of variables of the same type. 

An equality comparison using " " (or " ") is evaluated as -1 if the two operands have equal values; otherwise it's evaluated as 0. An equality comparison using " " (or " ") is evaluated as -1 if the two operands are not equal; otherwise it's evaluated as 0. Examples:

Order Comparisons
An order comparison can test the relative "order" of two numeric operands, or of two string operands; that is, it tests whether one operand is greater than or less than the other. In the case of strings,  is considered "less than"   if it precedes   alphabetically. More accurately, string comparison depends on the ASCII values of the characters in the strings. An order comparison takes the form, where   and   are both numeric expressions or both string expressions, and   is one of the operators in this table:



Examples:

Expressions with Unary Operators
A unary operator is an operator that takes only one operand. FB has three unary operators: " "; " "; " ". The unary operator always appears on the left side of the operand; the operand can be any numeric expression.



Examples:

Compound Expressions
A compound numeric expression is any list of numeric expressions separated by one or more of the operators in the table below. A compound expression has this form:

Additionally, any expression which is surrounded by a pair of parentheses is also an expression. When you surround an expression with parentheses, the entire expression within parentheses is evaluated before any operator to the left or right of the parenthetical expression is applied. This is useful when you want to change the default order in which the operators are applied. For example:

In the above expression, the " " operator is applied before the " " operator. 3 is multiplied by the sum of 7 and 1, giving a result of 24. But if the expression had been written like this:

then the " " operator would have been applied first. In this case, 1 is added to the product of 3 and 7, giving a result of 22.



Examples:

Operator Precedence
When an expression includes more than one operator, the order in which the operations are performed can affect the result. When an operator appears to the left or right of a parenthetical expression, all of the operations within the parentheses are performed first. When several operators all appear within the same matching pair of parentheses (or outside of all parentheses), the order in which their operations are performed is determined by their order of precedence, with "higher precedence" operations being performed before "lower precedence" ones. For example, consider this expression:

The " " operator has a higher precedence than the " " operator (see the table below). So, when this expression is evaluated, first 7 is multiplied by 5 to get 35; then that result is added to 4 to get the final answer of 39.

The following table lists the operators in order of their precedence, from highest to lowest. When an expression contains several operators at the same level of precedence (and within the same depth of parentheses), their operations are always performed from left to right.



Example: Consider the following expression:

The following shows the series of operations that FB performs to reduce this expression to its final value, 31.

Static Integer Expressions
Many FB statements require quantities that are expressed as static integer expressions. A static integer expression may be a simple or complex expression, but its operands are limited to the following:

 Integer literals; Symbolic constants; The,   and   functions. 

The following are examples of valid static integer expressions:

The following are not valid static integer expressions: