[<< wikibooks] A-level Computing/AQA/Paper 1/Fundamentals of programming/Iteration
An incredibly important part of computing is the idea of iteration, that is repeating the same thing again and again.  You probably use iteration every day. Take writing lines in a detention for example; you write some lines, check to see if you have met the line limit, and if you haven't you write some more lines, check if you have met the line limit and so on, until you do meet the line limit and then you can stop.

A further example might be in a computer game, where the speed of a car is increased all the time the accelerator pedal is pressed down until you hit its maximum speed.

== While Do ==

The while loop:
For example:

While not top speed Do
increase speed

== Do While Loop ==

Another type of while loop is a Do-While loop.  This is slightly different from the While loop in that you perform the task before you check that you have to perform the task again.  This means you perform the task whatever the circumstances of the check:

increase speed
While not top speed

Visual Basic handles this with some slight syntax differences

This is great for young students:

But we run into a problem when we have a 78 year old:

Be careful when you use each loop!

== For Loop ==

The most complicated tool you may meet is the for loop.  This is a glorified While loop and don't be put off by how complicated it looks.  It also tends to be one of the easiest ways to iterate in Visual Basic

For (speed = 0, not top speed, increase speed)

It is far easier to use in vb

For loops also allow you to count downwards.  For example if you creating a timer that counts down to a target.  To do this we use the step - 1 code, making the following code:


You have met the three main sorts of iteration that you can use, some being more suited for solving certain problems than others:

While Do
Do While
ForClick here to learn more

== Loopy Loops (Nested Loop) ==
Some times it might be a good idea to combine several loops together.  Take a look at this example 

This code uses a loop inside a loop to print out the times tables.  All that time you spent learning them at school can be rewritten in six lines of code!