Array Operations


Normally operations apply only to single values, so a+b expresses the addition of two numbers. Normally adding two arrays requires indexing and looping:


110 For i=0 to N

120 For j=0 to N

130 A(J,I) = A(J,I) + B(J,I)

140 Next j

150 Next i


This need to loop and index to perform operations on arrays is both tedious and error prone.

But in this version of Basic you can simply add the arrays together.


A() = A() + B()


The single line of code above performs the same function as the double loop, only much faster.

Entire arrays can be initialized in a single line.


A() = 10


Here every element of A() is set to 10.

Array operations can get as complex as you need.


A() = A() * B() / C() + 0.5


One general rule is to keep all the array variables to the left and all the scalars (single values and variables) to the right.


For example

A() = A() + 0.5

works fine, but

A() = 0.5 + A()

will generate an error.


All the array variables to the right of the equals sign must be dimensioned the same.

But, the array variable to the left of the equals sign may be dynamically redimensioned to the correct size.


For example:

Dim a(0)

Dim b(10000)

b() = 256

a() = b()

Now a() will have the same dimensions as b() and will contain same values as b().


It's easy to get lost or confused when mixing arrays and scalars in equations.

Please experiment and test your results carefully.




Available operators

^

(exponent)

*

(multiplication)

/

(division)

+

(addition)

-

(subtraction)

%

(remainder)

mod

(modulus)

&

(bitwise and)

|

(bitwise or)

||

(bitwise xor)

~

(bitwise not)

<<

(shift left)

>>

(shift right signed)

>>>

(shift right unsigned)


Operator precedence
(highest to lowest)

( )

functions(x)

-{unary minus}

^

* / % mod

+ - & | || << >> >>>



Arrays and Subscripts







Become a Patron