September 15, 2024, 09:12:19 PM

## News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........

## Basic Clarity 3 - Initialising Arrays

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

0 Members and 1 Guest are viewing this topic.

#### GWS

##### June 13, 2012, 09:56:36 AM
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:

`openconsolecls' Basic Clarity 3 ..' Initialising Arrays .autodefine "off"def i,a[4] as int def s[4] as stringa[0] = 0,2,4,6s[0] = "","dog","fish","bird"for i = 0 to 3: print "Array a[ ",i,"] is ",a[i]: next iprintfor i = 0 to 3: print "Array s[ ",i,"] is ",s[i]: next ido:until inkey\$<>""closeconsoleend`

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 ..