Operators

Arithmetic operators

The following operators are available :

+

addition

-

subtraction

*

multiplication

/

division (integer mode)

mod

modulus arithmetic

()

parenthesis

-

unary negative operator (such as a=-5)

Examples :

a=3+5 ' a contains 8

a=5-3 ' a contains 2

a=3*5 ' a contains 15

a=11/2 ' a contains 5

a=11 mod 2 ' a contains 1

a=(3+2*3)*2 ' a contains 18

a=1

b=-a ' a contains -1

 

String operators

The character + is used to concatenate strings.

Example :

a$="Hello"

b$=" "

c$="WinTask"

d$=a$+b$+c$ ' sets variable d$ to "Hello WinTask"

(see Add$ Subtract$ Multiply$ and Divide$ to know how to deal with floating point numbers)

Array operators

Examples :

Dim tab$(200)

Dim tab1$(100)

Dim tab2$(100)

tab1$(25) = tab2$(3)

 

tab1$() = tab2$() ' the entire contents of the 2nd array are copied into the 1st. If the 1st array has more entries than the 2nd, the remaining ones are unchanged.

 

tab$()="blue" ' all the entries of the array are initialized with "blue"

 

If a$()=b$() then ' Tests the contents of two arrays. This condition is true only if the arrays have the same number of cells AND all corresponding entries in both arrays are equal.

 

Binary operators for integers

You can use these operators to extract one or several bits of an integer for checking them, or for assigning a specific value.

&

binary AND

|

binary OR

Examples :

a = 36 & 32 ' sets a to 32 (the 4th bit)

a = 32 | 2 ' sets a to 34 (the 4th and 2nd bits are set to 1)

 

Relational operators

They are :

<

Less than

<=

Less than or equal to

>

Greater than

>=

Greater than or equal to

=

Equal to

<>

Not equal to

These 6 operators can be used with integers or strings. Strings are compared character by character according to their ASCII value, starting from the left.

 

Logical operators

AND

Logical AND

OR

Logical OR

NOT

Not (unary)

Examples :

IF (a=b) OR (c=d) THEN ... ' TRUE if a=b or c=d

IF (a=b) OR NOT (c=d) THEN ...' TRUE if a=b or c not equal to d

 

Precedence of operators

Boolean expressions can be complex.

Example :

IF a=b OR b*c=32 AND NOT (c<>1) OR a$+b$=c$ THEN ...

Arithmetic operators are evaluated first, then relational operators, then logical operators.

For each category, precedence of operators is :
Arithmetic operators
Unary (-)
Multiplication and division (*,/)
Modulus (mod)
Addition and subtraction (+, -)

Concatenation operator
String concatenation (+)

Logical operators
Negation (NOT( ))
Logical AND (AND)
Logical OR (OR)

The relational operators have the same level of precedence. They are evaluated from left to right.

You can use parentheses to force the order of evaluation.