The Goals of this Module
■ Understand loops
■ Know how to create and resize arrays
■ Use loops to initialize and access arrays
Loops are used to repeat the execution of code statements. This is useful in a variety of contexts. For example, if an application user enters invalid data, you may want to ask the user whether they want to retry or quit. If they retry and still enter invalid data, you again would ask the user whether they want to retry or quit. This process keeps repeating until the user either enters valid data or quits.
An array permits you to use a single variable to store many values. The values are stored at consecutive indexes, starting with zero and then incrementing by one for each additional element of the array. For example, to store sales for each day of the week, you can create one array with seven elements, rather than declaring seven separate variables. Not only is it easier to keep track of one variable than seven, but you can also use a loop to access each consecutive element in an array, whether to assign a value of that element or to display that value.
Was this article helpful?