Small Basic: Unconventional Indexing of Arrays


The arrays in other programming languages also start with the number 1 to identify the first element in their index. Think of an array as a sequence of elements arranged one after another in a single column (or row). You can access each element in the array using its position in that column/row.

  

Unconventional Indexing of Arrays

Because Small Basic lets you choose any numbers you want for your indices, you could initialize four elements of an array like this:

score[-10]  = 80

score[0]     = 85

score[3.5]  = 90

score[100] = 95

       

Small Basic creates an array with just these four elements. The fact that the index values are between –10 and 100 doesn’t change the size of the array. If we try to access the undefined element at index 50 (via score[50]), Small Basic returns an empty string. But most programming languages only allow sequential index numbers. So most examples you find will follow the traditional view of arrays, where the elements are indexed sequentially and referenced by consecutive integers. They'll usually follow the convention of starting an array with index number 1. So you'd usually use score[1] to reference the first element of a score array, score[2] to reference the second element … and so on.

Sequential indexing allows you to use an integer loop counter (in a For loop) to easily cycle through all the elements of an array.

    

Do you have any questions? Ask us! We're full of answers and other fine things!

Head to the Small Basic forum to get the most answers to your questions: 

http://social.msdn.microsoft.com/Forums/en-US/smallbasic/threads/   

And go to http://blogs.msdn.com/SmallBasic to download Small Basic and learn all about it!

   

Small and Basically yours

- Ninja Ed & Majed Marji

Comments (1)

Skip to main content