0% found this document useful (0 votes)
14 views20 pages

Comp117 Unit 06

Uploaded by

karanrajgiri1066
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)
14 views20 pages

Comp117 Unit 06

Uploaded by

karanrajgiri1066
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/ 20

COMP 117

Python Programming
by Manoj Shakya

These slides are heavily influenced by MITOPENCOURSEWARE 6.0001


Be respectful to
others as you want
them to be respectful
to you!

COMP117 - UNIT 6 2
Unit 6 - Loops

COMP117 - UNIT 6 3
Contents

• Introduction to looping structure


• The while statement
• The for statement
• using range function (using for to write a count-controlled loop)
• The break, continue, and pass statements
• Nested Loops

COMP117 - UNIT 6 4
Looping

• Definition:
Loops are control structures that repeat a block of code until a
certain condition is met.
code
• Importance:
• Iterating over data
• Automating repetitive tasks, and
• Efficient code execution. condition
• Two looping statements
• while loop False
True
• for loop
loop body

code

COMP117 - UNIT 6 5
The while statement

• <condition> evaluates to a • Syntax


Boolean
• if <condition> is True, do
all the steps inside the while
code block while <condition>
<statement1>
• check <condition> again <statement2>
• repeat until <condition> is …
False

COMP117 - UNIT 6 6
Examples (while loop)

COMP117 - UNIT 6 7
Trace Table
LN factorial current number test

1 4

2 1

3 1

5 True
6 1

7 2

5 True

6 2

7 3
5 True
6 6

7 4

5 True

6 24
7 5

5 False

9 24 4

COMP117 - UNIT 6 8
The for statement

• Iterate through numbers in a sequence

# more complicated with while loop


n = 0
while n < 5:
print(n)
n = n+1

# shortcut with for loop


for n in range(5):
print(n)

COMP117 - UNIT 6 10
The for statement

for <variable> in range(<some_number>):


<expression>
<expression>
...
• Each time through the loop, <variable> takes a value
• First time, <variable> starts at the smallest value
• Next time, <variable> gets the prev value + 1
• And so on…

COMP117 - UNIT 6 11
range(start, stop, step)

• Default values are start=0 and step=1 and optional


• Loop until value is stop-1

mysum = 0
for i in range(7,10):
mysum += i
print(mysum)

mysum = 0
for i in range(5,11,2):
mysum += i
print(mysum)

COMP117 - UNIT 6 12
Examples (for loop w/ Trace Table)
LN sum_of i num i<num num%i
_divis == 0?
ors
2 6

5 0

6 1 True

7 True

8 1

6 2 True

7 True

8 3

6 3 True

Complete yourself!!!
COMP117 - UNIT 6 13
The break statement

mysum = 0

for i in range(5, 11, 2):


mysum += i
if mysum == 5:
break
mysum += 1
print(mysum)

• What happens in this program?

COMP117 - UNIT 6 14
The continue statement

mysum = 0

for i in range(5, 11, 2):


mysum += i
if mysum == 5:
continue
mysum += 1
print(mysum)

• What happens in this program?

COMP117 - UNIT 6 15
The pass statement

for i in range(5, 11, 2):


pass

• What happens in this program?

• The pass statement is used as a placeholder for future code.


• When the pass statement is executed, nothing happens, but you
avoid getting an error when empty code is not allowed.
• Empty code is not allowed in loops, function definitions, class
definitions, or in if statements.

COMP117 - UNIT 6 16
for vs while loops

for loops while loops


• Know number of iterations • Unbounded number of
• Can end early via break iterations
• Uses a counter • Can end early via break
• Can rewrite a for loop using a • Can use a counter but must
while loop initialize before loop and
increment it inside loop
• May not be able to rewrite a
while loops using a for loop

COMP117 - UNIT 6 17
Nested loops (while loop)

# Outer loop
i = 1
while i <= 3:
# Inner loop
j = 1
while j <= 3:
print(f"i = {i}, j = {j}")
j += 1
i += 1

COMP117 - UNIT 6 18
Nested loops (for loop)

# Outer loop
for i in range(1, 4):
# Inner loop
for j in range(1, 4):
print(f"i = {i}, j = {j}”)

COMP117 - UNIT 6 19
Let’s practice Lab #6

COMP117 - UNIT 6 20
The end!

COMP117 - UNIT 6 21

You might also like