When the number of iterations is pre-determined, such as counting to 10 we use:

Homepage

  • Skip to content
  • Accessibility Help

BBC Account

Notifications

  • Home
  • News
  • Sport
  • Weather
  • iPlayer
  • Sounds
  • Bitesize
  • CBeebies
  • CBBC
  • Food
  • Home
  • News
  • Sport
  • Reel
  • Worklife
  • Travel
  • Future
  • Culture
  • TV
  • Weather
  • Sounds

More menu

Search Bitesize Search Bitesize

  • Home
  • News
  • Sport
  • Weather
  • iPlayer
  • Sounds
  • Bitesize
  • CBeebies
  • CBBC
  • Food
  • Home
  • News
  • Sport
  • Reel
  • Worklife
  • Travel
  • Future
  • Culture
  • TV
  • Weather
  • Sounds
Close menu

BITESIZE

  • Home
  • Learn
  • Support
  • Careers
    • My Bitesize

KS3

Iteration in programming

When designing programs, there may be some instructions that need repeating. This is known as iteration, and is implemented in programming using FOR and WHILE statements.

Part of

Computer Science

Programming

  • Revise

  • quiz

    Test

  1. previous

  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6
  8. 7
  9. Page2 of7
  10. next

Count-controlled loops

There are two ways in which programs can iterate or ‘loop’:

  • count-controlled loops
  • condition-controlled loops

Each type of loop works in a slightly different way and produces different results.

Count-controlled loops

Sometimes it is necessary for steps to iterate a specific number of times.

Consider this simple algorithm for adding up five inputted numbers:

  1. set the total to 0
  2. repeat this section five times
    • input a number
    • add the number to the total
  3. go back to step 2
  4. say what the total is

This algorithm would allow five numbers to be inputted and would work out the total. Because it is known in advance how many times the algorithm needs to loop, a count-controlled loop is used.

curriculum-key-fact

A count-controlled loop is used when the number of iterations to occur is already known.

A count controlled loop continues a predefined number of times. At a race track, a car might be given the rules, loop 52 times, then stop.

A count-controlled loop is so called because it uses a counter to keep track of how many times the algorithm has iterated. The pseudocode for this algorithm might look like this:

total to 0 count to 1 FOR as long as count is in the range 1 to 5 INPUT user inputs a number STORE the user's input in the number variable total = total + number Add 1 to count OUTPUT "The total is " + total

Steps that are part of the loop are indented. Indentation is used to show which steps are to be iterated.

In this example, the variable ‘count’ is used to keep track of how many times the algorithm has iterated. This variable controls the loop. The algorithm will continue to iterate until the value of count has reached 5. As soon as count reaches 5, the algorithm stops iterating.

  1. previous

  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6
  8. 7
  9. Page2 of7
  10. next

KS3 Subjects

  1. Biology
  2. Chemistry
  3. Citizenship (Wales)
  4. Computer Science
  5. English
  6. French
  7. Geography
  8. German
  9. History
  10. Humanities - Geography (Wales)
  11. Humanities - History (Wales)
  12. ICT
  13. Maths
  14. Modern Foreign Languages
  15. Music
  16. Physics
  17. Religious Studies
  18. Science
  19. Spanish

Which loop is used when number of iterations are predetermined?

For-loops are typically used when the number of iterations is known before entering the loop. For-loops can be thought of as shorthands for while-loops which increment and test a loop variable.

When the number of iterations are known we use?

For loops are typically used when the number of iterations is known before entering the loop. For loops can be thought of as shorthands for while loops which increment and test a loop variable.

Which loop will be the best choice if you know that you need exactly 10 iterations from the loop?

The while loop is always the best choice in situations where the exact number of iterations is known.

What are the 3 types of iteration?

We will study three forms of iteration: tail-recursion, while loops, and for loops. We will use the task of reversing a list as an example to illustrate how different forms of iteration are related to each other and to recursion. A recursive implementation of reverse is given below.