0% found this document useful (0 votes)
14 views

Sequence Selection and Iterations WHILE Loops Python Practical

I don't know, just saw it

Uploaded by

07fayeni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Sequence Selection and Iterations WHILE Loops Python Practical

I don't know, just saw it

Uploaded by

07fayeni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

GCSE 9-1: OCR Computer Science

Iterations
Programming Condition Controlled Loops
(WHILE Loop)

www.computerscienceuk.com
GCSE 9-1: OCR Computer Science

Iterations – understanding WHILE loops


Learning
Objectives:
Introduction
To understand…

…the use of the three basic


We were introduced to iterations last lesson and
programming constructs used to saw how we could program a count controlled loop
control the flow of a program:
• sequence in python (FOR Loop).
• selection
• iteration (count and condition
controlled loops)
Today we shall look at another type of iteration.

While loops will repeated whilst a certain condition


is true.

It is therefore known as a condition control loop.

We shall now look to see how these can be


programmed in Python.

www.computerscienceuk.com
GCSE 9-1: OCR Computer Science

While Loops in Python


Learning
Objectives: ‘While Loops’ in Python
To understand…

…the use of the three basic


programming constructs used to
control the flow of a program:
• sequence
• selection
While Loops are set up
• iteration (count and condition
controlled loops) using the following
Start statement:
X=0

Execute
<
==
!=
>
<>
>=
<=
whileThese
x are 0:
Command

called conditions
Does No
‘x’ =
5?

Yes
Lets take a look more
closely… www.computerscienceuk.com
GCSE 9-1: OCR Computer Science

While Loops in Python


Learning
Objectives: ‘While Loops’ in Python
To understand… The x is simply a variable. It could We must
…the use of the three basic have any name. finish the
programming constructs used to
control the flow of a program: statement
• sequence
• selection
It is however a special kind of with a
• iteration (count and condition
controlled loops)
variable known as the ‘most recent colon
value’
Start ==

X=0
while x != n:
>
Execute <
Command
The n is represents a value that we want x to
Does No either equal, not equal, be greater than, etc.
‘x’ =
5?
depending on the condition we want to use.

E.g. n=5 and the condition while x != 5 (not


Yes
equal to 5) then the loop would repeat until x
equals 5. www.computerscienceuk.com
GCSE 9-1: OCR Computer Science

While Loops in Python


Learning
Objectives: ‘While Loops’ in
To understand…

…the use of the three basic


Python
programming constructs used to
control the flow of a program:
• sequence
• selection

x=0
• iteration (count and condition
controlled loops)

Hello World
while x == 0:
print(“Hello World”)
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

www.computerscienceuk.com
GCSE 9-1: OCR Computer Science

While Loops in Python


Learning
Objectives: ‘While Loops’ in
To understand…

…the use of the three basic


Python
programming constructs used to
control the flow of a program:
• sequence
• selection
• iteration (count and condition x=0
controlled loops)
while x != 5:
x = int(input(“Please type in a
Please type in a number: 1
number”))
Please type in a number: -5
Please type in a number: 122
print(“Loop has ended”)
Please type in a number: 5 Remember:
If you create a condition where a variable
Loop has ended
is being checked against an integer, you
must remember to convert the variable’s
input into an integer (e.g. int())

www.computerscienceuk.com
GCSE 9-1: OCR Computer Science

a t i o n s
o n s t r
Dem

www.computerscienceuk.com
GCSE 9-1: OCR Computer Science

Commenting
# Starting a line with a hash will turn your text
into a comment.
(It will be ignored by the program)

www.computerscienceuk.com
GCSE 9-1: OCR Computer Science

Tasks
WHILE LOOP TASKS:

1.Write a program that asks the user to input a series of numbers and
adds them to a total until the user enters zero. (This stopping value is
often called a rogue value).

2.Write a program which asks the user to set a password and then
asks the user to enter the password again. Program it so that if the
second password doesn’t match the one set, the request to enter the
password is repeated until it does match the set password.

3.Write a program that asks the user for a number between 10 and 20
and validates (which means ‘tests’) that the input is within the correct
range. It should repeatedly ask the user for a number from this range
until the input is within the valid range.
www.computerscienceuk.com

You might also like