Chapter 9
Loops, Loops, Loops
IN THIS CHAPTER
Understanding loops
Exploring the for loop
Creating nested for loops
Working a while loop
Using a do while loop
Avoiding the endless loop
Programs love to do things over and over, mirthfully so. They never complain, they never tire. In fact, they’ll repeat things forever unless you properly code instructions for when to stop. Indeed, the loop is a basic programming concept. Do it well. Do it well. Do it well.
A Little Déjà Vu
A loop is a section of code that repeats. How often? That depends on how you write the loop. As an overview, a loop involves three things:
- Initialization
- One or more statements that repeat
- An exit
The initialization sets up the loop, usually specifying a condition upon which the loop begins or is activated. For example, “Start the counter at 1.”
The statements that repeat are contained as a block in curly brackets. ...
Get C Programming For Dummies, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.