Week 14 Explore 2 Iteration
Week 14 Explore 2 Iteration
being
recorded
Starter
What is Iteration?
What is Iteration?
2. Programming
There are times when a program needs to repeat certain steps until told otherwise, or
until a condition has been met. This process is known as iteration.
The program ‘loops’ back to an earlier line of code.
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 execute it again and again until no longer
needed.
ITERATION – COUNT CONTROLLED
Count controlled
Iteration means repetition. Statements to be repeated are placed inside a loop structure. A
FOR … NEXT loop is a count-controlled loop. A counter is automatically incremented each
time the loop is performed.
Activity 13.2.1
Complete For loops
13.2.1 on
CodeHS • Write a program that prints out ‘I like
Ellie Goulding’ ten times.
Activity 13.2.1 - Answer
Complete print(number)
Activity
Explain what this program does.
CodeHS
Write a program that prints out the 9 times table (up to 20 x
9).
Write a program that asks the user which times table they
want and then displays that table.
Write a program that asks for a start value and an end value
and then counts in fives between those numbers.
A program that prints the numbers from 1 to 15: A program that asks the user which times table they want and then
displays that table:
for number in range(1,16):
print(number + 1) table=int(input("What times table would you like? "))
for number in range(1,21):
A program that prints the numbers from 1 to 8, with the print(number, number*table)
square of each number, both on the same line:
A program that asks for a start value and end value and then counts
for number in range(1,9):
in fives between those numbers:
print(number, number*number)
start=int(input("Start number "))
A program that prints out the 9 times table (up to 20 x end=int(input("End number "))
9): for number in range(start,end,5):
print(number)
for number in range(1,21):
print(number, number*9)
Built in Functions – range()
x = range (3,6)
range(start, stop, step) for n in x:
print(n)
EXAMPLE:
x = range (3,20,2)
for n in x:
print(n)
Activity 13.2.3
Activity 13.2.3
Activity
13.2.3 on
CodeHS • Explain the purpose of the ‘name’ variable.
mylist=["cat","book","trumpet","swimming costume"]
for item in mylist:
print("I would take on my desert island a", item)
Activity 13.2.4
Activity 13.2.4
Re-arrange the program statements to write a
Complete program that will print out the names of the animals
that begin with the character ‘c’. You will need to add
appropriate indentation.
Activity print(next)
on ary"]
for next in myList:
CodeHS
Activity 13.2.4 - Answer
myList=["cat","dog","cow","donkey","rabbit","canary"]
for next in myList:
if next[0] == "c" :
print(next)
Syllabus
Lesson complete!
See you next lesson