Chapter 9

Loops, Loops, Loops

IN THIS CHAPTER

check Understanding loops

check Exploring the for loop

check Creating nested for loops

check Working a while loop

check Using a do while loop

check 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.