15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using an Array 315<br />

the array, which defines how many chunks of data (elements) that the array<br />

can hold.<br />

Bounds<br />

The size of an array is defined by two numbers:<br />

✦ The lower bound defines the number of the first array element.<br />

✦ The upper bound defines the number of the last array element.<br />

Default bounds<br />

The default value of the lower bound depends on the <strong>programming</strong> language:<br />

✦ Many <strong>programming</strong> languages, including the curly bracket language<br />

family of C and Java, always define the lower bound of an array starting<br />

with the number 0 (known as zero-based arrays).<br />

✦ Other <strong>programming</strong> languages always define the lower bound of an<br />

array starting with the number 1 (known as one-based arrays).<br />

The following BASIC code actually creates a zero-based array that can hold<br />

six elements, numbered 0 through 5, as shown in Figure 1-3:<br />

Dim LotteryNumbers(5) as Integer<br />

Book III<br />

Chapter 1<br />

Figure 1-3:<br />

One-based<br />

array<br />

numbers<br />

array<br />

elements<br />

differently<br />

than zerobased<br />

arrays.<br />

Dim LotteryNumbers(5) as Integer<br />

0 1 2 3 4 5<br />

1 2 3 4 5<br />

A zero-based array starts<br />

counting at 0.<br />

A one-based array starts<br />

counting at 1.<br />

Structures<br />

and Arrays<br />

If the <strong>programming</strong> language created a one-based array, the array would<br />

hold only five elements.<br />

Zero-based arrays were made popular in the C language. As a result, any<br />

language derived from the C language, such as C++, C#, Java, Python, and<br />

Objective-C, will also use zero-based arrays. Because many programmers are<br />

familiar with zero-based arrays, many other <strong>programming</strong> languages also use

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

Saved successfully!

Ooh no, something went wrong!