Python Chapter 5.1
Python Chapter 5.1
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)
print(count) print(data)
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
for i in range(1,10):
print(i) i<10