A-level Computing 2009/AQA/Problem Solving, Programming, Data Representation and Practical Exercise/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.

1. I should always do my programming homework.

2. I should always do my programming homework.

3. I should always do my programming homework.

4. I should always do my programming homework.

... 100. I should always do my programming homework.

A computer game example would be increasing the speed of a car while the accelerator is pressed down and until you hit its top speed.

0

1

2

3

...

120

MAXSPEED!

Write a program that counts from 20 to 60 inclusive like so:

20 21 22 ... 60

Write a program that takes an input and outputs the times table for that number:

insert a number: 7

1 * 7 = 7

2 * 7 = 14

3 * 7 = 21

4 * 7 = 28

5 * 7 = 35

6 * 7 = 42

7 * 7 = 49

8 * 7 = 56

9 * 7 = 63

10 * 7 = 70

Write a program that adds all the numbers from 10 to 20 inclusive together and finally outputs the result

While Do
The while loop: For example: While not top speed Do increase speed End

0 1 ... 118 119

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: Do increase speed While not top speed End

Visual Basic handles this with some slight syntax differences This is great for young students:

how old are you? 15 15 year olds should attend school! 16 year olds should attend school! 17 is too old to attend school!

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

78 year olds should attend school! 78 is too old to attend school!

For the dodgy example above re-write the Do While as a While Do loop

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) drive 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  code, making the following code: display:

10,9,8,7,6,5,4,3,2,1,Test over!

Write a for loop to display the words "I will eat my greens" 40 times:

Write code that will input a lower and higher number, then write the numbers on the screen, starting at the lower and writing each number until you reach the higher. Use a for loop, it should display the following:

insert lower number: 10

insert higher number: 13

10

11

12

13

Write a for loop that will output the frequencies: 100,200,300,400, ..., 20000. HINT, you might want to start at 1 and multiply. Remember

Get the computer to keep asking a user whether they are "Ready to launch?". If they say anything other than "Yes", then keep asking the question. If they say yes, then count down from 5 and end with the words "BLAST OFF!".

Ready to launch? No

Ready to launch? Not yet

Ready to launch? Yes

5

4

3

2

1

BLAST OFF!

Extension: If you want to really show that you know how to use case statements, get it to say: FIVE, FOUR, THREE, TWO, ONE instead of showing the numbers

You have met the three main sorts of iteration that you can use, some being more suited for solving certain problems than others: Click here to learn more
 * While Do
 * Do While
 * For

Loopy Loops
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!

1 : 1 * 0 = 0 1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 1 * 4 = 4 1 * 5 = 5 1 * 6 = 6 1 * 7 = 7 1 * 8 = 8 1 * 9 = 9 1 * 10 = 10 2 : 2 * 0 = 0 ...

Write a for loop inside a loop to display a Christmas Tree, like so:

\

\\

\\\

\\\\

\\\\\

Adjust the code above so that the user inputs how high the tree is