Operators

An expression is any valid combination of operators, variables, numbers, strings, and WordBasic functions that can be evaluated to a single result. Depending on the kind of operator and values used, the result of an expression can be a number, string, or logical value, where the numbers –1 and 0 (zero) represent the logical values true and false, respectively. In WordBasic, there are four categories of operators to use with values to form expressions: arithmetic, string concatenation, comparison, and logical. This section describes the operators within these categories in order of operator precedence.

Operator Precedence

When several operations occur in an expression, each part is evaluated and resolved in a predetermined order known as operator precedence. Parentheses can be used to override the order of precedence and force some parts of an expression to be evaluated before others. Operations within parentheses are always performed before those outside parentheses.

Within parentheses, however, normal operator precedence is maintained. When expressions contain operators from more than one category, arithmetic operators (including the string concatenation operator) are evaluated first, comparison operators are evaluated next, and logical operators are evaluated last.

Within an expression, multiplication and division operations are evaluated before addition and subtraction operations. When multiplication and division occur together in an expression, each operation is evaluated as it occurs from left to right. Likewise, when addition and subtraction occur together in an expression, each operation is evaluated in order of appearance from left to right. All comparison operators have equal precedence; that is, they are evaluated in the left-to-right order in which they appear in an expression.

The string concatenation operator (+) is not really an arithmetic operator, but in precedence it does fall after all arithmetic operators and before all comparison operators.

Arithmetic Operators

Use these operators to generate any numeric value to assign to a variable or to use in input, output, or loops.

Operator

Description

– (Negation)

Indicates that the operand is a negative value. The operand can be any numeric expression.

* (Multiplication)

Multiplies two numbers. The operands can be any numeric expressions.

/ (Division)

Divides two numbers. The operands can be any numeric expressions.

MOD (Modular division)

Divides two operands and returns only the remainder. For example, the result of the expression 19 MOD 7 (which can be read as 19 modulo 7) is 5. The operands can be any numeric expressions.

+ (Addition)

Sums two numbers. The operands can be any numeric expressions.

Note that you also use + as the string concatenation operator.

– (Subtraction)

Finds the difference between two numbers. The operands can be any numeric expressions.


The String Concatenation Operator

Use the string concatenation operator to link literal strings and string variables.

Operator

Description

+ (String concatenation)

Concatenates two strings. For example, the result of "Microsoft " + "Word" is "Microsoft Word". You must ensure that spaces are included in the strings being concatenated to avoid running words or characters together.

If you use the Str$() function to return numbers as strings, note that the function adds a space before positive numbers (for example, Str$(47) returns " 47"), but not before negative numbers (for example, Str$(-47) returns "-47").

Note that you also use + as the addition operator.


Comparison Operators

Use these operators, also known as relational operators, to compare two expressions (numeric or string) and return true (–1) or false (0) values for use in control structures such as If conditionals and While¼Wend loops. The following table lists the comparison operators and the conditions that determine whether the result is true or false.

Operator

True

False

= (Equal to)

exp1 = exp2

exp1 <> exp2

<> (Not equal to)

exp1 <> exp2

exp1 = exp2

< (Less than)

exp1 < exp2

exp1 >= exp2

> (Greater than)

exp1 > exp2

exp1 <= exp2

<= (Less than or equal to)

exp1 <= exp2

exp1 > exp2

>= (Greater than or equal to)

exp1 >= exp2

exp1 < exp2


Logical Operators

Use these operators in combination with comparison expressions to create compound logical expressions that return true (–1) or false (0) values.

Operator

Description

AND

If, and only if, both expressions evaluate true, the result is true. If either expression evaluates false, the result is false. The result is determined as follows:

True AND True True

False AND True False

True AND False False

False AND False False


Operator

Description

OR

If either or both expressions evaluate true, the result is true. The result is determined as follows:

True OR True True

False OR True True

True OR False True

False OR False False

NOT

The result is determined as follows:

NOT False True

NOT True False

Note that a NOT compound expression evaluates as described only when the operands are comparisons or numeric true and false values, where true is –1 and false is 0 (zero).


True, False, and Bitwise Comparisons

In WordBasic, "true" is represented by the number –1, and "false" by the number 0 (zero). When WordBasic recognizes the number –1 as true and 0 (zero) as false, it is actually recognizing the values of each bit in the bytes that represent those numbers: –1 is the byte 1111 1111 and 0 (zero) is the byte 0000 0000. In fact, if WordBasic finds at least one "1" bit in any byte that represents a number, it recognizes the byte as true. Therefore, any nonzero number can represent true because the bytes for all nonzero numbers, both positive and negative, include at least one "1" bit. Only the byte for the number 0 (zero) contains all "0" bits and is therefore considered false.

When WordBasic evaluates a comparison — such as "A" = "A" or 5 < 2 —
it returns the standard true or false byte. But when WordBasic evaluates a compound expression (using one of the logical operators AND, OR, or NOT), it returns the byte for whatever number results from the eight "bitwise" comparisons that the logical operator makes with the original numbers. In a bitwise comparison, the operator compares each corresponding bit in the bytes that represent the values in the expression.

For example, in an AND bitwise comparison, if the first bit in each byte is set to 1, the first bit in the resulting byte is set to 1; otherwise, the bit is set to 0 (zero). In the expression "A" = "A" AND 5 < 2, the byte that represents "A" = "A" is 1111 1111 (the byte for –1, or true), and the byte that represents 5 < 2 is 0000 0000 (the byte for 0, or false). So WordBasic makes the following eight bitwise comparisons.

Bit in "A" = "A"

Bit in 5 < 2

Bit in AND result

1

0

0

1

0

0

1

0

0

1

0

0

1

0

0

1

0

0

1

0

0

1

0

0


The resulting byte is 0000 0000, which is the number 0 (zero). Therefore, because WordBasic considers the value 0 (zero) to be false, the result of "A" = "A" AND 5 < 2 is false.

Note

If the eight bitwise comparisons are made with bytes other than those representing –1 and 0 (zero), unexpected results may occur. (Remember that WordBasic recognizes any nonzero value as true because every nonzero value contains at least one "1" bit.) For example, with the compound expression 5 AND 2, where the byte for 5 is 0000 0101 and the byte for 2 is 0000 0010, the resulting byte is 0000 0000, which is the number 0 (zero). Because WordBasic always considers 0 (zero) to be false, the result of 5 AND 2 is false, even though the nonzero values 5 and 2 are considered "true" on their own.

In a compound expression, the three logical operators AND, OR, and NOT make the following bitwise comparisons for each bit in the bytes that represent the values in the expression.

AND

This operator returns the "1" bit if, and only if, both bits in the bytes being compared are "1" bits.

Bit in first byte

Bit in second byte

Bit in AND result

0

0

0

0

1

0

1

0

0

1

1

1


OR

This operator returns the "1" bit if either bit in the bytes being compared is a "1" bit.

Bit in first byte

Bit in second byte

Bit in OR result

0

0

0

0

1

1

1

0

1

1

1

1


NOT

This operator converts each bit in a single byte to its opposite bit in the result.

Bit in byte

Bit in NOT result

0

1

1

0


You can get unexpected results using the NOT operator with true values other than –1. For example, the number 1 evaluates true, but the expression NOT 1 also evaluates true. The result is true because 1 is the byte 0000 0001, and the NOT operator changes each bit to its opposite value; thus, the result of NOT 1 is the byte 1111 1110, which is the number –2. Just as WordBasic recognizes 1 as a numeric value for true, it also recognizes –2 as a numeric value for true.

A number of WordBasic functions can return the value 1. For example, Bold() returns 1 if all the current selection is bold and –1 if some of the current selection is bold. Consider the following instruction:


If Bold() Then MsgBox "Some or all of the selection is bold."

This instruction works reliably because both 1 and –1 evaluate true. But the following instruction will not work reliably:


If NOT Bold() Then MsgBox "None of the selection is bold."

If none of the selection is bold, Bold() returns 0 (zero), and the message box is displayed as expected. Likewise, if some of the selection is bold, Bold() returns –1, and the message box is not displayed. But if all the selected text is bold, Bold() returns 1; because NOT 1 is true (as shown earlier), the message box will be displayed, even though the selection is bold. To avoid unexpected results with NOT, you should use only the values –1 and 0 (zero) to represent true and false.