Variables and values can have multiple roles, but it is useful to
mention three different roles in the context of loops:
Counter
: Variable that is incremented every time a given event occurs.
Sentinel Value
: A special value that signals that the loop needs to end.
Accumulator
: Variable used to keep the total of several values.
We can have an accumulator and a sentinel value at the same time:
You can have counter, accumulator and sentinel values at the same time:
We can distinguish between three “flavors” of loops (that are not
mutually exclusive):
Sentinel controlled loop
: The exit condition tests if a variable has (or is different from) a
specific value.
User controlled loop
: The number of iterations depends on the user.
Count controlled loop
: The number of iterations depends on a counter.
Note that a user-controlled loop can be sentinel-controlled (that is the
example we just saw), but also count-controlled (“Give me a value, and I
will iterate a task that many times”).