Understanding repetition
Very often, we need to perform a series of statements repeatedly. We might want to perform a calculation on each member of a set of values, or we might want to perform a calculation using all of the members in a set of values. Given a collection of values, we also might want to iterate over the whole collection to find the desired value, to count all the values, to perform some kind of calculation on them, or to manipulate the set in some way – say, to sort it.
There are a number of ways to do this. The simplest, yet most restrictive way is the brute-force method. This can be done regardless of the language being used. A more dynamic and flexible method is to iterate or repeatedly loop. C provides three interrelated looping statements – while()…
, for()…
, and do … while()
. Each of them has a control or continuation expression, and a loop body. The most general form of these is the while()…
loop. Lastly, there is...