Pseudo Code
Pseudo Code
Data types
• You will use the following data types in your
algorithms:
Boolean operators
• The following operators are used to compare two
values
> greater than
>= greater than or equal
< less than
<= less than or equal
= equal (in Python this is written ==)
<> not equal (in Python this is written !=)
Pseudocode
Boolean expressions
• A Boolean expression evaluates to TRUE or FALSE
• Which of the following expressions are TRUE?
(i) 35 >= 5 * 7
(ii) ‘A’ < ‘B’
(iii) 100 <= 10**2
(iv) 25 > 2 * 5
(v) 2 ^ 4 = 16
(vi) n ^ 2 = n * 2
Pseudocode
Boolean expressions
• A Boolean expression evaluates to TRUE or FALSE
• Which of the following expressions are TRUE?
(i) 35 >= 5 * 7 True
(ii) ‘A’ < ‘B’ True
(iii) 100 <= 10**2 True
(iv) 25 > 2 * 5 True
(v) 2 ^ 4 = 16 True
(vi) n ^ 2 = n * n True
Pseudocode
Programming constructs
• There are three basic ways of controlling the order in
which program statements are carried out
• You have already used examples of all of them:
• Sequence
• Selection
• Iteration
Pseudocode
Sequence
• The statements are executed in the order they are
written
mealCost = 4.00
drinkCost = 2.00
total = mealCost + drinkcost
Pseudocode
Selection
• An IF statement is a selection statement
• The next statement to be executed depends on
whether the condition being tested is true or false
hoursPerNight = (“How many hours a night do
you sleep?”)
if (hoursPerNight < 8) then
OUTPUT (“That’s not enough!”)
else
OUTPUT (“That’s plenty!”)
endif
Pseudocode
Introducing pseudocode
• Pseudocode is a kind of structured English for
describing algorithms
• It allows the designer to focus on the logic of the
algorithm without being distracted by the syntax of
the programming language
• You will see pseudocode statements written in a
consistent style in exam questions, but you can use
alternative statements so long as the meaning is
clear
Pseudocode
Writing pseudocode
• Write pseudocode for a program which asks the user
to enter the cost of two items, adds the two costs
and if the cost is greater than £10.00, displays a
message “Sorry, too much”.
Otherwise it displays
the change due
from £10.00
Pseudocode
Pseudocode solution
item1 = input (“Please enter price of first
item:”)
item2 = input (“Please enter price of second
item:”)
total = item1 + item2
if total > 10 then
print (“Sorry, too much”)
else
change = 10 – item1 - item2
print (“Change from £10.00 is £”, change)
endif
Pseudocode
Flowchart or pseudocode?
• Write pseudocode corresponding to this flowchart:
Input mark
No
Yes
mark >=50? print “Pass”
No
print “Fail”
Pseudocode
Iteration
• Iteration means repetition
• There are three types of iteration statement in most,
but not all, imperative programming languages such
as Pascal, Visual Basic or Python
• for … next
• while … endwhile
• do … until
email
address”) emailAddress
Yes
while emailAddress does not contain contains @?
“@” No
emailAddress = input print “Invalid
address – please
(“Invalid re-enter”
address – please re-
enter”) Input
emailAddress
endwhile
print (“Thank you”)
Pseudocode
do … until
• Use this when you want to execute the loop until a
certain condition is true
• The condition is tested at the end of the loop
• Can you rewrite this algorithm using a do loop
instead of a while loop?
emailAddress = input (Please enter email
address”)
while emailAddress does not contain “@”
emailAddress = input (“Invalid address
– please
re-
enter”)
endwhile
Pseudocode
Using do … until
• The condition is not tested until the end of the loop
(it is always executed at least once), so you need an
if statement as well as the do loop
do
emailAddress = input (Please enter email
address”)
if emailaddress does not contain “@” then
emailAddress = input (“Invalid
address –
please re-enter”)
endif
until emailAddress contains “@”