0% found this document useful (0 votes)
27 views23 pages

Pseudo Code

Uploaded by

marsels.0.loza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views23 pages

Pseudo Code

Uploaded by

marsels.0.loza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Objectives

• Define data types integer, real, Boolean, character,


string
• Define Boolean operators
• Understand and use sequence, selection and
iteration in an algorithm
• Write algorithms in pseudocode involving sequence,
selection and iteration
Pseudocode

Data types
• You will use the following data types in your
algorithms:

Data type Description Example

INTEGER a whole number 1475, 0, -5

REAL a number with a decimal point 56.75, 6.0, -2.456, 0.0

BOOLEAN Can only be TRUE or FALSE TRUE, FALSE


A single alphabetic or numeric
CHARACTER ‘a, ‘K’, ‘4’, ‘@’, ‘%’
character
One or more characters
STRING ‘Jo Hobson’, ‘123’
enclosed in quote marks
Pseudocode

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

The switch/case statement


• The switch/case statement is used if there are
several possible options to be tested
switch optionChosen:
case 1:
print (“You chose option 1”)
case 2:
print (“You chose option 2”)
case 3:
print (“You chose option 2”)
endswitch
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

Pseudocode for assignment


• To assign a value to a variable, you can write
statements such as
total = 0
cost = adult * 2 + child * 3
counter = counter + 1
• How would you write a statement to subtract
discount from markedPrice to give salePrice?
Pseudocode

Pseudocode for input/output


• Most programs that you will write will ask the user to
enter some data, and then accept the user input and
assign it to a variable
• The pseudocode used in an exam will look like this:
firstname = input (“Please enter your name”)
• This is equivalent to two statements:
print (“Please enter your name”)
firstname = input
Pseudocode

Inputting data - examples


Write a single statement to do each of the following:
• Display a prompt “How old are you?”
Accept an answer from the user
Assign the answer to a variable called age

• Display a prompt “Press Enter to continue”


When the user presses Enter, continue to the next
statement
Pseudocode

Inputting data - Answer


Write a single statement to do each of the following:
• Display a prompt “How old are you?”
Accept an answer from the user
Assign the answer to a variable called age
age = input (“How old are you?”)
• Display a prompt “Press Enter to continue”
When the user presses Enter, continue to the next
statement
x = input (“Press Enter to continue”)
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

Yes print “Merit”


mark >=70?

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

• Some languages (e.g. Python) do not have the


do … until statement
Pseudocode

for … next loop


• Use this when you want to execute the loop a
specific number of times
total = 0
for counter from 1 to 7
maxTemperature = input(“enter max temperature: ”)
add maxTemperature to total
next counter
averageWeeksTemp = total / 7
print (“This week’s average is ”, averageweekstemp)
Pseudocode

while … end while


• use this when you want to execute the loop while a
certain condition is true.
emailaddress = input (“Enter email
address:”)
while emailaddress does not contain “@”
print (“invalid address – please re-
enter”)
emailaddress = input (“Enter email
address:”)
endwhile
print (“thank you”)
• the condition is tested at the beginning of the loop
• how many times will the loop be executed if the user
Pseudocode

Flowchart while … end while


emailAddress = input (Please enter Input
emailAddress

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 “@”

You might also like