19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

162 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

into a short loop. A shorth<strong>and</strong> notation for the many related variables would be welcome. It<br />

would be nice if we could just write<br />

For i = 1 To 30<br />

Input #1, studenti, scorei<br />

Next i<br />

Of course, this will not work. <strong>Visual</strong> <strong>Basic</strong> will treat studenti <strong>and</strong> scorei as two variables <strong>and</strong><br />

keep reassigning new values to them. At the end of the loop they will have the values of the<br />

thirtieth student.<br />

<strong>Visual</strong> <strong>Basic</strong> provides a data structure called an array that lets us do what we tried to<br />

accomplish in the loop. The variable names will be similar to those in the Input # statement.<br />

They will be<br />

student(1), student(2), student(3), ..., student(30)<br />

<strong>and</strong><br />

score(1), score(2), score(3), ..., score(30).<br />

We refer to these collections of variables as the array variables student( ) <strong>and</strong> score( ). The numbers<br />

inside the parentheses of the individual variables are called subscripts, <strong>and</strong> each individual<br />

variable is called a subscripted variable or element. For instance, student(3) is the third subscripted<br />

variable of the array student( ), <strong>and</strong> score( ) is the 20th subscripted variable of the array<br />

score( ). The elements of an array are assigned successive memory locations. Figure 6-1 shows<br />

the memory locations for the array score( ).<br />

FIGURE 6-1 The Array Score( )<br />

Array variables have the same kinds of names as simple variables. If arrayName is the<br />

name of an array variable <strong>and</strong> n is a whole number, then the statement<br />

Dim arrayName(1 To n) As varType<br />

placed in the (Declarations) section of (General) reserves space in memory to hold the values<br />

of the subscripted variables arrayName(1), arrayName(2), arrayName(3), . . . , arrayName(n).<br />

(Recall from Section 3.1 that the (Declarations) section of (General) is accessed from any<br />

code window by selecting these values in the Object <strong>and</strong> Procedure boxes.) The spread of the<br />

subscripts specified by the Dim statement is called the range of the array, <strong>and</strong> the Dim statement<br />

is said to dimension the array. The subscripted variables will all have the same data type;<br />

namely, the type specified by varType. For instance, they could be all String variables or all<br />

Integer variables. In particular, the statements<br />

Dim student(1 To 30) As String<br />

Dim score(1 To 30) As Integer<br />

dimension the arrays needed for the preceding program.<br />

As with any variable created in the (Declarations) section of (General), these array variables<br />

are form-level as discussed in Section 3. Recall that form-level variables can be<br />

accessed from any procedure in the program <strong>and</strong> continue to exist <strong>and</strong> retain their values as<br />

long as the program is running.<br />

Values can be assigned to subscripted variables with assignment statements <strong>and</strong> displayed<br />

with Print methods. The statement<br />

Dim score(1 To 30) As Integer

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

Saved successfully!

Ooh no, something went wrong!