Chapter3 - Iterations
Chapter3 - Iterations
Computer
Programming
Iterations
Prepared by Dayanasari binti Abdul Hadi
Learning Objective
• Example:
i = 1
while i <= 5:
print(i)
i += 1 # Increment i to avoid infinite
loop
2024/2025 SEM II BERV 1313 Computer Programming 5
while loop with FLowchart
Flow chart
i = 1
Variable name i
START
while
while ii <=
<= 3: Variable value 21
34
3: print(i
print(i)
)i +=
i += 1 i = i + 1 i=1
1
FALSE
i <= 3
1 TRUE
3
i+=1
Exercise:
Change the code so that its print the number 1,
3, 5,7 and 9
2024/2025 SEM II BERV 1313 Computer Programming 6
Sentinel controlled loop
• A sentinel controlled loop continues to loop until reaching a special value that signal the end.
• The special values is called sentinel and any values might be.
• In this example, variable txt is the sentinel as it is used to signal when to stop the loop.
count = 0
say = ""
txt = ""
result = "th" in
True
"Python"
print (result)
lette
D a y a n a s a r i A b d u l H a d i r
D
a
y
Exit for loop at letter = y 0x100
0
Exercise:
Break when letter is y or n Change or to and in the if
condition
2024/2025 SEM II BERV 1313 Computer Programming 19
Example on break statement in
while loop i
• Example 1: 3
4
0
1
2
TRUE
i = 0 0x1000
while i < 10 :
: :
10 :print(
print( ii )
print( 0
i
if
)
iff ii ==
== 4 : End of while loop at i = 4 1
4 : break
break 2
i+=1
i+=1 3
4
Exercise:
Change the coding above to implement for loop
count = 0 If the while and for loops end normally, then else statement is executed.
FALSE count
while count
while
while count <<< 4:
count 4:
print("Countis:",
4: print("Count
print("Count is:",count)
is:", count) 4
0
1
2
3
count
count
count) +=
count +=
+= 11 0x1000
1if count
if
if count ==
count == 2:
== 2:
2: print("Breaking
print("Breakingthetheloop")
loop")
Count is: 0
break
else:
else: Count is: 1
print("Loopcompleted
print("Loop completedwithout
withoutbreak") Breaking the loop
break") Count is: 2
print("Loop has ended.")
Count is: 3
print("Loop has ended.")
Loop completed without break
Loop has ended.
2
0
3
41
FALSE while count
while
while count <<< 4:
count 4: when count = 4, EXIT while loop
print("Count is:",
is:", count)
count) 0x1000
4: print("Count
print("Count is:",
count
count +=
count
count) += 11
+= Count is: 0
if
1if count
if count ==
count == 2:
== 2: Count is: 1
2: pass
Count is: 2
print(“Count
print(“Countis:“,
is:“,count)
count) Count is: 3
Count is: 4
When count is 2, the pass statement is encountered, so nothing
happens, and the loop moves to the next iteration.
2024/2025 SEM II BERV 1313 Computer Programming 24
Nested loop
i = 1 12
3 1
2
When i = 3, it EXIT while loop 0x1000 0x1004
# Outer while loop FALSE
while ii <=
while 2:
<= 2:
Outer loop print("WhileLoop
2: print("While
print("While LoopIteration
Loop Iteration{i}:")
Iteration {i}:")
{i}:")
# Inner for loop While Loop Iteration 1
forj jin
for inrange(1,
range(1,3): 3): For Loop Iteration 1
Inner loop
print(" For
print(" For Loop
Loop Iteration
Iteration {j}")
{j}") For Loop Iteration 2
While Loop Iteration 2
i +=
ii += 11
+= Start at 1 For Loop Iteration 1
1 (inclusive)
End at 3 For Loop Iteration 2
When j = 2, it EXIT for loop (exclusive)
Repeat the for
2024/2025 SEM II
loop same as
BERV 1313 Computer Programming 26
previous
Nested loops 1: while loop in
while loop
# Initialize row number
row = 1