Looping Topic 4.3.7 / Core
Key points
  • the various ways of repeating code;
  • declaring variables locally;
  • trace tables.
  • iteration;
  • stopping condition;
  • infinite loop;
  • initialisation;
  • counter;
  • increment;
  • decrement;
  • trace table;
  • sentinel value.
    Related topics


    Looping can be tedious:

    1. Work through the presentation on Looping.
    2. Study this example of how to display times tables.
    3. Make the computer display:
      - the even numbers between 0 and 40
      - a countdown in fives from 75 to 0
      - all the multiples of 3 between 27 and 69 inclusive.
    4. A program which totals up and averages a set of pupil test scores could have the teacher enter the scores until s/he enters a negative value to mark the end of the input. As it proceeds, it needs to add the score to a total variable and 1 to a count variable, and divide total by count as soon as this so-called sentinel value arrives. Decide between a while loop and a for loop to program such a project. The project Average may help you but don't look at it until you have had a go.
    5. A program is needed to calculate the average weekly temperature at a holiday resort given the 7 temperatures for each day of the week.
    6. Construct a kilograms to pounds conversion table from 1 to 30 kg at 2 kg intervals (1 kg = 2.2 lbs).
    7. A child is learning the alphabet. Design and write a program to accept letters input in alphabetical order until a wrong key is pressed. Present the user with a message saying how many letters they got correct out of 26 and as a percentage. Make it child-friendly. Hint: for (char letter = 'a'; letter <= 'z'; letter ++) { is good syntax.
    8. A factory is turning out widgets at the rate of 1200 per month. Production is planned to increase at 10% each month until production has reached or exceeded 1750 per month. How many months will this take? Produce a suitably user-friendly program.
    9. A summary sheet of the work so far in Java.
    10. An exercise involving trace tables.
    11. Write a trace table for one of the loops you wrote in task 3.