PART I
C h a p t e r 5 :
P r o g r a m C o n t r o l S t a t e m e n t s
99
PART IPART I
this statement says “add to
sum
the value of
sum
plus
i
, then increment
i
.” Thus, it is the
same as this sequence of statements:
sum = sum + i;
i++;
Declaring Loop Control Variables Inside the for Loop
Often the variable that controls a
for
loop is needed only for the purposes of the loop and
is not used elsewhere. When this is the case, it is possible to declare the variable inside the
initialization portion of the
for
. For example, the following program computes both the
summation and the factorial of the numbers 1 through 5. It declares its loop control variable
Do'stlaringiz bilan baham: |