5.4. Examples Using the for Statement

The following examples show methods of varying the control variable in a for statement. In each case, we write the appropriate for statement header. Note the change in the relational operator for loops that decrement the control variable.

a) Vary the control variable from 1 to 100 in increments of 1.

   for ( unsigned int i = 1; i <= 100; ++i )

b) Vary the control variable from 100 down to 0 in decrements of 1. Notice that we used type int for the control variable in this for header. The condition does not become false until control variable i contains -1, so the control variable must be able to store both positive and negative numbers.

   for ( int i = 100; i >= 0; --i )

c) Vary the control variable ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.