September 26, 2020, 06:41:23 pm

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Basic Clarity 3 - Initialising Arrays

Started by GWS, June 13, 2012, 09:56:36 am

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

GWS

Hi,

When you have a set of values of a particular type, you will often want to keep them in an Array.

They are then much easier to access and update as the program runs.

Basic has a neat way of doing this:

Code Select

openconsole
cls

' Basic Clarity 3 ..
' Initialising Arrays .

autodefine "off"

def i,a[4] as int
def s[4] as string

a[0] = 0,2,4,6

s[0] = "","dog","fish","bird"


for i = 0 to 3: print "Array a[ ",i,"] is ",a[i]: next i
print
for i = 0 to 3: print "Array s[ ",i,"] is ",s[i]: next i

do:until inkey$<>""
closeconsole
end



Other languages often require you to set each element separately as in:

a[0] = 0
a[1] = 2
a[2] = 4
a[3] = 6

If you only have a few values, the separate statement method is no problem, but if you have maybe 50 or more, it can become a bit of a chore.

That is when the list method is useful and neater.

You don't even have to fully define the starting element as a[0] - you can just use a = 0,2,4,6.

But for consistency, I prefer to specifically define the loading start point as a[0] in this case.

The reason being, you may want to start loading another array from some other array element, as in:

b[10] = 32,33,34,35,36 which will load elements 10 to 14 of array b[ ].

The string array example is very useful if you have a lot of descriptions to store.

The handling of arrays in Basic is neat and provides a great tool in many programs.

best wishes, :)

Graham

Tomorrow may be too late ..