Next: Common errors in writing
Up: Unit 06
Previous: Example of while loop:
initialization
while (condition) {
operation
next step
}
- initialization: sets the variables used in the loop before
starting the execution of the loop (before the loop statement)
E.g., product = 0;
- condition: expression evaluated at the beginning of each
iteration, whose truth value determines whether the body of the loop is
executed or whether the loop finishes
E.g., (multiplicator > 0)
- operation of the loop: computation of the partial result at each
loop iteration (in the body of the loop)
E.g., product = product + multiplicand;
- next step: increment/decrement operation for the variable that
controls the repetitions of the loop (in the body of the loop)
E.g., multiplicator--;
Once we have designed the loop, we have to check its proper
termination. In particular, we have to check that the execution of
the loop statements can modify the value of the condition in such a way that it
becomes false.
Example: The statement multiplicator--;
can cause the condition
(multiplicator > 0) to become false, provided multiplicator
is a positive integer number.
Next: Common errors in writing
Up: Unit 06
Previous: Example of while loop: