0% found this document useful (0 votes)
19 views15 pages

CT11 Year10Slides

This document teaches about condition-controlled loops, specifically 'while' loops, in programming. It covers how to implement these loops in algorithms, code, and flowcharts, emphasizing their usefulness in repeating actions until a condition is met. Additionally, it introduces nesting of code, allowing for more complex programming structures within loops.

Uploaded by

M K Khaing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views15 pages

CT11 Year10Slides

This document teaches about condition-controlled loops, specifically 'while' loops, in programming. It covers how to implement these loops in algorithms, code, and flowcharts, emphasizing their usefulness in repeating actions until a condition is met. Additionally, it introduces nesting of code, allowing for more complex programming structures within loops.

Uploaded by

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

Y10-02-CT11:

Repetition (while)
Y10-02-CT11: Repetition (while)

Learning objectives
In this lesson you will learn to:

• use repetition (condition-controlled loops) in algorithms


• use repetition (condition-controlled loops) in code
• use repetition (condition-controlled loops) in flowcharts.

© 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.

Selection is about making decisions. Using selection a program


chooses a path through an algorithm based on certain conditions.

Looping is a way of executing parts of a program over and over


again, without rewriting the code. It can be used to repeatedly
execute code.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-02-CT11: Repetition (while)

Going loopy

Sometimes, you really do need to repeat yourself.

In programming it is often necessary to repeat steps a certain number


of times or until a condition is met.

There are two types of repeat loops: condition-controlled and count-


controlled.

You are going to focus on condition-controlled loops today.

© 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.

These conditions are the same as the types we looked at in


selection.

Let’s look at an example to see why this is so useful.

© 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.

You could do it like this: But this would be time


consuming and inefficient.
Instead you could use a
while loop like this:

© 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.

Let’s look at another example.

© 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)

1. Declare and initialise a variable to store the user’s choice.


2. Create a while loop that looks at what the user entered to see if it is ‘exit’.
3. If it’s not ‘exit’ ask the user to enter something.
4. Print what the user entered.
5. Loop back to Step 3.

© 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

output “no need


output “take an
for the umbrella
umbrella”
today”

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-02-CT11: Repetition (while)

In a flowchart start

Let’s use our userChoice example to see


userChoice = “”
how a while loop works in a flowchart.

userChoice ! No
= “exit”?

userChoice = "" Yes


while userChoice != "exit":
userChoice = input("Enter your choice: userChoice =
") user input
print("You chose:", userChoice)

output
userChoice

This is the ‘loop’.


It takes the program back end
to the condition.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-02-CT11: Repetition (while)

Nesting
It is common for programmers to need to put one piece of complex
code within another piece of code.

This is known as nesting – when one programming structure is put


inside another.

This is a very common procedure, so you’ll need to get to grips with


how it works.

© 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?

It would have to go inside the while loop.

© 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)

Wrap up, you have learned how to . . .


 Use condition-controlled loops.

 Use while loops in Python to repeat sections of code, saving


yourself the trouble of writing them over and over again.

 Represent a while loop using the decision symbol in a flowchart.

 Nest code – putting structures inside one another.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.

You might also like