You are on page 1of 18

Control Structures

Repetitive Statements (for/while)

Sudharshan Welihinda(weli@iit.ac.lk)
Senior Lecturer, Department of Computing
Informatics Institute of Technology
Lecture 9
1
How To Determine If Loops Can Be Applied?

• Something needs to occur multiple times (generally it will


repeat itself as long as some condition has been met).
• Example 1: Flowchart
N
Play again?

Run game again

END GAME
Re-running the entire
program Pseudo code
While the player wants to play
Run the game again
slide 2 James Tam
Loops In Python
1. While
2. For

Characteristics:
1. The stopping condition is checked before the body executes.
2. These types of loops execute zero or more times.

slide 3 James Tam


The While Loop
• This type of loop can be used if it’s not known in advance how many
times that the loop will repeat (most powerful type of loop, any other
type of loop can be simulated with a while loop).

- It can repeat so long as some arbitrary condition holds true.

Syntax :

(Simple condition)

while (Boolean expression):


body

(Compound condition)

while (Boolean expression) Boolean operator (Boolean expression):


body

slide 4 James Tam


The While Loop (1)

1) Initialize control Let i = 0


•Program name: while1.py

i = 0
2) Check condition is No
while (i <= 3): i <= 3?
print("i =", i)
Yes
i = i + 1 3) Execute body
print i

print("Done!")
4) Update control
Let i = i + 1

Print message “Done!”

slide 5 James Tam


The While Loop (1)
i = 0
while (i <= 3):
Let i = 0
print("i =", i)
i = i + 1

No
print("Done!") Is i <= 3?

Execution : Yes

Print i
Variable i 0
is i <= 3? Yes
Let i = i + 1
Print i 0

i = i + 1 1

Print message “Done!”

slide 6 James Tam


The While Loop (1)
i = 0
while (i <= 3):
Let i = 0
print("i =", i)
i = i + 1

No
print("Done!") Is i <= 3?

Execution : Yes

Print i
Variable i 0 1 2 3 4
is i <= 3? Yes Yes
Let i = i + 1
Print i 0 1

i = i + 1 1 2

Print message “Done!”

slide 7 James Tam


The While Loop (1)
i = 0
while (i <= 3):
Let i = 0
print("i =", i)
i = i + 1

No
print("Done!") Is i <= 3?

Execution : Yes

Print i
Variable i 0 1 2 3 4
is i <= 3? Yes Yes Yes
Let i = i + 1
Print i 0 1 2

i = i + 1 1 2 3

Print message “Done!”

slide 8 James Tam


The While Loop (1)
i = 0
while (i <= 3):
Let i = 0
print("i =", i)
i = i + 1

No
print("Done!") Is i <= 3?

Execution : Yes

Print i
Variable i 0 1 2 3 4
is i <= 3? Yes Yes Yes Yes
Let i = i + 1
Print i 0 1 2 3

i = i + 1 1 2 3 4

Print message “Done!”

slide 9 James Tam


The While Loop (1)
i = 0
while (i <= 3):
Let i = 0
print("i =", i)
i = i + 1

No
print("Done!") Is i <= 3?

Execution : Yes

Print i
Variable i 0 1 2 3 4
is i <= 3? Yes Yes Yes Yes No
Let i = i + 1
Print i 0 1 2 3

i = i + 1 1 2 3 4

Print message “Done!”

slide 10 James Tam


The While Loop (2)

•Program name: while1.py

i = 1 1) Initialize control
while (i <= 3):
2) Check condition
print("i =", i)
3) Execute body
i = i + 1
print("Done!")

4) Update control

slide 11 James Tam


The While Loop (4 & 5)
• Write a Python program that prints all the numbers from 0 to 6.

i = 0
while (i <= 6):
print("i =", i)
i = i + 1
print("Done!")

• Write a Python program that prints all the numbers from 0 to 6 except 3 and 6.

i = 0
while (i <= 6):
if ((i == 3) or (i == 6)):
print(‘’)
else
print("i =", i)
i = i + 1

slide 12
print("Done!") James Tam
• What is the output of the following code?

x=0
while (x < 10):
x=x+1
print(“Hi..”)

while (x < 8):


x=x+1
print(“Hello..”)

1. ‘Hi’ is printed 8 times, ‘Hello’ 7 times and then ‘Hi’ 2 times


2. ‘Hi’ is printed 10 times, ‘Hello’ 0 times
3. ‘Hi’ is printed once, ‘Hello’ 7 times
4. ‘Hi’ is printed once, ‘Hello’ 7 times and then ‘Hi’ 2 times

slide 13 James Tam


The following code segment is to print the message ‘Hello World’ 4 times. An instruction
(a statement) is missing from the following code segment. What is the missing
instruction and where does it belong in the code?

intNumber = 1

while (intNumber < 5) :

print(“Hello World…”)

If it is needed to print the message 5 times, what change(s) need to be done for the
above code segment?

slide 14 James Tam


Countdown Loop
•Program name: while2.py

i = 3

while (i > 0):


print("i =", i)
i = i – 1

print("Done!")

slide 15 James Tam


Tracing The Count Down While Loop
Execution Variable
>python while2.py i

slide 16 James Tam


Common Mistakes: While Loops

Forgetting to include the basic parts of a loop.


• Updating the control
i = 1

while(i <= 4):

print("i =", i)

slide 17 James Tam


Practice Exercise
•The following program that prompts for and displays the
user’s age.
•Modifications:
- As long as the user enters a negative age the program will continue
prompting for age.
- After a valid age has been entered then stop the prompts and display the
age.

age = int(input("Age: "))


print(age)

slide 18 James Tam

You might also like