CHAPTER 4
LOOPS 
CHAPTER GOALS
To implement while, for, and do loops
To hand-trace the execution of a program
To become familiar with common loop algorithms
To understand nested loops
To implement programs that read and process data sets
To use a computer for simulations
CHAPTER CONTENTS
Common Error 4.1: Don't Think "Are We There Yet?"
Common Error 4.2: Infinite Loops
Common Error 4.3: Off-by-One Errors
Random Fact 4.1: The First Bug
4.2 PROBLEM SOLVING: HAND-TRACING
Programming Tip 4.1: Use for Loops for Their Intended Purpose Only
Programming Tip 4.2: Choose Loop Bounds That Match Your Task
Programming Tip 4.3: Count Iterations
Programming Tip 4.4: Flowcharts for Loops
4.5 APPLICATION: PROCESSING SENTINEL VALUES
Special Topic 4.1: The Loop-and-a-Half Problem and the break Statement
Special Topic 4.2: Redirection of Input and Output
Video Example 4.1: Evaluating a Cell Phone Plan ![]()
4.6 PROBLEM SOLVING: STORYBOARDS
Worked Example 4.1: Credit Card Processing
Worked Example 4.2: Manipulating the Pixels in an Image
4.9 APPLICATION: RANDOM NUMBERS AND SIMULATIONS
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access