Chapter 6. Lists and loops
Chapter 5 showed you how apps can remember things. In this chapter, we’ll build on that idea to make apps that remember and use collections of things using a special type of variable: the list. You’ll also find out how to make apps that repeat an action several times using loops.
Lists are all around you—especially in the technology you use. If you think about it, we bet you can think of at least five lists you’ve used today. Here are some examples:
- Choosing a person to send a message to from your contacts list
- Choosing a ring tone from a list of sound files
- Reading a list of updates from your friends on social media
- Choosing a TV program to record from a TV planner list
- Selecting a way to pay from a list of options when internet shopping
These are all visible lists, where you choose one or more items. Computers also use invisible lists like these to track activities:
- Your internet history —A list of which pages you’ve visited.
- Actions you’ve taken in a program —They’re saved in a list so they can be undone using the Undo button.
- Game saves —When you quit a game, your PC or console saves a list of parameters like your score, your current location or level, which objectives you’ve achieved, and so on.
In the first part of this chapter, you’ll learn how to create visible lists to help users choose items. Then we’ll move on to invisible lists, which are a flexible way to store lots of data items without having to create a bunch of separate variables.