Indefinite Loops and Counting Loops

Some of the while loop examples have been indefinite loops. That means, you don't know in advance how many times the loop will be executed before the expression becomes false. For example, when Listing 6.1 used an interactive loop to sum integers, you didn't know beforehand how many integers would be entered. Other examples, however, have been counting loops. They execute a predetermined number of repetitions. Listing 6.10 is a short example of a while counting loop.

Listing 6.10. The sweetie1.c Program
 // sweetie1.c -- a counting loop #include <stdio.h> int main(void) { const int NUMBER = 22; int count = 1; // initialization while (count <= NUMBER) // test { printf("Be my Valentine!\n"); // action count++; ...

Get C Primer Plus, Fourth 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.