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

Looping Constructs

The document discusses looping constructs in Python. It provides examples of using for and while loops. The for loop is used to print "Python is awesome" 1000 times. It uses the range(1000) function and indentation to iterate. The while loop is used to repeatedly take an exam until getting an A grade on the math subject, with the grade != 'A' comparison as the stopping criteria.

Uploaded by

viswa teja
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Looping Constructs

The document discusses looping constructs in Python. It provides examples of using for and while loops. The for loop is used to print "Python is awesome" 1000 times. It uses the range(1000) function and indentation to iterate. The while loop is used to repeatedly take an exam until getting an A grade on the math subject, with the grade != 'A' comparison as the stopping criteria.

Uploaded by

viswa teja
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Looping Constructs

Scenario

● Print “Python is awesome” 1000 times

print(“Python is awesome”) print(“Python is awesome”)


print(“Python is awesome”) print(“Python is awesome”)
print(“Python is awesome”) …….
print(“Python is awesome”) …….
print(“Python is awesome”) …….
print(“Python is awesome”) …….
print(“Python is awesome”) …….
Scenario

● Print “Python is awesome” 1000 times

print(“Python is awesome”) print(“Python is awesome”)


print(“Python is awesome”) print(“Python is awesome”)
print(“Python is awesome”) Looping Statements!! …….
print(“Python is awesome”) …….
print(“Python is awesome”) …….
print(“Python is awesome”) …….
print(“Python is awesome”) …….
Looping constructs in Python

● Print “Python is awesome” 1000 times

Pseudo-Code:
Looping constructs in Python

● Print “Python is awesome” 1000 times

Pseudo-Code:
print “Python is awesome” (repeat 1000 times)
Looping constructs in Python

● Print “Python is awesome” 1000 times

Pseudo-Code: Code:
print “Python is awesome” (repeat 1000 times) for i in range(1000):
print(“Python is awesome”)
Looping constructs in Python: The for loop

● The ‘for’ loop in python

Pseudo-Code: Syntax:
print “Python is awesome” (repeat 1000 times) for iterating_variable in sequence:
statements(s)
Looping constructs in Python: The for loop

● The ‘for’ loop in python

Pseudo-Code: Syntax:
print “Python is awesome” (repeat 1000 times) for iterating_variable in sequence:
statements(s)

4 space
“indentation”
Looping constructs in Python: The for loop

● The ‘for’ loop in python

Pseudo-Code: Code:
print “Python is awesome” (repeat 1000 times) for i in range(1000):
print(“Python is awesome”)
Looping constructs in Python: The for loop

● The ‘for’ loop in python

Pseudo-Code: Code:
print “Python is awesome” (repeat 1000 times) for i in range(1000):
print(“Python is awesome”)

4 space
“indentation”
Looping constructs in Python: The for loop

● The ‘for’ loop in python


“stop condition”
Pseudo-Code: Code:
print “Python is awesome” (repeat 1000 times) for i in range(1000):
print(“Python is awesome”)

4 space
“indentation”
Types of loops: Example
Types of loops: Example

Scenario #1:

Pass in 5 subjects:

● Math
● Physics
● English..etc.
Types of loops: Example

Scenario #1:

Pass in 5 subjects:
Code:
● Math
for subject in range(5):
● Physics
# Pass exam 5 times
● English..etc.
# Once for each subject
Types of loops: Example

Scenario #1:
“stop condition”
Pass in 5 subjects:
Code:
● Math
for subject in range(5):
● Physics
# Pass exam 5 times
● English..etc.
# Once for each subject
Types of loops: Example

Scenario #2:

● Secure at least an A grade in


Math to pass.
Types of loops: Example

Scenario #2:

● Secure at least an A grade in


Math to pass.
● When do you stop?
Types of loops: Example

Scenario #2:

● Secure at least an A grade in


Math to pass.
● When do you stop?
● Keep trying until you succeed!
● New kind of looping needed..
Types of loops: Example

Scenario #2:

● Secure at least an A grade in


Code:
Math to pass.
while grade != ‘A’:
● When do you stop?
# Keep repeating until
● Keep trying until you succeed! # comparison gives a False
● New kind of looping needed..
Types of loops: Example

Scenario #2:

comparison (“stopping criteria”)


● Secure at least an A grade in
Code:
Math to pass.
while grade != ‘A’:
● When do you stop?
# Keep repeating until
● Keep trying until you succeed! # comparison gives a False
● New kind of looping needed..
Looping constructs in Python: The while loop

● The ‘while’ loop in python

Syntax: Code:
while comparison:
while grade != ‘A’:
statements(s) # Keep repeating until
# comparison gives a False
Looping constructs in Python: Summary

‘for’ loop ‘while’ loop

Code: Code:

for subject in range(5): while grade != ‘A’:


# Pass exam 5 times # Keep repeating until
# Once for each subject # comparison gives a False
Thank You

You might also like