0% found this document useful (0 votes)
3 views23 pages

Week 14 Explore 2 Iteration

This document provides an overview of iteration in programming, explaining its importance in reducing code repetition through loops. It covers different types of loops, including count-controlled and condition-controlled iterations, and provides examples in Python. Additionally, it includes activities for practicing the implementation of loops and using the range function.

Uploaded by

fatimayuriya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views23 pages

Week 14 Explore 2 Iteration

This document provides an overview of iteration in programming, explaining its importance in reducing code repetition through loops. It covers different types of loops, including count-controlled and condition-controlled iterations, and provides examples in Python. Additionally, it includes activities for practicing the implementation of loops and using the range function.

Uploaded by

fatimayuriya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

This lesson is

being
recorded
Starter

What is Iteration?

Theory Past Paper Mark Scheme Video


Starter

What is Iteration?

The term iteration means to repeat something over and


over like a loop.

Iteration can be used in coding to massively reduce the


amount of code that needs to be written.

Theory Past Paper Mark Scheme Video


Edexcel iGCSE Computer
Science

2. Programming

2.2.2 Programming Constructs


Syllabus
Learning Objectives

Understand the construct of Iteration

Understand the diff erent types of loop that can be used

To be able to use Iteration in Python programming


ITERATION – looping in code

 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.

This program would print a


message out SIX times:
for count = 1 to 6
print(“Coding is cool”) print(“Coding is
print(“Coding is cool”) cool”)
print(“Coding is cool”) next count
print(“Coding is cool”) Using a FOR loop,
print(“Coding is cool”) the number of
print(“Coding is cool”) lines of code are
reduced
ITERATION – CONDITION CONTROLLED

Condition-controlled iteration repeatedly executes a section of code until


a condition is met - or no longer met. There are two types of condition-
controlled iteration:

while loops - uses the statements while and endwhile


repeat loops - uses the statements repeat and until
ITERATION – CONDITION CONTROLLED

Pre Condition loop – is controlled by a Boolean condition which is checked


before the loop is entered.

count = 0 This pseudocode would print out a message six


while count <6 times.
print(“Coding is
cool”) The while statement defines the start of the
count = count + 1 loop
endwhile
The endwhile statement declares the end
of the loop
ITERATION – CONDITION CONTROLLED

Post Condition loop – is controlled by a Boolean condition which is checked at


the end of the loop. It is therefore always performed at least once..

This pesudocode would print out a message 10


times.
count = 0
repeat
The repeat statement defines the start of
print(“Coding is
the loop
cool”)
count = count + 1 The until statement declares the end of the
until count = 10 loop
(in this type of loop, the code is always run at
least once)
Activity 13.2.1

Activity 13.2.1
Complete For loops

Activity • Copy and run this program.

13.2.1 on
CodeHS • Write a program that prints out ‘I like
Ellie Goulding’ ten times.
Activity 13.2.1 - Answer

for number in range(10):


print(“I like Ellie Goulding”)
Activity 13.2.2
Activity 13.2.2

 Copy and run this program:

for number in range(10):

Complete print(number)

Activity 


Explain what this program does.

Write a program that prints the numbers from 1 to 15.

13.2.2 on  Write a program that prints the numbers from 1 to 8, with


the square of each number, both on the same line.

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.

Hint: range(start value, end value, step)


Activity 13.2.2 - Answer

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()

The range() function returns a sequence of numbers, starting from 0 by


default, and increments by 1 (by default), and stops before a specified
number. EXAMPLE:

Create a sequence of numbers from 3 to 5, and print each item in the


Syntax sequence:

x = range (3,6)
range(start, stop, step) for n in x:
print(n)

EXAMPLE:

Create a sequence of numbers from 3 to 19, but increment by 2


instead of 1:

x = range (3,20,2)
for n in x:
print(n)
Activity 13.2.3

Activity 13.2.3

Complete • Copy this program into a file and run it.

Activity
13.2.3 on
CodeHS • Explain the purpose of the ‘name’ variable.

• Write a program that creates a list of things you would


take to a desert island. The program should then display each
item in the list a line at a time.
Activity 13.2.3 - Answer

● The ‘name’ variable will be assigned to each item in the list


each time through the for loop.

● A program that creates a list of things you would take to a


desert island and displays each item a line at a time:

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)

13.2.4 if next[0] == "c" :


myList=["cat","dog","cow","donkey","rabbit","can

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

You might also like