• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Loops

Even though accessing individual items in arrays is quite common, you’ll also have the need to access each item in sequence. You already saw how to create an empty array or populate one. Later in this chapter, you’ll learn all the methods available to add to, or otherwise change, arrays. If you populate your array by hand, going through each item is not much of a challenge because you’ll know how many items are present. However, you might often add to arrays and never know precisely which items are present. In such cases, it’s easiest to use a loop to go through each item. Even when you know how many items are in your array, a loop is often more efficient than writing a separate line of code to access each item individually.

Suppose, for example, that you have an array called vowels that is initialized as follows:


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint