0% found this document useful (0 votes)
8 views

Module6 - Loops

Uploaded by

Blossomlove12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Module6 - Loops

Uploaded by

Blossomlove12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

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

Dec-24 Python Programming 15


Program…
n = int(input('Enter a number: '))
print (n, 'X', 1, '=', n*1) Too much
print (n, 'X', 2, '=', n*2) repetitio
Can I avo
print (n, 'X', 3, '=', n*3)
it?
print (n, 'X', 4, '=', n*4)
print (n, 'X', 5, '=', n*5)
print (n, 'X', 6, '=', n*6)
….

Dec-24 Python Programming 16


Printing Multiplication Table

Input n Loop Entry


i=1

Loop Exit
i <=10
TRUE FALSE

Print n X i = n*i Stop


i = i+1

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

while (i <= 10) :


print (n ,'X', i, '=', n*i)
i=i+1
print ('done‘)

Dec-24 Python Programming 18


While Statement
while (expression):
S1 FALSE
expression
S2
TRUE

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.

rsum=0.0# the reciprocal sum

# the for loop


for i in range(1,101):
rsum = rsum + 1.0/i
print ('sum is', rsum)

Dec-24 Python Programming 20


Quiz
• What will be the output of the following
program

# print all odd numbers < 10


i = 1
while i <= 10:
if i%2==0: # even
continue
print (i, end=‘ ‘)
i = i+1
Dec-24 Python Programming 21
Continue and Update Expr
• Make sure continue does not bypass update-
expression for while loops

# print all odd numbers < 10


i = 1 i is not incremented
while i <= 10: when even number
if i%2==0: # even encountered.
continue Infinite loop!!
print (i, end=‘ ‘)
i = i+1
Dec-24 Python Programming 22
Simulated While Loop
• Python does not have a built-in do-while loop
like some other languages (e.g., C, C++, Java).
However, you can simulate a do-while loop in
Python using a while loop with a specific
structure.
Flowchart of Do-While Loop
Simulated While Loop
• A do-while loop executes the code block at
least once and then checks the condition at
the end of each iteration. You can achieve this
in Python by using a while loop with a break
condition.
Simulated Do While Loop
• # Simulating a do-while loop
• while True:
• # Code block to execute at least once
• print("This code runs at least once.")

• # Check the condition
• user_input = input("Enter 'yes' to continue or
anything else to stop: ")
• if user_input.lower() != "yes":
• break
Simulated Do While Loop
Suppose you want to keep asking the user for a number until they enter a
positive number:
# Simulating a do-while loop
while True:
number = int(input("Enter a positive number: "))
if number > 0:
print("Thank you!")
break # Exit the loop if the number is positive
else:
print("That's not a positive number. Try again.")
THANK YOU

You might also like