Module6 - Loops
Module6 - Loops
1
LOOPS
•A loop in programming is a control structure that
allows a set of instructions to be executed
repeatedly based on a specific condition or for a
predetermined number of times.
•Loops are used when you want to perform the
same task multiple times without writing the code
for that task each time.
2
3
For loop flowchart
for loop
• Executes a block of code a fixed number of times,
iterating over a sequence (such as a list, tuple,
string, etc.) or a range of values.
– # Example: Printing numbers from 0 to 4 using a for loop
for i in range(5):
print(i)
While loop flowchart
while loop
• Repeats a block of code as long as a specified
condition is true. The loop continues to execute
until the condition becomes false.
• # Example: Printing numbers from 0 to 4 using a
while loop
count = 0
while count < 5:
print(count)
count += 1
Infinite loop
• A construct where a sequence of instructions
repeats endlessly.
• Loop that continues indefinitely unless explicitly
interrupted by some external means, like user input
or a system command.
• Infinite loops are generally unintended in
programming, as they can cause programs to
become unresponsive or consume excessive
resources.
• They often occur due to coding errors or incorrect
logic within the loop structure.
Infinite Loop Example
• 1
x=10
i=1
while i<=x:
print("infinite loop")
• 2
while True:
print("This loop will run indefinitely!")
break and Continue statement
• break statement: It terminates the current
loop and resumes execution at the next
statement immediately following the loop.
• continue statement: It skips the rest of the
code inside the loop for the current iteration
and proceeds to the next iteration of the loop.
break and Continue statement
# Example: Using break and continue in a loop
for i in range(10):
if i == 3:
break # Stop the loop if i is 3
if i == 1:
continue # Skip the rest of the code
in this iteration if i is 1
print(i)
for loop example explained
• When i is 1, the continue statement is
encountered. It skips the print(i) statement
and jumps to the next iteration of the loop.
• When i is 3, the break statement is
encountered. It terminates the loop
immediately.
• Output:
0
2
break and Continue statement cont.
# Example: Using break and continue in a while loop
count = 0
while count < 5:
if count == 2:
count += 1 # Increment count to avoid an infinite loop
continue # Skip printing 2 and move to the next
iteration
print(count)
if count == 3:
break # Terminate the loop when count reaches 3
count += 1
while loop example explained
• When count is 2, the continue statement is
executed. It skips the print(count) statement
and directly moves to the next iteration.
• When count is 3, the break statement is
executed, terminating the loop immediately.
• Output:
0
1
3
Printing Multiplication Table
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50
Loop Exit
i <=10
TRUE FALSE
Loop
Dec-24 Python Programming 17
Printing Multiplication Table
Input n
i=1
TRUE
i <=10
FALSE n = int(input('n=? '))
i=1
Print n x i = ni Stop
i = i+1
S1 S2
1. Evaluate expression
2. If TRUE then
a) execute statement1
b) goto step 1.
3. If FALSE then execute statement2.
Dec-24 Python Programming 19
Classwork
• Print the sum of the reciprocals of the
first 100 natural numbers.