1. Exam Points
Iterations:
- REPEAT x TIMES
- REPEAT UNTIL(EXPRESSION)
Nested iterations- Use
natural languageto describe an algorithm with iterations.- Use of iterations:
Selectthe rightalgorithmfor a given problem.Predict result or outputof given code segments.Comparetwoiteration algorithms.Represent iterationsusingflow charts.
2. Knowledge Points
(1) Iteration
Iterationis arepeating portion of an algorithm.- Iteration repeats a specified number of times or until a given stopping condition is met.
Iteration constructs(构造/结构)repeat n times- Syntax
- Example
- Syntax
repeat until(exit condition)- Syntax
- Example
- Syntax
- In
REPEAT UNTIL(condition)iteration, aninfinite loopoccurs when the ending condition willnever evaluate to true.- Example:
- Example:
- In REPEAT UNTIL(condition) iteration, if the conditional evaluates to
true initially, the loop body isnot executed at all, due to the condition being checked before the loop. - An
iterationcan benestedwithin another, you can nest any of the following loop inside another loop.- repeat n times
- repeat until(condition)
- Example of
nested iteration:
- Note:
Remember to update the loop control variableto avoid infinite loops.