A repetition structure is a control structure that allows a statement or sequence of statements to be repeated.
All loop structures involve three elementsan initializer, a loop-entry condition or a loopboundary condition, and an updater.
When designing a loop, it is important to analyze the loop structure to make sure that the loop bound will eventually be satisfied.
The for statement has the following syntax:
for ( initializer; loop-entry condition; updater ) loop body
The while statement takes the following form:
while ( loop-entrycondition ) loop body
The do-while statement has the following general form:
doloop body while ( loop-entrycondition );
When designing a loop, it is important to analyze the loop structure to make sure that the loop bound will eventually be satisfied. Table 6.2 summarizes the types of loop bounds that we have identified.
Table 6.2. Summary of loop bounds
(This item is displayed on page 291 in the print version)
Bound
Example
Counting
k < 100
Sentinel
input != 9999
Flag
done != true
Limit
amount < 0.5
Structured programming is the practice of writing programs that are built up from a small set of predefined control structuresthe sequence, selection, repetition, and method-call structures. An important feature of these structures is that each has a single entry and exit.
[Page 291]
A pre-condition is a condition that must be true before a certain code segment executes. A postcondition is a condition that must be true when a certain code segment is finished. Pre-conditions and postconditions should be used in the design, coding, documentation, and debugging of algorithms and methods.