CT11 Year10Slides
CT11 Year10Slides
Repetition (while)
Y10-02-CT11: Repetition (while)
Learning objectives
In this lesson you will learn to:
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-02-CT11: Repetition (while)
Programming structures
Previously you have learned about sequence and selection.
Remember that sequence is the order in which steps are carried out
– one after another.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-02-CT11: Repetition (while)
Going loopy
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-02-CT11: Repetition (while)
Condition-controlled loops
A condition-controlled loop is a structure that allows a block of steps
in code to be repeated while a condition is true.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-02-CT11: Repetition (while)
Example
Let’s imagine you wanted to print ‘hello world’ ten times.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-02-CT11: Repetition (while)
In detail
The ‘while’ keyword together with a condition is used
First create a
to control the loop.
variable that will
count how many This reads ‘while the value in the variable count is
times the code has less than 10…’
looped.
count = 0 Notice the colon
while count < 10: – just as in ‘if’
print("Hello world") statements.
count = count + 1
These lines are indented. This line adds one to the counter
This means they happen variable, indicating that a pass
inside the loop. They will be through the loop has been
repeated over and over. completed.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-02-CT11: Repetition (while)
However…
There’s much more to repetition than just counting.
You can use a condition-controlled loop to wait for the user to type
something in, for example a ‘Q’ to quit a program.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-02-CT11: Repetition (while)
Getting input
Imagine you are building a small part of a larger program.
You want your program to keep asking the user for an input until they type
‘exit’.
The input could be used for something else later on, but for now it will just be
printed out.
userChoice = ""
while userChoice != "exit":
userChoice = input("Enter your
choice: ")
print("You chose:", userChoice)
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-02-CT11: Repetition (while)
In a flowchart
There are no ‘while’ or ‘loop’ symbols in flowcharts.
How do you think you might implement a loop using a flowchart?
You have already learned how condition statements are used to
control program flow in if statements.
Where in a flowchart is a condition tested?
weather == No
“rain”?
In a decision Yes
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-02-CT11: Repetition (while)
In a flowchart start
userChoice ! No
= “exit”?
output
userChoice
Nesting
It is common for programmers to need to put one piece of complex
code within another piece of code.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-02-CT11: Repetition (while)
Nesting: an example
Let’s build some functionality into the user choice program.
If the user types in ‘hello’, you want the program to say ‘hello’ back.
Where would you add this ‘if’ statement?
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-02-CT11: Repetition (while)
Nesting: an example
The ‘if’ statement has been added so that the program says ‘hello’ back to
the user.
Notice that the statement is placed after the line that asks the user to type in
their choice. Notice that it is indented to match that line. This puts it inside the
loop.
Your next challenge will be to add more selection statements (using ‘elif’ and
‘else’) to provide additional options for the user.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-02-CT11: Repetition (while)
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.