0% found this document useful (0 votes)
2 views18 pages

05 Iteration (Condition Controlled) - Procedural Python

This document provides an overview of condition-controlled iteration using WHILE loops in Python. It explains the concept of iteration, the structure of WHILE loops, and includes examples of how to implement them in code. Additionally, it outlines lesson objectives and activities for practicing WHILE loops.

Uploaded by

d
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)
2 views18 pages

05 Iteration (Condition Controlled) - Procedural Python

This document provides an overview of condition-controlled iteration using WHILE loops in Python. It explains the concept of iteration, the structure of WHILE loops, and includes examples of how to implement them in code. Additionally, it outlines lesson objectives and activities for practicing WHILE loops.

Uploaded by

d
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/ 18

8.

3 Python:
Condition Controlled Iteration – WHILE Loop

Computer Science UK www.computerscienceuk.com


Starter

Study the code below…

What do you think the code will do?


Can you explain how it works?

Computer Science UK www.computerscienceuk.com


Lesson Objectives
Lesson Objectives
• Understand what an iteration is
• Understand how to program WHILE loops

Lesson Outcomes
• Carry out a number of programming tasks to practice
programming the WHILE loop construct
Literacy – Key Words

Iteration A programming construct, where code is executed repeatedly, either for a set number
of times or based on a conditional.
WHILE Loop A programming construct which enables a program to repeatedly execute code, whilst
a conditional evaluates to TRUE.

Computer Science UK www.computerscienceuk.com


Iterations in Python
Iterations in Python

The word iterate mean loop.

Iterations are therefore loops and in the python language there are two types of loop.

For Loops While Loops

These are Count Controlled These are Condition Controlled

(they run for a set number of times) (they run whilst a condition is True)

Computer Science UK www.computerscienceuk.com


WHILE Loops in Python
‘WHILE Loops’ in Python

While Loops are set up using the following statement:

while x <
==
>
!=
<>
>=
<= 0:
These are called conditions
Let's take a look more closely…

Computer Science UK www.computerscienceuk.com


While Loops in Python
‘While Loops’ in Python
The x is simply a variable. It could have any
name.
We must
It is however a special kind of variable known as finish the
the ‘most recent value’ statement
Start with a colon
==
X=0 while x !=
> n:
Execute
<
Command

The n is simply representing a value that we want x to either equal,


No
Does not equal, be greater than etc depending on the while loop
‘x’ = 5? condition.

If n=5 and the condition was while x != 5 (not equal to 5) then the
Yes
loop would repeat until x equals 5.

Computer Science UK www.computerscienceuk.com


While Loops in Python
‘While Loops’ in Python

x=0 Hello World

while x == 0: Hello World


Hello World

print(“Hello World”) Hello World


Hello World
Hello World
Hello World
This is an example of a WHILE loop.

And this while loop in particular will repeat forever


as the condition will always evaluate to true!

Let’s see if you can code a while loop…

Computer Science UK www.computerscienceuk.com


While Loops in Python
‘While Loops’ in Python

x=0 Hello World

while x == 0: Hello World


Hello World

print(“Hello World”) Hello World


Hello World
Hello World
Hello World
This is an example of a WHILE loop.

And this while loop in particular will repeat forever


as the condition will always evaluate to true!

Let’s see if you can code a while loop…

Computer Science UK www.computerscienceuk.com


While Loops in Python
On your Python IDLE (Thonny/repl.it)…

Write the code to repeat the word


“bananas”, forever

bananas
bananas
bananas
bananas
bananas
bananas
bananas

Computer Science UK www.computerscienceuk.com


While Loops in Python
On your Python IDLE (Thonny/repl.it)…

Write the code to repeat the word


“bananas”, forever

bananas
bananas
bananas
bananas
x=0
bananas while x == 0:
bananas
bananas
print(“bananas”)

Computer Science UK www.computerscienceuk.com


While Loops in Python
On your Python IDLE (Thonny/repl.it)…

Write the code to repeat the word


“cheese”, forever

cheese
cheese
cheese
cheese
x=0
cheese while x == 0:
cheese
cheese
print(“cheese”)

Computer Science UK www.computerscienceuk.com


While Loops in Python
On your Python IDLE (Thonny/repl.it)…

Write the code to repeat the word


“cheese”, forever

cheese
cheese
cheese
cheese
cheese
cheese
cheese

Computer Science UK www.computerscienceuk.com


While Loops in Python
‘While Loops’ in Python This is another example of a WHILE loop.

And this while loop in particular will repeat while x


51
doesn’t equal 5.
-5
1220
x=0
As soon as a 5 is entered, the loop will end…
while x != 5:
x = int(input(“Please type in a number”))

Please type in a number: 1


print(“Loop has ended”)
Please type in a number: -5

Please type in a number: 122


Remember:
Please type in a number: 5
If you create a condition where a variable is being
checked against an integer, you must remember to Loop has ended
convert the variable’s input into an integer (e.g. int())

Computer Science UK www.computerscienceuk.com


While Loops in Python
On your Python IDLE (Thonny/repl.it)…

Write the code which will continue to ask the


user for a number until they type in the
number 100.
Please type in a letter: 4

Please type in a letter: 88

Please type in a letter: -22

Please type in a letter: 100

Loop has ended

Computer Science UK www.computerscienceuk.com


While Loops in Python
On your Python IDLE (Thonny/repl.it)…

Write the code which will continue to ask the


user for a number until they type in the
number 100.
Please type in a letter: 4

Please type in a letter: 88


x=0
Please type in a letter: -22
while x != 100:
x = int(input(“Please type in a number”)) Please type in a letter: 100

Loop has ended


print(“Loop has ended”)

Computer Science UK www.computerscienceuk.com


While Loops in Python

On your Python IDLE (Thonny/repl.it)…

Write the code which will continue to ask the user


for a letter until they type in the letter ‘s’.
Please type in a letter: a

Please type in a letter: x

Please type in a letter: t

Please type in a letter: s

Loop has ended

Computer Science UK www.computerscienceuk.com


While Loops in Python

On your Python IDLE (Thonny/repl.it)…

Write the code which will continue to ask the user


for a letter until they type in the letter ‘s’.
Please type in a letter: a

Please type in a letter: x


letter = “ ”
while letter != “s”: Please type in a letter: t

letter = input(“Please type in a letter”) Please type in a letter: s

Loop has ended


print(“Loop has ended”)

Computer Science UK www.computerscienceuk.com


Lesson Activity

• Open the PRIMM challenges on using WHILE Loops in


Python.

• Complete as least FIVE challenges and save your files


in your Z Drive. Do not forget to add print screens to
your PRIMM Task as well!

Computer Science UK www.computerscienceuk.com

You might also like