C.16 Examples Using the for
Statement
The following examples show techniques for varying the control variable in a for
statement. In each case, we write the appropriate for
header. Note the change in the relational operator for loops that decrement the control variable to count downward.
Vary the control variable from
1
to100
in increments of1
.for ( int i = 1; i <= 100; ++i )
Vary the control variable from
100
to1
in decrements of1
.for ( int i = 100; i >= 1; --i )
Vary the control variable from
7
to77
in increments of7
.for ( int i = 7; i <= 77; i += 7 )
Vary the control variable from
20
to2
in decrements of2
.for ( int i = 20; i >= 2; i -= 2 )
Vary the control variable over the values
2
,5
,8
,11
,14
,17
,20
.for ( int
Get Android How to Program, 3/e 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.