Java Reference
In-Depth Information
The following examples show techniques for varying the control variable in a
for
state-
ment. In each case, we write
only
the appropriate
for
header. Note the change in the rela-
tional operator for the loops that
decrement
the control variable.
a)
Vary the control variable from
1
to
100
in increments of
1
.
for
(
int
i =
1
; i <=
100
; i++)
b)
Vary the control variable from
100
to
1
in
decrements
of
1
.
for
(
int
i =
100
; i >=
1
; i--)
c)
Vary the control variable from
7
to
77
in increments of
7
.
for
(
int
i =
7
; i <=
77
; i +=
7
)
d)
Vary the control variable from
20
to
2
in
decrements
of
2
.
for
(
int
i =
20
; i >=
2
; i -=
2
)
e)
Vary the control variable over the values
2
,
5
,
8
,
11
,
14
,
17
,
20
.
for
(
int
i =
2
; i <=
20
; i +=
3
)
f)
Vary the control variable over the values
99
,
88
,
77
,
66
,
55
,
44
,
33
,
22
,
11
,
0
.
for
(
int
i =
99
; i >=
0
; i -=
11
)
Common Programming Error 5.5
Using an incorrect relational operator in the loop-continuation condition of a loop that
counts downward (e.g., using
i
<=
1
instead of
i
>=
1
in a loop counting down to 1) is
usually a logic error.
Common Programming Error 5.6
Do not use equality operators (
!=
or
==
) in a loop-continuation condition if the loop's control
variable increments or decrements by more than 1. For example, consider the
for
statement
header
for (int counter = 1; counter != 10; counter += 2)
. The loop-continuation test
counter != 10
never becomes false (resulting in an
infinite loop
) because
counter
incre-
ments by 2 after each iteration.
Application: Summing the Even Integers from 2 to 20
We now consider two sample applications that demonstrate simple uses of
for
. The ap-
plication in Fig. 5.5 uses a
for
statement to sum the even integers from 2 to 20 and store
the result in an
int
variable called
total
.
1
// Fig. 5.5: Sum.java
2
// Summing integers with the for statement.
3
4
public class
Sum
5
{
Fig. 5.5
|
Summing integers with the
for
statement. (Part 1 of 2.)