Student Name
Chapter 6 Notes for the Turing textbook Repetition
Counted Loops
Run Turing and try the sample code on page 103.
List the 3 types of loops:
infinite loop
,
conditional loop
, and
counted loop
.A counted loop is
a loop that repeats a fixed number of times
.The keyword that starts a counted loop is
for
, which is immediately followed by thename of the
index
(also known as the
counter
) for the loop.The index of a counted loop is a variable of type
int
with the following special properties:Declaration —
Cannot be declared. Its declaration is implied by itsappearance in a for statement
Range — The range of the index of a counted loop is given as follows:
thestarting value of the index, followed by two dots, followed by the finishingvalue of the index
.Scope —
The value of the index may be used in arithmetic statements, or itmay be output. But the index may not be used outside the loop, that is, theindex is only available within the scope of the loop.Read the sample code on page 104.
Rather than using a number in a
for
loop, it is better to declare and use a
constant
. Thisis better because
if you want to modify the program, you only have to change theconstant
.
Read the sample code on page 105.
Counting in Multiples
Read the sample code on page 105 in Section 6.3.1.
The
by
clause is used when you want to increase the index by a number greater than
one
.An example of a loop that outputs even numbers from 2 to 20 using the
by
clause is:
Leave a Comment