Sequence Selection and Iterations WHILE Loops Python Practical
Sequence Selection and Iterations WHILE Loops Python Practical
Iterations
Programming Condition Controlled Loops
(WHILE Loop)
www.computerscienceuk.com
GCSE 9-1: OCR Computer Science
www.computerscienceuk.com
GCSE 9-1: OCR Computer Science
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
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.
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
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