Count-controlled iteration
iterationThe repetition of a block of statements within a computer program. is the final programming construct. There are times when a programSequences of instructions for a computer. needs to repeat certain steps until told otherwise, or until a conditionIn computing, this is a statement that is either true or false. A computation depends on whether a condition equates to true or false. has been met. This process is known as iteration.
An explanation of iteration, as used in algorithms and programming
Iteration is also often referred to as loopingRepeatedly executing a section of code., since the program ‘loops’ back to an earlier line of code.
Sections of code that are iterated are called loopThe repetition of an activity within a computer program.. Iteration enables programmers to greatly simplify a program. Instead of writing out the same lines of code again and again, a programmer can write a section of code once and ask the program to executionThe process of a program being run on a computer. it again and again until no longer needed.
This program would print a message out six times:
print “Coding is cool” print “Coding is cool” print “Coding is cool” print “Coding is cool” print “Coding is cool” print “Coding is cool”count-controlled iterationA loop that repeats a set number of times, usually implemented using a FOR loop. A loop counter is used to keep track of how many times the loop has iterated. repeatedly executes a section of code a fixed number of predetermined times. It uses the statementThe smallest element of a programming language which expresses an action to be carried out.for and next to determine what code is repeatedly executed and how many times. This program would also print out a message six times:
for count = 1 to 6 print “Coding is cool” next countThe first line of the program determines how many times the code is to be iterated. It uses a variableA memory location within a computer program where values are stored., in this case count, known as the condition variable, to keep track of how many times the code has been repeated so far. The variable is given a starting value (in this case 1) and an end value (in this case 6).
Every time the code is iterated, the value of count increases by one. At the end of the iteration, the value of count is tested to see if it is greater than the end value. If the result is FALSE, the code loops back to the start of the iteration and runs again. If it is TRUE, the iteration ends and the program continues with the next line of code.
The condition variable used to initialise a for next loop can be used within the loop itself. This program uses a loop’s condition variable to print the 10 times table:
for count = 1 to 10 print count * 10 next countAs can be seen above, using iteration makes a program simpler, less error-prone and more flexible. This is because:
- there are fewer lines of code, meaning fewer opportunities for typing errors to creep in
- to increase or decrease the number of iterations, all the programmer has to do is change the loop's end value