5.
Looping structure
5.1 While loop
5.2 For Loop
▪ explain the looping concept
▪ List types of looping in Python
▪ Overview the loop structure
▪ Evaluate while and for loop
▪ Practicals
▪ Synonym : iteration, repetition
▪ codes are executed repeatedly until specific
condition is accessed
▪ 2 types of loop
▪ for loop
▪ while loop
▪while loop
▪for loop
INITIALIZE
▪ All while loop contains 3 important LCV
parts:
▪ Initialize
TEST
LCV
▪ while(Loop test)
▪ Updating STATEMENTS
TO REPEAT
STATEMENTS
UPDATE
LCV
▪ Counter controlled while loop
▪ Loop controlled by a counter variable. Number of iteration is
known
▪ Sentinel controlled while loop
▪ Loop controlled by a sentinel value. The sentinel value is
entered to stop the iteration
▪ Event controlled while loop
▪ Loop controlled by result of calculation. The calculation is
inside loop body. Loop stop when result reaches limit
▪ Flag controlled while loop
▪ Loop controlled by Boolean variable
COUNTER CONTROLLED WHILE SENTINEL CONTROL WHILE LOOP
LOOP (execute 5 times) (0 is the sentinel value)
count=1 data=input("type a number or type 0 to stop")
while count<=5: while data!=0:
print(count) print(data)
count=count+1 data=input("type a number or type 0 to stop")
count count<= output data data!=0 output
5 1 T 1
1 T 1 In these loop structure, identify 12 T 12
2 T 2 1. Initialization 3 T 3
3 T 3 2. Loop test 400 T 400
3. updating
4 T 4 -5 T -5
5 T 5 0 F
6 F
▪ Read and display marks. Type -1 to stop the entry. Input marks
marks! false
=-1
marks marks!=-1 Output
Input
marks
10 TRUE 10 true
70 TRUE 70
print(marks)
30 TRUE 30
print(“Bye”)
Input 40 TRUE 40
marks
50 TRUE 50 Input marks
-1 F Bye
EVENT CONTROLLED WHILE
LOOP FLAG CONTROLLED WHILE LOOP
sum=0 found=false
while sum<100: while not found:
print(sum) value=input("enter a number")
sum=sum+20 print(value)
if value==0:
found=true
In these loop structure, identify
1. Initialization
2. Loop test
3. updating
i=1
for i in range(1,10):
print(i) i<10
Make changes based on the following instructions and
observe the output print(i)
1. Change range(1,10): to range(1,100):
2. Change range(1,100): to range(1,100,2): i=i+1
3. Change range(1,100,2): to range(0,100,5):
Try this:
for i in range(0,10):
x=int(input(“Type an integer value”))
1. How many values read?
▪ Other than display values, there are more important activities that programmer
must master such as:
▪ Summing in a loop
▪ Counting in a loop
▪ Read values in a loop
Sum all numbers range 1-9 sum=0
for i in range(1,10):
1. set summing variable (SV) to 0 print(i)
2. Construct a loop range from 1-10 sum=sum+i
using i as LCV
print(“sum of numbers is “, sum)
3. Add sum to i
count=0
▪ Count how many even numbers in
numbers in the range 1-10 for i in range(1,10):
print(i)
1. Initialize counter variable (CV) to 0
if i%2==0:
2. Construct a loop
count=count+1
3. Use selection to test for even print(“count of even numbers is “, count)
numbers
4. If test is true, increase the counter
by 1
▪ Read 10 marks
for i in range(0,10):
1. Construct a loop that execute 10 mark=int(input(“enter a mark”))
times
2. Place input statement in the loop
body
▪ Determine what this codes do?
▪ Use the following input values:
70, 40, 60,55,89,100,30,20,58,77
Then, write the output
pass=fail=0
stud=1
while stud<=5:
mark=eval(input("enter a mark :"))
sum=sum+mark
if mark>=40:
pass=pass+1
else:
fail=fail+1
print("average= ",sum/5)
print("pass= ",pass)
print("fail= ",fail)
▪ Determine the output produced by this loop
even=odd=0
for x in range(1,20):
if x%2==0:
even=even+1
else
odd=odd+1
print("total even= ",even)
print("total odd= ",odd)
1. Modify the codes so that it
LCV=1 display the first 100 integers
while(LCV <=5):
2. Modify the codes in 1. so that it
print(“hello-”, LCV) display all even numbers.
LCV=LCV+1
3. Modify codes in 2, so that it
print(“bye”)
count all even numbers
4. Modify the codes in 3 so that it
sum all even numbers
▪ Write codes to find sum of all natural numbers
between 1 to n where n is entered by user.
▪ Example output:
How many numbers? 4
The sum of 4 numbers is 10
▪ Add to your statement to sum all numbers
entered by user. Calculate the average
outside the loop. Display the result.
score=int(input("type a score or type -1 to
stop")) ▪ Add to your statement to count how many
numbers entered by user. Display the
while score!= -1:
result outside the loop
score=int(input("type a score or type -1
to stop")) ▪ Add to the program so that it can count
how many pass and fail. Given, pass value
is 60 and above. Display the result
▪ Test the program with the following score values:
10 50 70 90 35 100 82 -1
▪ Display the value entered as follows:
Score entered : x
▪ Modify the loop so that it uses sentinel value 999 to
stop the loop
▪ Write program to find sum of all odd numbers between 1 to n.
▪ Read 10 numbers. Determine how many positive, zero and negative
numbers entered by user. Display the result.
▪ Read 10 marks. For each mark entered, test for valid marks (0-100
inclusively). Determine whether the mark is excellent, pas or fail.
Given, excellent marks are marks above 80 inclusively, pass marks
are marks between 50-79 inclusively and fail marks are marks below
50. Give appropriate message if the mark is invalid