3: Loops
( \newcommand{\kernel}{\mathrm{null}\,}\)
The programs we have seen so far are straight-line code; that is, they execute one instruction after another from top to bottom. This chapter introduces one of the most important programming-language features, the for
loop, which allows simple programs to perform complex, repetitive tasks. This chapter also introduces the mathematical concepts of sequence and series, and a process for writing programs, incremental development.
- 3.1: Updating Variables
- The text discusses a problem in writing a program for a bike-share system with bikes moving between Boston and Cambridge. It highlights an error in updating bike counts when new values for one location are used before updating the other, violating the Principle of Conservation of Bikes. The solution involves using temporary variables to ensure simultaneous updates or an alternative method simplifying calculations while maintaining correct total bike counts.