15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

316<br />

Using an Array<br />

zero-based arrays, such as Visual Basic and REALbasic. One-based arrays are<br />

less common, but found in some versions of BASIC along with less popular<br />

languages like Pascal and Smalltalk.<br />

When defining arrays, always make sure you know whether your <strong>programming</strong><br />

language creates zero-based or one-based arrays. Otherwise, you may<br />

try to store data in non-existent array elements.<br />

Definable bounds<br />

To avoid confusion, some <strong>programming</strong> languages (such as Pascal) let you<br />

define both the lower and upper bound arrays.<br />

If you wanted to create an array to hold five integers, you could use the following<br />

code:<br />

Var<br />

LotteryNumbers[1..5] of Integer;<br />

This would number the LotteryNumbers array from 1 to 5. However, you<br />

could choose any number range of five like this:<br />

Var<br />

LotteryNumbers[33..37] of Integer;<br />

This would create an array of five elements, numbered from 33 to 37, as<br />

shown in Figure 1-4.<br />

Figure 1-4:<br />

Some<br />

<strong>programming</strong><br />

languages<br />

let you<br />

define the<br />

numbering<br />

of an array.<br />

Var<br />

LotteryNumbers[33..37] of Integer;<br />

33 34 35 36 37<br />

One advantage of defining the numbering of an array is that you can use<br />

meaningful numbers. For example, if you wanted to store the names of<br />

employees in an array, you could number the array so each array element is<br />

identified by an employee number. So if Jan Howards has employee ID<br />

number 102, Mike Edwards has employee ID number 103, and John

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!