# 4 Loops and repetition

Learning Objectives

• Identify unnecessary repetition in code
• Understand the concept of iteration, or looping.
• Write simple programs using loops
• Recognise the difference between two types of loop – If and while.

Learning Outcomes

All must appreciate the need for a system to repeat code without re-typing.  Create a simple counting program. (Level 5)

Most should complete the above, adapt the program to create a countdown.  Write code which successfully uses both types of loop. (Level 5)

Some could complete the above, and without any help, use the concept of iteration to solve simple mathematical problems. (Level 6)

Keywords

Words to learn: Loop,  iteration, for,  while, repetition

Starter

Look at this scratch demonstration.  The idea of this exercise is to make the mouse reach the cheese in the shortest possible number of moves.

Start off using only the buttons at the bottom of the screen.  How many moves did you need?

Now try again,  clicking a multiplier (x2, x3, x4) before a movement.  How many moves are needed now?
Main

You’ll have realised that grouping repeated moves together drastically reduces the number of moves needed for the mouse.  The same is true for all programming.  We need to avoid unnecessary repetition at all costs.  Imagine you need to write a program to print out the 6 times table.  You would start something like this:- See how far you can get with this program in 5 minutes.

Have you got further than 12 x 6?  If so, you’ll see the program is getting very long, and quite difficult to read.  You’ll also see that it is difficult to change the program.  What if we wanted the 10 times table instead?  You’d have a lot to edit,  and could very easily make mistakes in a more complicated program.

For this reason, we need some way to repeat code.  We use a loop which iterates through code, repeating it as many times as necessary.  Try this: First, we create two variables . One for the times table we wish to print out, and the next to count through 1,2,3,4 etc.

Then we make our loop, using “while”.  There is then a condition, to make the loop stop (<= means less than or equal to), so the loop will continue running as long as the value of the variable “count” is less than 10.

The next line prints the values of the variables, and multiplies them to create the answer.

Finally, we add one to the variable “count”.  Otherwise, the loop would keep running forever, as count would never reach 10, and stop.

Challenges:

• Amend the code so it prints out the 9 times table.
• Now make the program print the first 100 values in the 12 times tableExtensionMake a program that counts from 10 to 0 (clue – look at the line  count = count +1)

Really we usually use while loops when we don’t know how many times the loop must iterate.  Try this code.  You’ll see it can run any number of times: Challenge:

• Write comments on this code explaining how it works.
• Amend the code so it prints a message when the correct password has been entered.

For loops.

If we know how many times a loop should iterate (like our first example), we use a different type of loop.

Run this program.  it will iterate as many times as specified in the “range” (here from 1 to 10) do you notice something strange?  It doesn’t iterate for the number 10. The second number in range() is not included.

Also note that the variable count is created inside the for loop.

Again, make sure you comment this code and print it out for your book.

Challenge:

• Write a guessing game.  The computer stores a number, then asks the user to guess it. This will repeat until the user successfully guesses the number, when the computer prints a congratulatory message.Extension (Very difficult!)
• Make a program print ALL the times tables (from one to ten).  You aren’t allowed more than 12 lines of code.  (Clue:  You will need two loops:- one for each individual  multiplication table, and an outer one for all the multiplication tables.  We call this a nested loop.