Count-Controlled Repetition

Count-controlled repetition requires

  • control variable (or loop counter)
  • initial value of the control variable
  • increment (or decrement) by which the control variable is modified each iteration through the loop
  • condition that tests for the final value of the control variable

A count-controlled repetition will exit after running a certain number of times. The count is kept in a variable called an index or counter. When the index reaches a certain value (the loop bound) the loop will end.

Count-controlled repetition is often called definite repetition because the number of repetitions is known before the loop begins executing. When we do not know in advance the number of times we want to execute a statement, we cannot use count-controlled repetition. In such an instance, we would use sentinel-controlled repetition.

Display the character and its' ASCII value for all lower case characters.

for (char x = 'a'; x <= 'z'; x++)
   System.out.println(x + " = " + (int) x);

Fook Yuen Sin - Team 1