That Blue Square Thing

Computer Science GCSE

For loops - looping a set number of times

For loops allow you to repeat a section of program code. They are one of the key ways in which repetition can be coded (the other being while loops).

For loops are used when you know in advance how many times you want to repeat the code. For example:

For loops are a bit simpler than while loops, but can still be exactly what you need to be able to do at any time.

A simple for loop

A simple for loop can be used to display a count from 1 to 10. This is super simple, but shows the sort of way in which a for loop works. Here's some code:

# code to display numbers counting from 1 to 10
for i in range(0, 10):
print(i+1)

print("End")

The key thing here is to understand what i does.

i is just a counter. I could call it anything I like, but computer scientists generally use i - it's the sort of thing you would understand in a piece of code. The range(0,10) section starts i out at 0. Every time the loop code executes, i increases by one - so the first time in the loop i = 0, the second time i = 1 etc...

This continues until i = 10, at which point the loop stops and the code jumps to whatever is below the loop (in this case the line which prints the word End. It runs 10 times (from 0 to 9) - the 10 is the number it will stop at. By starting from 0 (which is the normal place to start from) we can get the code to print each number from 1 to 10 by using print(i+1).

It is possible to use for loops in other ways. You can jump 2, 5 or any number you like each time you go through the loop - it doesn't have to go up by 1 each time. You can even go backwards.

User Controlled For Lists

Sometimes you want the user to tell you how many times to loop around. For example, say you want to add up the costs of some shopping. If you know how many items there are in the basket this is easy to do using a for loop.

# code to add up shopping

totalCost = 0 # set total cost first
items = int(input("How many items did you buy? "))

for i in range(0, items):
itemCost = int(input("Cost of item: "))
totalCost = totalCost + itemCost # add to total

print("Total cost:", totalCost)

The use of a "running total" variable (totalCost) is an important idea. This is a really handy idea to think about and can be used in all sorts of ways. The line:

totalCost = totalCost + itemCost

is a really important part of this. It takes the current value or totalCost and adds the itemCost to it and then updates totalCost with the result. Make sure you understand this - it's super important.

Note - if you don't know how many items there are in the basket before you start (or can't be bothered to count them) then you need to use a while loop. This can keep looping until the user tells it to stop (a bit like a checkout operator pressing a button on a till to say that there's no more shopping to scan).

Using For Loops with Lists

For loops are super useful if you want to add items to a list or look at every item in a list.

# adding items to a list
songList = [ ] # start with an empty list

for i in range(0, 3): # will run loop 3 times
songList.append(input("Enter song " + str(i+1) + ": "))

print(songList) # print the list out

You can also use for loops to print items in a list out line by line.

To do this it's convenient to use the length of the list as the number of times to iterate (a techy term for loop). The built in len function does this nicely.

# print items in a list
shopList = ["Beans", "Chicken", "Potatoes", "Custard"] # set list

print("Shopping List")

for i in range(0, len(shopList)):
print(shopList[i])

print("End of list")

It's important to remember that the first item in a list has an index of 0. This makes starting from 0 in a for loop a really good idea.

Useful stuff

These might be useful if you need to understand more about what's going on in for loops.

PDF iconLooping with strings - for loops using strings

PDF iconFor loops - a bit more general use of for loops