ICT 2013-
COMPUTTIONAL
THINKING & CODING
CHAPTER ???
REPETITION (FOR LOOP)
CHAPTER ??? CLO3: Develop computer
algorithms to solve real life
programming problems
Versio Author Effectiv Change Des DRC DOCUMENT
n
1.0 Anand
e Date cription
Sept Define the
No
001
REVISION
Pandiyan 2020 first version CONTROL
1.1 Dr Sept Minor 001
(DRC)
Madeleine 2021 changes: upda
Togher ting the
formatting
X.Y
X: MAJOR CHANGE
Y: MINOR CHANGE
2
LECTURE NOTES
Contents of lectures are based on the textbook, recommended text, and supplementary
material
Please read supplementary material from page
18 to 18
Please read textbook chapter from page
96 to 98
3
1. THE FOR LOOP
2. FOR LOOP WITH END VALUE ONLY
3. FOR LOOP WITH START & END VALUES
4. FOR LOOP WITH INCREMENT VALUE
5. TASK
LECTURE
OUTLINE
6. ALGORITHM – REPETITION (FOR LOOP)
7. Conditions with loop
8. EXERCISES
KEY TERMS
4
LECTURE OBJECTIVES
At the end of lecture, the student should be able to:
Apply a for statement to repeat set of statements.
Understand and apply different types of for loops.
Write Python code to solve various computational problems that require the use of the
for statement.
Define Algorithms for Repetition to assist in writing code
5
1. THE FOR LOOP
The for loop repeats a block of code for number of times.
In this example, we will repeat 4 times.
counter
for i in range(4):
for loop
number of times
to repeat
1. THE FOR LOOP – KEY POINTS TO REMEMBER
The for loop is one kind of loop. It uses a counter which takes
on a new value every time the loop iterates.
The range() function indicates how many times the statement
will repeat.
E.g. range(4) means repeat 4 times and range(10) means
repeat 10 times 7
2. FOR LOOP WITH END VALUE ONLY
for i in range(endValue):
Statements
The starting value of a loop is 0. In actual fact, range(4) generates four numbers
0,1,2 and 3 and the counter takes on each value one at a time.
Example Output
8
3. FOR LOOP WITH START & END VALUES
for i in range(startValue, endValue):
Statements
The starting value of loop can be changed to any given number.
Note: Start value must be less than the end value, otherwise loop will not be
executed.
Output
Example
9
3. FOR LOOP WITH INCREMENT VALUE
for i in range(startValue, endValue, stepValue):
Statements
The starting value of loop can be changed to any given number. Step value can be
change from 1 to any value.
Note: Step value must be negative if start value is greater than end value.
Example Output
10
4. FOR LOOP WITH INCREMENT VALUE - EXERCISE
Exercise 1: Write a program that reads 5 marks of a student, calculate and display
the average mark
# program to read 5 marks and display its average
total_marks = 0
for i in range(5):
mark = int(input("Enter a mark "))
total_marks = total_marks + mark
avg_mark = total_marks / 5
print("Average mark ", avg_mark)
11
5. TASK
1. Identify and fix the errors in the below program.
2. Identify the output of the below program.
12
6. ALGORITHMS – REPETITION (FOR LOOPS)
Define a for loop algorithm that reads the price of 5 products, calculates and displays the total price.
reset and assign 0 to total price
for counter in range(5):
[ask user to enter a price
read in value entered by user and assign to product price
set total price as total price + Product Price]
Print the total price
This loop, repeats [list of instructions] from counter = start until end - 1
13
6. ALGORITHMS – REPETITION (FOR LOOPS)
Translate the for loop algorithm that reads the price of 5 products, calculates and displays the total price into Python
ALGORITHM CODE and OUTPUT
reset and assign 0 to total price
for counter in range(5):
[ask user to enter a price
read in value entered by user and assign to product price
set total price as total price + product price]
Print the total price
This loop, repeats [list of instructions] from counter = start until end - 1
14
1. USING CONDITION WITH LOOP
You caan use a condition statement with a loop, for example identify how many even
numbers in the given 10 numbers? Asume user enter below list of numbers how many even
numbers are there?
2, 3, 13, 5, 12, 0, 8, 6, 7, 9
evens = 0
For counter in range(10:
Read number
if the number is even:
evens = evens + 1
print(evens)
15
7. ALGORITHMS – AN EXAMPLE
We want to define an algorithm that will find the maximum number
in the following list:
1, 3, 5, 6, 2, 8, 9, 10, 11, 2, 4
Set maxNumber to first number in list
for every number in the list
if the number is greater than maxNumber
assign number to the maxNumber
next number
print maxNumber
We will translate this algorithm into code later in the course 16
7. ALGORITHMS – ANOTHER EXAMPLE
Define an algorithm to find all even numbers in the following list:
1, 3, 5, 6, 2, 8, 9, 10, 11, 2, 4
for every number in the list
divide the number by 2
get the remainder of the division
if the remainder = 0
the number is even, print it
else
the number is odd, do nothing
next number
We will translate this algorithm into code later in the course
17
7. EXERCISES - ADVANCED
Exercise 1: Write a program that read 5 numbers and displays the given
number of negative numbers. Create an algorithm first.
Exercise 2: Write a program that displays all the numbers from 1 to 25, that
are divisible by 5. Create an algorithm first.
18
FOR LOOP
INCREMENT
RANGE KEY TERMS
ITERATES
START AND END VALUES
19
FORMATIVE KAHOOT! Q
ASSESSMENT UIZ1
20
21