The repetition structure, or loop, processes one or more instructions repeatedly. The repeatable
instructions in a loop are called the loop body. When a loop’s condition evaluates to true, the
instructions in the loop body are processed. Otherwise, the instructions are skipped and
processing
continues with the first instruction after the loop. After each processing of the loop body
(iteration), the loop’s condition is reevaluated to determine if the instructions should be
processed again.
It contains four elements:
Repetition statement – a body of the loop
Condition – a boolean condition that controls whether the instructions are repeated.
Initial value – starting value for the condition
Loop termination - there must be a statement within the repeating section of code that
allows
the condition to become false. This is necessary to ensure that, at some point, the
repetition
stops.
A repetition structure can be pretest or posttest:
A pretest loop is an entrance-controlled loop where the condition is
evaluated before the
instructions in the loop are processed. If the condition is false, the statements in the
loop
body will never be executed. Examples of pretest loops are while and for
loop.
A posttest loop is an exit-controlled loop where the condition is
evaluated
after the
instructions in the loop are processed. In this loop, the instructions will always be
processed
at least once. An example of a posttest loop is a do-while loop.
Some loops require the user to enter a special sentinel value to end the loop.
Sentinel values should
be easily distinguishable from the valid data recognized by the program.
A loop whose instructions are processed indefinitely is called an infinite loop or
endless
loop .
while loop
while statement is used to create a while loop.
Syntax:
while (condition)
statement;
condition can contain constants, variables, functions, arithmetic operators,
comparison operators, and logical operators.
statement(s) are processed repeatedly as long as the condition is true.
If there is more than one statement in the loop body then it must be entered as a statement
block (enclosed in braces). There can be braces even if there is only one statement in the
statement block however it is not mandatory.
Example: Diplaying numbers from 1 to 10
for loop
A for loop is a counter-controlled loop which uses the for statement to code
any
pretest loop.
Syntax:
for ([initialization]; condition; [update])
statement;
initialization and update arguments are optional.
initialization argument usually creates and initializes a counter variable.
condition argument specifies the condition that must be true for the loop body to
be
processed.
It may contain variables, constants, functions, arithmetic operators, comparison operators,
and
logical operators.
update argument usually contains an expression that updates the counter variable
Loop
ends when
the condition evaluates to false
If there is more than one statement in the loop body then it must be entered as a statement
block (enclosed in braces). There can be braces even if there is only one statement in the
statement block however it is not mandatory.
Example: displaying numbers from 1 to 10
do-while loop
In this loop, the condition is tested at the end of the loop therefore all the statements in the
body
of this loop are executed at least once. A do-while statement is used to code posttest
loops.
Syntax:
do {
//one or more statements to be processed one time,
and thereafter as long as the condition is true
} while (condition);
condition argument specifies condition that must be true for the loop body to be
processed. It may contain variables, constants, functions, arithmetic operators, comparison
operators, and logical operators.
Braces are required around statements if there are more than one.
Example
Counters and Accumulators
A counter can be used to control the loop instead of using a sentinel value. A
counter is a numeric
variable used for counting something. An accumulator is a numeric variable used
for
accumulating
(adding together) multiple values. It is used to perform calculations such as total or average.
Two tasks are associated with counters and accumulators:
Initializing means assigning a beginning value to a counter or accumulator
(usually 0) –
happens once, before the loop is processed
Updating (or incrementing) means adding a number to the value of a counter
or
accumulator. A
counter is updated by a constant value (usually 1). An accumulator is updated by a value
that
varies. Update statements are placed in the body of a loop since they must be performed at
each
iteration.
break statement
A break statement forces an immediate break or exit from switch, while, for, and do-while
statements.
It is useful for breaking out of loops when an unusual condition is detected
Example: This example stops the loop when i is equal to 4
continue statement
A continue statement applies to while, do-while, and for statements; causes the next
iteration of the
loop to begin immediately. It is useful for skipping over data that should not be processed in
this
iteration while staying within the loop.
Example: This example skips the value of 4
Nested loops
Like selection structures, repetition structures can be nested. You can place one loop (the
inner, or
nested loop) inside another loop (the outer loop). Both loops can be pretest loops or posttest
loops, or the two loops may be different types. The programmer decides whether a problem
requires a
nested loop by analyzing the problem specification.
Example: In this example, we are using two for loops to draw a pattern of *.