type | description |
---|---|
counting loop | Uses a loop control variable to count upwards or downwards (usually by an integer increment.) |
sentinel-controlled loop | Loop keeps going until a special value is encountered in the data. |
result-controlled loop | Loop keeps going until a test determines that the desired result has been achieved. |
Each type of loop can be built using the
while
and other statements.
The loops are built out of several fundamental statements because there
are three things
(in each of the three types of loops)
that must be done correctly:
Overlooking one of these aspects results in a defective loop and a sometimes difficult to find program bug! Usually each of these three considerations is located in a different spot. No wonder that loops often go wrong!
The flow chart to the right shows what all loops must do.
It could be for any of the three types of loops.
The ellipses
Would it be convenient to have all three aspects combined into one statement?