You are on page 1of 16

PSEUDOCODE:

LOOPS
REVIEW OF LOOPS
•What is a loop?
•What are the steps for
writing a “FOR” loop in
pseudocode?
HOMEWORK QUESTION 1:
For 20 employees, input
a salary and a bonus.
The total salary is the
salary plus the bonus.
Print the total salary.
STEP 1: Identify the counter QUESTION:

Counter: employee For 20 employees, input


a salary and a bonus.
Iterations: 20 The total salary is the
salary plus the bonus.
STEP 2: Determine IPOs Print the total salary.
I : salary, bonus RESULT:
P: total_salary = salary+bonus FOR employees 1 to 20
O: PRINT pay DO
INPUT salary
INPUT bonus
STEP 3: Apply “FOR” Statement total_salary =
FOR Number of Counts DO salary + bonus
Actions PRINT total_salary
ENDFOR
ENDFOR
HOMEWORK QUESTION 2:
For 12 patrons in a library,
input the days overdue
and the daily overdue
rate. Calculate and output
the fees owed for an
overdue book.
STEP 1: Identify the counter QUESTION:
Counter: patrons For 12 patrons in a library,
input the days overdue and
Iterations: 12
the daily overdue rate.
Calculate and output the
STEP 2: Determine IPOs fees owed for an overdue
I : days_overdue, rate book.
P: overdue_fee = RESULT:

days_overdue * rate FOR employees 1 to 5 DO


O: PRINT overdue_fee
INPUT days_overdue
STEP 3: Apply “FOR” Statement INPUT rate
FOR Number of Counts DO
overdue_fee =
Actions
days_overdue * rate
ENDFOR
PRINT overdue_fee
LOOPS
• Allows one or more actions to be
repeated.
• Each repetition is called an ITERATION.
• There are two types of loops, the
difference between them is how the loop
stops.
• All loops must include a way of stopping.
1) COUNTER-CONTROLLED LOOPS
• The loop stops after a fixed number
of iteration.
• Must know HOW MANY TIMES the
loop needs to be repeated.
• FOR Statements
2) CONDITION-CONTROLLED LOOPS
• A condition needs to be met to stop the loop

• This type of loop is stopped by a Yes/No or


True/False test.
• The number of repeats is unknown.

• Two types of condition-controlled loops:


WHILE – condition tested at beginning
REPEAT – condition tested at end
STEPS FOR WRITING A “WHILE” LOOP IN PSEUDOCODE

STEP 2: Determine IPOs Remember:


I: Statement structure:
P: • Capital letters
O: Keywords:
• Capital letters
STEP 2: Apply “WHILE” Statement
Variables:
INPUT
• Common letters
WHILE Conditional Test DO
with no spaces e.g.
Actions if False
variable_name
INPUT
Actions:
ENDWHILE
• Always indented
Actions if True
Ask what is the password. If
the password is “Sesame”
then output “Correct
Password.” If the password
is not “Sesame” then output
“Wrong password, try again”
and accept a password.
STEP 2: Determine IPOs QUESTION:
Ask what is the password. If the
I: password is not “Sesame” then
P: output “Wrong password, try
again” and accept a password. If
O: the password is “Sesame” then
output “Correct Password.”

STEP 2: Apply “WHILE” Statement RESULT:


INPUT INPUT
WHILE Conditional Test DO WHILE Conditional Test DO
Actions if False Actions if False
INPUT INPUT
ENDWHILE
ENDWHILE
Actions if True
Actions if True
STEP 2: Determine IPOs QUESTION:
Ask what is the password. If the
I : password password is “Sesame” then
P: password <> “Sesame” output “Correct Password.” If the
password is not “Sesame” then
O: If False: “Wrong password. output “Wrong password, try
Try again.” again” and accept a password.

If True “ Correct password.” RESULT:


INPUT password
STEP 2: Apply “WHILE” Statement WHILE password <> “Sesame” DO
INPUT OUTPUT “Wrong
WHILE Conditional Test DO password. Try again.”
Actions if False INPUT password
INPUT ENDWHILE
ENDWHILE OUTPUT “Correct password.”
Actions if True
Output “What is 40 + 60?”.
Input an answer. If the
wrong answer is entered,
output “No, try again” and
repeat the actions. If the
correct answer is input say
“Yes, well done and stop.
Print: Enter a number
greater than 10. Accept an
answer. If the answer is less
than 10, print: No, try again.
If the answer is greater than
10, print: Good job!
THE END

You might also like