Chapter Four · Many Values, One Name
Lists and Loops
Programs rarely deal with one value at a time. This chapter introduces the list, which holds many values in order under one name, and the loop, which does the same work for every item without writing it out again. It ends with a month of expenses and the questions you would ask of it.
A shopping list, a week of temperatures, a month of card payments: most real information comes in many pieces. Writing one name per piece stops working at about the third piece. This chapter gives you the two tools that make many values manageable, and together they do most of the work in most programs.
The first two topics are about lists: making them, reaching into them by position, and changing them. The next two are about loops: for, which walks through every item in turn, and while, which repeats until something changes and so can, by mistake, repeat forever. The last topic brings them together on a list of real amounts, with the built-in tools that answer the usual questions in one line.
Topics in This Chapter
range, and the running total.break, and stopping a loop that never ends.