11 Loop Statements
11 Loop Statements
Online Course
Supported by
* 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
False
Condition?
True
statements
while
# print 0,1,2,…9
i=0
while i < 10 :
print(i)
i=i+1
print(‘exit value; ‘, i)
value = 50
value = 100
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
#2
#3
num = 0
#3
num = 0
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
* 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.