Counting Up Again

Line: Loopy Line
Easy Iteration Learning Outcome One Exercise
Type: Exercise
You should have completed: Driving Test This topic leads to: Counting Up Once More

Summary

This exercise revisits an earlier example of data processing (in this case adding up) and makes the code more efficient by using iteration structures.

Task

  1. Load your Counting Up application
  2. Identify the pattern in your code that is used over and over again
  3. Create a for loop that runs 5 times
  4. Put one copy of the code identified in (2) into it and delete the others
  5. Run the program and check that it still adds up correctly

Questions

  1. How would you write a for loop to count down?
  2. How would you modify the code to let the user decide how many times to count up?

Using a for loop executes a predictable number of times and is the ideal solution when we know or can calculate how many times to repeat a task.