51
TIDE and Tibbo BASIC User Manual
©2000-2008 Tibbo Technology Inc.
variable.
y = x(
4
)
' y would get a value of -30, according to the previous array
y = x(z)
' y would get the value of index z within array x.
As an example, you could iterate through an array with a loop (such as a
) and execute code on each element in the array, by using a
variable to refer to the index of an element. Let's see how to calculate the sum of
the first three elements in the array we previously defined:
dim
x(
5
)
as
char
x(
0
) =
15
x(
1
) =
32
x(
2
) =
4
x(
3
) =
100
x(
4
) = -
30
dim
sum
as
integer
sum =
0
for
i =
0
to
4
' note that you do not necessarily have to iterate through
all elements in the array.
sum = sum + x(i)
next
i
' now, at the end of this loop, sum contains the sum for the first three
elements (51)
The TIDE and Tibbo Basic
V2.0
introduced correct handling of array indices. It is
no longer possible for your program to point to an array element that does not
exist. For example, if your array only has 5 elements and you try to access
element number 5 the Virtual Machine will:
and halt if your program is in the
mode. If you attempt to continue the Virtual Machine will access x(4)
-- the element with maximum available index.
When in the
mode, the OOR exception will not be generated but
"index limiting" will still occur.
Example:
dim
x(5)
as
char
dim
f
as
byte
f=5
x(f)=3
'index limiting will happen here (preceded by the OOR exception if
you are in the debug mode)
Compiler is smart enough to notice out-of-range situations even at compile time.
For example, the following code will generate an error during compilation:
86
29
27
27