Loops are used to repeat the execution of code statements. The For...Next statement is used to repeat code execution a specified number of times. By contrast, the While...End While and Do statements are used to repeat code execution an indefinite number of times. Additionally, the Do statement can test the condition at the end of the loop instead of at the beginning.
An array permits you to use a single variable to store many values. The values are stored at consecutve indexes, starting with zero and then incrementing by one for each additional element of the array. You can use the ReDim statement to resize an array, and the Preserve keyword to retain the values of the original array.
The array is an object, and has useful properties and methods. For example, the Length property returns the number of elements in the array and the GetUpperBound method returns the upper bound of an array.
Loops and arrays work well together. You can use a loop to access each consecutive element in an array, whether to assign a value of that element or to display that value. The For Each..Next statement is designed to work with arrays, and does not require code concerning the number of elements in the array.
Was this article helpful?