Problem Solving:
Pseudocode
Prepared by: S. Lewis
Lesson Objectives
1. Identify the steps in the problem solving process
2. Explain the terms: algorithm, pseudocode
3. Differentiate between a variable and a constant
4. Identify data types and operators used in programming
5. Solve problems using pseudocode algorithm.
LET’S RECAP!
What is Pseudocode?
Pseudo means ‘fake’.
Pseudocode is a representation of a programming
code that uses English-type words, numbers and
special symbols to document an algorithm.
Pseudocode Terms and Symbols
Key Terms
★ Identifier
★ Variable
★ Constant
★ Input/ Read/Get
★ Output/ Display/ Print/ Write
★ Conditional Statements (IF, ELSE, ELSEIF
★ Loops (for, while, repeat-until)
IDENTIFIER
★ In the computer, values are stored in memory locations.
★ There are many memory locations. In order to keep track of where our
values are stored, we need to place a label or identifier in a particular
memory location.
★ The label or identifier is called a variable or constant.
VARIABLES
A symbolic named location in memory that stores a value. The value
changes during program execution.
Used for storing:
● Data provided by user
● Computations during processing
It is good practice to choose variable names that reflect the kind of data that is
being stored, e.g. num1, age1, sum, average etc.
CONSTANTS
A symbolic named location in memory that stores a value. The value
remains the same during program execution.
Useful when storing: Declaration
● Conversion rates Example: GCT = 17.5
● Tax rates
identifier
● Discount rates value
Identifier Naming Rules
Rule #1: Do not begin with a 1Age, 2Age Recommended:
digit/number Age1, Age2
Rule #2: Do not begin with an _Age, _name Recommended:
underscore Age_1, name_1
Rule #3: variable names are Age, age, AGE, aGE, AgE Recommended:
case sensitive are all different Use a consistent case
Rule #4: Special characters @, *, #, ^, &, %, $ and others
(except underscore)
are not allowed
Identifier Naming Rules
Rule #5: Blank or whitespaces not first name, age 1, Recommended:
allowed First_name, age_1,
Rule #6: no keywords If, then, else, while, float,
double etc.
Rule #7: avoid looong names totalitemsPurchased itemsP, Total, …..
Activity
Identify the constants and variables in the problem below. Circle the variables in
green and constants in red.
Susan owns and runs ‘British Virgin Islands’ (BVI) Boat Hire’ , that specializes in
renting out boats. There is a $3.50 charge per hour for rental. She needs a program
that reads in the number of hours that a boat has been rented, calculates and prints
the total cost.
Data Types
Data Type Description Examples
Integer Whole numbers, positive or 7,76, -45, -12
negative
Real Positive or negative decimal 12.6, -17.1, 3.1472
number (fractions)
Character A single character such as a letter ‘ G ’, ‘ # ’ , ‘ / ’
of the alphabet or punctuation.
String A collection of characters such as a “ABC”, “A+”, “Good Job!”
word or phrase or sentence.
Boolean Can contain only one of two possible TRUE or FALSE, YES or NO
values.
Activity
Suggest the most appropriate data type to store values related to EACH of the
following:
1. A person’s age
2. The weight of a piggy bank
3. The number of coins in a piggy bank
4. The total value of coins in a piggy bank
5. The number of items purchased.
6. The name of an item
7. Whether or not a piece of luggage is overweight
Mathematical Operators
Symbol Description Example
+ Addition 2+1
- Subtraction 6 -2
/ Divide By 54/2
* Multiplication 4*9
^ Raised to the power to 4^2
DIV Integer Division 17 DIV 5 = 3
MOD Remainder 17 MOD 5 = 2
Logical Operators
Symbol Description Example
= Equal to x = 35
> Greater than 8>5
< Less than 3<9
>= Greater than or equal to x>=8
<= Less than or equal to x<=60
<> Not equal to 40 <>34
Logical Operator
AND – compares two Boolean expressions.
If both expressions evaluate to True, then the AND operator returns
True. If either or both expressions evaluate to False, then AND
returns False.
AND
Expression A Expression B Result
False False False
False True False
True False False
True True True
Logical Operator
OR - The OR operator compares two Boolean expressions. If either
expression evaluates to True, OR returns True. If neither expression
evaluates to True, OR returns False.
OR
Expression A Expression B Result
False False False
False True True
True False True
True True True
Logical Operator
NOT - yields the opposite of the expression it evaluates. If the
expression evaluates to True, Not yields False; if the expression
evaluates to False, Not yields True
NOT
Expression Result
False True
True False
Prompting Statements
● Used to get information from the user interacting with the program
to enter the required data for the program to work.
● This is usually a message displayed to the screen.
● The keywords WRITE, OUTPUT, PRINT or DISPLAY can be used.
Prompting Statement
Example:
Susan owns and runs ‘British Virgin Islands’ (BVI) Boat Hire’ , that specializes in
renting out boats. There is a $3.50 charge per hour for rental. She needs a program
that reads in the number of hours that a boat has been rented, calculates and
prints the total cost.
Solution: WRITE “Enter the number of hours you rented the boat.”
Instruction in
keyword quotation marks
NB: Try to begin your instruction with the word Enter. Instructions should be clear for
understanding.
Try on your own
Write a PROMPTING statement for EACH of the following:
OUTPUT
Write an instruction to prompt the user to enter the
age of three students.
Write an instruction requesting the user to enter
the name of a book and its author
Input Statements
● Used to get data from outside the computer via an input device into a variable
for manipulation by the pseudocode.
● The keyword READ is used.
● Example:
Susan owns and runs ‘British Virgin Islands’ (BVI) Boat Hire’ , that specializes in
renting out boats. There is a $3.50 charge per hour for rental. She needs a program
that reads in the number of hours that a boat has been rented, calculates and prints
the total cost.
Solution: Read hours
Try on your own
Write an INPUT statement for EACH of the following:
INPUT
Input the name of a student
Input two numbers
Accept the name, age and height of a student
Read the make, colour and price of a car.
Input the price and quantity of an item
Output Statements
● Used to display the results of the pseudocode algorithm.
● The result is displayed with a label as shown on next slide.
● The keywords WRITE, OUTPUT, PRINT or DISPLAY can be used.
Output Statements
Example:
Susan owns and runs ‘British Virgin Islands’ (BVI) Boat Hire’ , that specializes in
renting out boats. There is a $3.50 charge per hour for rental. She needs a program
that reads in the number of hours that a boat has been rented, calculates and prints
the total cost.
Solution: PRINT “Total cost is” , total_cost
“label”
variable_name
Try on your own
Write an OUTPUT statement for EACH of the following:
OUTPUT
Write an instruction to print “Today is Monday”
Write an instruction to print “Total=”
Write an instruction to read a name and one to
output a name.
Assignment Statements
● Used to give a starting value to variables and to change the value assigned to a
variable.
● It has two parts location (variable name) and expression or value.
● It usually uses the equal (=) sign.
● Example:
Variable_name = expression or value Variable_name expression or
value
days =28
OR days 28
rate= 500
Rate 500
salary = days * rate
salary days * rate
Activity!
● Identify the assignment statements in the problem below.
Susan owns and runs ‘British Virgin Islands’ (BVI) Boat Hire’ , that specializes in
renting out boats. There is a $3.50 charge per hour for rental. She needs a
program that reads in the number of hours that a boat has been rented,
calculates and prints the total cost.
SEQUENCE
● When we write programs, we assume that the computer executes the program
starting at the beginning and working its way to the end.
● This is a basic assumption of all algorithm design.
● We call this SEQUENCE.
START BEGIN
Sequence of instructions; Sequence of instructions;
Sequence of instructions; Sequence of instructions;
OR
Sequence of instructions; Sequence of instructions;
Sequence of instructions; Sequence of instructions;
STOP END
PSEUDOCODE STRUCTURE
START or BEGIN
Initialize variable (where necessary)
Prompting statement
Input Statement
Assignment Statement (s)
Output Statement
STOP or END
PSEUDOCODE STRUCTURE
Write a pseudocode algorithm that calculates and prints the total of two numbers.
START
PRINT “ Enter two numbers”
READ num1, num2 BODY
total = num1 + num2
PRINT “ The total is” , total
TERMINATOR
STOP
Pseudocode Example
● Write a pseudocode algorithm for the problem below.
Susan owns and runs ‘British Virgin Islands’ (BVI) Boat Hire’ , that specializes in
renting out boats. There is a $3.50 charge per hour for rental. She needs a
program that reads in the number of hours that a boat has been rented,
calculates and prints the total cost.
Try on your own!
● Write a pseudocode algorithm to read three numbers and find their product,
sum and average.
Keep Practicing!