Homepage
Accessibility links
- 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
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
quiz
Testprevious
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- Page2 of7
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:
- set the total to 0
- repeat this section five times
- input a number
- add the number to the total
- go back to step 2
- 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 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.
previous
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- Page2 of7
next
KS3 Subjects
- Biology
- Chemistry
- Citizenship [Wales]
- Computer Science
- English
- French
- Geography
- German
- History
- Humanities - Geography [Wales]
- Humanities - History [Wales]
- ICT
- Maths
- Modern Foreign Languages
- Music
- Physics
- Religious Studies
- Science
- Spanish