0% found this document useful (0 votes)
4 views34 pages

11 Loop Statements

The document is an educational resource from UNESCO UNITWIN, focusing on loop statements in programming, specifically 'while' and 'for' loops. It includes learning objectives, examples, exercises, and answers to help learners understand how to implement and troubleshoot loop statements. The material is intended for non-commercial educational use only.

Uploaded by

Thae Thae Aung
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)
4 views34 pages

11 Loop Statements

The document is an educational resource from UNESCO UNITWIN, focusing on loop statements in programming, specifically 'while' and 'for' loops. It includes learning objectives, examples, exercises, and answers to help learners understand how to implement and troubleshoot loop statements. The material is intended for non-commercial educational use only.

Uploaded by

Thae Thae Aung
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/ 34

UNESCO UNITWIN

Online Course
Supported by

Handong Global University


Ministry of Education, Korea

* All copyrights belong to UNESCO UNITWIN and it is prohibited to use these educational materials including the video and other
relevant materials for commercial and for-profit use.
Loop Statements
11
Global Leadership School
Handong Global University
Learning Objectives
• Understand the condition in loop statement
• Understand the basic while
• Understand the basic for
Loop statement
• When handling repeating tasks
• Same or regularly repeating tasks
 Repeat while it meets the conditions
• Types of loops
• while
• For # print 0,1,2,…9 # print 0,1,2,…9

i=0 for i in range(10) :


while i < 10 : print(i)
print(i)
i=i+1
Flow of loop

False
Condition?

True
statements
while
# print 0,1,2,…9
i=0
while i < 10 :
print(i)
i=i+1

print(‘exit value; ‘, i)

• Check if condition is true or false


• If the condition is false(0)
• End while and execute next lines
• If the condition is true(1)
• Run body in the while loop
• After executing the body, go back to beginning of the
body
Example ‘while’ 1
# What is the output?

value = 50

while value < 100 :


value = value + 10
print(value)

print(“last value= “, value)


Example ‘while’ 2
# What is the output?

value = 100

while value > 0 :


value = value - 5
print(value)

print(“last value= “, value)


Example ‘while’ 3
# What is the output?

i=1
while i <= 2**10 :
print(i)
i=i*2

print(“last i= “, i)
Example ‘while’ 4
# What is the output?

i = 2**10
while i > 1 :
print(i)
i = i // 2

print(“last i= “, i)
For loop
• for loop is executed based on repetition
• It is used with repeatable strings and lists
• Used with range() for <variable> in <sequence>:
<statements>

for i in range(10) :
if i%2 == 0:
print(“@” * i)
else :
print(“^” * i)

print("Final number = “, i)
Example ‘for’ 1
# Print numbers from 0 to 9

for i in range(10) :
print(i)
Example ‘for’ 2
# Print numbers from 10 to 1 (without 0)

for i in range(10,0,-1) :
print(i)
Example ‘for’ 3
# Print ’Hello’ 5 times, ‘There’ once.

for i in range(5) :
print ("Hello")

print ("There")
Example ‘for’ 4
# Print 'Hello' 'There’ 5 times

for i in range(5) :
print ("Hello")
print ("There")
Exercise 1-1, Find the mistake

#1

i=1
while i > 1 :
print(i)
i = i +1

print(“last i = “, i )
Exercise 1-1, Answer

#1

i=1
while i > 1 :
print(i) while() body is never
i = i +1 executed.

print(“last i = “, i )
Exercise 1-2, Find the mistake

#2

i=1
while i < 10 :
print(i)
i=i-1

print(“last i = “, i )
Exercise 1-2, Answer
Value of i is negative number
causing the program
#2
to endlessly execute.
It will keep running
i=1
until IDLE is closed
while i < 10 :
print(i)
i=i-1

print(“last i = “, i )
Exercise 1-3, Find the mistake

#3

for i in range(1,10,3)
print(i)
print(i**2)
Exercise 1-3, Answer
No colon at the end of the
#3 condition. This won’t execute.

for i in range(1,10,3) :
print(i)
print(i**2)
Exercise 2-1, What is the ouput?

#1

i=0
j = 10
n=0

while i < j :
i=i+1
n=n+2

print(“i= “, i)
print(“n= “, n, “***”)
Exercise 2-1, Answer

#1

i=0
j = 10
n=0

while i < j :
i=i+1
n=n+2

print(“i= “, i)
print(“n= “, n, “***”)
Exercise 2-2, What is the output?

#2

for i in [10, 1, 5,9, 21, 53] :


print(“i = “, i)
print(“i ** 2= “, i**2)
Exercise 2-2, Answer

#2

for i in [10, 1, 5,9, 21, 53] :


print(“i = “, i)
print(“i ** 2= “, i**2)
Exercise 2-3, What is the output?

#3

num = 0

while num < 20 :


print(num)
num = num + 2

print("last num: ", num)


Exercise 2-3, Answer

#3

num = 0

while num < 20 :


print(num)
num = num + 2

print("last num: ", num)


Lecture Summary
• Understand the condition in loop statement
• The repetition or suspension is determined by the
value of the condition
• Understand while()
• If the condition is false(0), it exits the while
statement.
• If the condition is true(1), execute while statement.
• Understand for()
• Used with repeatable strings and lists
Practice Question 1
• What is the execution result of the following
code? value = 50
while value < 100 :
print(value)
value = value + 15
print(“last value= “, value)

50 50 50 50
65 65 65 65
80 80 80 80
95 95 95 95
110 last value= 110 110 last value= 95
last value= 110 last value= 125
Practice Question 1, Answer
• What is the execution result of the following
code? value = 50
while value < 100 :
print(value)
value = value + 15
print(“last value= “, value)

50 50 50 50
65 65 65 65
80 80 80 80
95 95 95 95
110 last value= 110 110 last value= 95
last value= 110 last value= 125
Practice Question 2
• Which code outputs the following Hello
There
results? Hello
There
Hello
There

for i in range(6) : for i in range(3) : for i in range(3) : for i in range(6) :


print ("Hello") print ("Hello") print ("Hello") print ("Hello")
print ("There") print ("There") print ("There") print ("There")
Practice Question 2, Answer
• Which code outputs the following Hello
There
results? Hello
There
Hello
There

for i in range(6) : for i in range(3) : for i in range(3) : for i in range(6) :


print ("Hello") print ("Hello") print ("Hello") print ("Hello")
print ("There") print ("There") print ("There") print ("There")
Thank you
11 Loop Statements
Contact Info
unitwinlms@handong.edu
https://ecampus.handong.edu/

* All copyrights belong to UNESCO UNITWIN and it is prohibited to use these educational
materials including the video and other relevant materials for commercial and for-profit use.
CREDITS: This presentation template was created by Slidesgo, including icons by Flaticon, and infographics & images by Freepik.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy