Introduction to
Pseudocode
Algorithm design and problem solving 1
❑ Pseudocode ❑ Identifiers, variables
❑ Arithmetic operators and constants
❑ Logical operators ❑ Joining different data
❑ Boolean operators ❑ Maintainable program
❑ Primitive data types ❑ Sequence
Topics covered ❑ Input, Process, Output, ❑
Storage ❑
Testing - normal
Comments in
❑ Input/output pseudocode
❑ Flowcharts ❑ Selection statement –
IF
❑ Declare, assignment,
update ❑ Trace tables
2
Pseudocode Q1
Pseudocode: a simple method of showing an algorithm; it describes what the
algorithm does by using English key words that are very similar to those used in
high-level programming language but without strict syntax rules
3
Arithmetic Operators
A symbol that performs a mathematical function, e.g. '+' adds two values together.
+ - * / ^
Addition Subtraction Multiplication Division Raised to the
power of
Answer num1 + num2 Answer num1 - num2 Answer num1 * num2 Answer num1 / num2 Answer num1 ^ num2
Exam technique note: the result of these
Logical Operators
operations is always of data type BOOLEAN
A symbol that performs a comparison resulting in a true or false. Can be equals, not equal to,
less than, less than or equal to, greater than or greater than or equal to.
= < <= > >= <>
Equal to Less than Less than or equal Greater than Greater than or Not equal to
to equal to
IF x = y IF x < y IF x <= y IF x > y IF x >= y IF x <> y
THEN THEN THEN THEN THEN THEN
Boolean Operators Q2-6
Used to join multiple relational comparisons. AND, OR or NOT. Always results in Boolean type.
AND OR NOT
Returns TRUE when both Returns TRUE when one or Returns TRUE if the input is
inputs are true both inputs are true false and vice versa
IF A = B AND A = C IF A = B OR A = C Rarely used at IGCSE
Q7-8
Primitive Datatypes
The characteristics of a piece of data. Primitive data types include string, integer, real and Boolean.
17 12.45 F 3m TRUE 25/02/1982
Integer Real Char String Boolean Date
Any whole number Any decimal number A single character e.g. Any characters TRUE or FALSE A valid calendar date.
including zero. “A”, “1”, “*”. including letters, Often in dd/mm/yyyy
numbers and/or format, but can vary.
symbols.
Q9 Algorithms/programs use this system:
• Data is entered into a system/algorithm.
• A process is applied to that data.
Input, process, storage, output • Data is stored.
• An output is produced
Input Process Output
Storage
Primary
Storage
Secondary Storage Offline
Q10
Input, process, storage, output
The user entering data into the program The tasks that need to be performed by a Data that is displayed to the user usually
usually from a keyboard. program using the input data and any other on-screen.
previously stored data.
Input Process Output
Storage
Data that needs to be stored in files on an appropriate media for use in the future.
Q11
Exam technique note: you cannot separate
multiple INPUTS with a comma, but you can
Input and output
concatenate OUTPUTS
Input Process Output
When getting user to input in pseudocode: When getting user to output in pseudocode:
INPUT Name OUTPUT “Full name: “, Name, “Age:”, Age
Storage Age
INPUT
Primary
When getting user input in flowcharts: Storage
When getting user input in flowcharts:
Secondary Storage
INPUT Name
INPUT Age Offline
OUTPUT “Full name: “,
Name, “Age: ”, Age
10
Input, process, output (flowchart symbols) Q12-13
Terminator: You always need a START and often need an END on your diagram.
Input/Output: You can include multiple inputs on new lines within the same symbol if they are input at the same time.
Output must display exactly what will be output
Process: any processes or calculations are represented by these.
Decision: Use this to represent conditions in selection or loops. You will need at least two arrows leaving a decision
symbol.
Subroutine: Used to represent procedures or functions.
Flow line: You MUST ALWAYS use an arrow and the arrowhead points in the direction of data flow.
Flowchart: a diagram that shows the steps required for a task (sub-system) and the order in Offline
which the steps are to be
performed.
Secondary Storage 11
Q14
Declare, Assign and update
Declare Assign Update
Specifies the properties of a Storing a value in a memory location is Changing the value in a memory
variable such as data type and its called assigning a value to a variable. location with a new value is updating a
identifier (name). We use a left pointing arrow . variable.
DECLARE Age : INTEGER Age 18 Age Age + 2
Assignment
A type of programming statement that sets or resets the data in a variable or constant.
a ← 12 // variable a is set to the value 12
• The assignment operator is ←
• Assignments should be made in the following format: Examples of Assignment
Counter ← 0
• <identifier> ← <value> Counter ← Counter + 1
TotalToPay ← NumberOfHours * HourlyRate
IDENTIFIER: a name given to a variable, constant, data structure (array) or subroutine. 13
Q15
Identifier Rules
Identifiers are used for naming variable, constants and arrays amongst many other things. There are
some important rules for identifiers:
1. PascalCase – capitalise each word with no spaces between words
2. Cannot begin with a number, BUT a number (0-9) can be used after the start
3. Only the 26 English letters can be used (A-Z and a-z)
4. Should be descriptive of the data being stored
5. Single letters can only be used in reference to indexes of arrays or coordinate values 14
Exam technique note : It is good practice
Q16-25 Variables and constants
to use constants if this makes the
pseudocode more readable, and easier to
update if the value of the constant
changes.
Variables Constants
A named memory location that can store A named memory location that can store data.
data. The data can change whilst the The data cannot change whilst a program is
program is running running.
Examples of how to make the declaration
Integer : DECLARE Counter : INTEGER CONSTANT Pi ← 3.14
Real : DECLARE TotalToPay : REAL CONSTANT HourlyRate ← 6.50
String : DECLARE Name : STRING CONSTANT DefaultText ← "N/A" 15
Q26-27
OUTPUT - Joining different data items
Concatenation – joining 2 or more different data items together e.g a string and a variable
Output messages go in “speech marks”.
Commas ( , ) are used to join data of different types e.g., a string and integer. Ampers And ( & ) is used for strings only.
Average ← 18
OUTPUT “The average is ”, Average (Output – The average is 18)
StudentName ← “Kenneth”
OUTPUT “Hello ” & StudentName (Output – Hello Kenneth)
16
Maintainable program Q28
Maintainable program is a program that has key features to help it be understood later such as:
• Meaningful, consistent, and unique identifiers throughout:
• Identifiers should relate to the data being stored within the variable, constant, array, or subroutine
• Identifiers should follow rules stated previously, including PascalCase – Countdown is not the same as
CountDown
• Use of suitable comments that explain certain sections of code:
• Comments can be single or multi line depending on the situation
• Comments should not state the obvious, but provide context for other programmers to understand 17
A programming construct. Instructions are run once and in the order they are written.
Sequence Q29
Pseudocode and flowcharts allow you to represent your commands in order. This is the order that the commands are carried out.
1. David dreamed of finding gold
David the pirate has always dreamed of finding gold. In the end David 2. David found a treasure map
was disappointed they didn’t find the gold but happy that an adventure 3. David read the treasure map
4. David jumped in the ship and set sail for the land
was had. Yesterday, David found a treasure map. After reading the map,
Can you work out what themarked
order of“X”events that happed in
David jumped in the ship and set sail for the land marked with a big 5. David
David’sbegan
story?to dig
“X”. David found a chest that was filled with sand. Before that when 6. David’s shovel hit something hard
David reached land David began to dig. Suddenly, David’s shovel hit 7. David found a chest filled with sand
something hard. It was a large wooden chest. 8. David was disappointed no gold was found but glad to
have an adventure 18
Exam technique note : every flowchart has a
Sequence - flowchart
stat and end symbol (box with rounded
corner)
The sequence in a flowchart is represented by the flowline (arrows). A typical flowchart goes down the page.
When writing an algorithm, it is important to think about what order 1.
Start
Can you put the algorithm in
the commands need to be carried out in.
e.g
the correct sequence?
INPUT name
OUTPUT “the
users name
2. Can you explain why it must be
the sequence you have chosen?
is: “, Name
OUTPUT “the
users name
3. What does this algorithm do?
is: “, name
INPUT Name
End
19
Q30
Sequence - pseudocode
The sequence in pseudocode is represented by moving down the page in order.
When writing an algorithm, it is important to think about what order
the commands need to be carried out in. 1. Canvalue1
INPUT you put the algorithm in the correct
INPUT value2
e.g sequence?
INPUT value 3
Average ← Total/3
INPUT Value1 2. Can←value1+value2+value3
Total you explain why it must be the
INPUT Value2 average ←total/3
INPUT Value3 sequence
OUTPUT “the youaverage
have chosen?is: “,
OUTPUT “The average is: “, Average
Total ← Value1+Value2+Value3 3. What does this algorithm do?
average
20
Q31-37
Testing
How can we check a program works? With testing
What do we need to test? Data that should be accepted by the system
Value1 Value2 Total Output
Normal test data – this is data that INPUT Value1
6 9
should be accepted by the program and INPUT Value2
15
allows you to check that your solution Total ← Value1 + Value2
15
OUTPUT Total
calculates what you expect it to. In this
example you could choose any two values
e.g. Value1 = 6 and Value2 = 9
You stand up because it is
greater than or equal to. The
Selection – If //Statement 1
IF Age >= 16 THEN
condition is met
statements
Stand up
ELSE
ENDIF
Sit on the floor
Q38-39
A selection statement is a programming
construct using IF or CASE. A condition is //Statement 2
checked, and this determines which code is IF Raining = TRUE THEN
OUTPUT “take an umbrella”
run or not run. ENDIF
An IF statement is a type of selection What happens if it is not
nothing
construct where the result of the condition is raining?
either true or false
This is a comment. We will cover those
in a few slides time.
Selection – If //checks the age of a child and
outputs which school they would go to
statements IF mark >= 85 THEN
IF 2024 - YearBorn >=11 THEN
OUTPUT “A*”
OUTPUT “Senior Student”
ELSEELSE
IF mark IF
>= 752024
OUTPUT
THEN - YearBorn >=6 THEN
OUTPUT “A”
“Junior Student”
Q40
Indentations are VERY important. They show ELSEELSE
IF mark IF 2024 - YearBorn >=3 THEN
>= 68 THEN
dependence on a previous statement. THEN OUTPUT “Infant Student”
ELSE OUTPUT “B”
and OUTPUT are dependant on the IF part ELSE IF mark >= 55 “Too
OUTPUT THEN young to start school”
being TRUE. ENDIF OUTPUT “C”
ELSE IF mark >= 45 THEN
Dependence indentations are approximately OUTPUT “D”
three spaces ELSE IF mark >= 30 THEN
OUTPUT “D”
Line continuations are approximately two ELSE
spaces. OUTPUT “F”
ENDIF
IF statements, logic operators and Boolean Exam technique note : In complex
expressions, it is advisable to use
parentheses to make the order of
operators operations explicit
If statements need logic operators to check a given condition but if you need to check 2 or more
conditions, you can use Boolean operators within a single IF statement.
e.g.
The identifier name must be stated
INPUT Value both sides of the Boolean operator
IF Value < 1 OR Value > 10 THEN
OUTPUT “Value is not between 1 – 10”
ENDIF
Q41-42
How many IF’s
• If statements may or may not have an else clause.
• If you have 3 options, you will need 2 questions (for example: if I was thinking of a number between 1 - 3 you
would only have to ask 2 questions because if it was neither of them you could presume that it was the 3rd option).
• Pseudocode example of IF to identify if number is positive, negative or equal to 0
IF number > 0 THEN
OUTPUT “number is positive”
ELSE IF number < 0 THEN
OUTPUT “number is negative”
ELSE
OUTPUT “number is equal to 0”
ENDIF
Exam technique note : The comment
Q43-45 Comments in pseudocode continues until the end of the line
Explanatory text within a program to describe its function, it is not executed when the
program is run
As programmers often work as part of a team, others may need to read and understand your
program. It is important to use comments to ensure that your code is understandable. // this algorithm swaps the
values of X and Y
Normally the comment is on a separate line before, and at the same level of temp ← X // temporarily store X
indentation as, the code it refers to. X ← Y
Y ← temp
Occasionally, however, a short comment that refers to a single line may be at the
end of the line to which it refers.
Trace tables are used to help the programmer either plan their programs or check to see what is
Trace Tables happening in the program after it has been created. It sets out the variable names and allows the
developer to see what will happen to each variable as the program runs.
We have already seen a small trace table in Qs 32 and 33. Now I will go through with you how to complete a trace
table from both pseudocode and a flowchart below.
Q46
INPUT Num1
Total Num1
WHILE Total <= 10 DO
INPUT Num2
Total = Total + Num2
ENDWHILE
OUTPUT Total
Trace tables are used to help the programmer either plan their programs or check to see what is
Trace Tables happening in the program after it has been created. It sets out the variable names and allows the
developer to see what will happen to each variable as the program runs.
Q47
To create a trace table, we create columns for all variables (data will be updated) and
a column for output. Num1 Total Num2 Output
We can now begin filling the table in one row at a time. Let’s imagine 2 is the first
INPUT Num1
Total Num1
input. 2 is assigned to Num1 and then in turn assigned to Total.
WHILE Total <= 10 DO Num1 Total Num2 Output
INPUT Num2
Total = Total + Num2 2 2
ENDWHILE
OUTPUT Total
Trace tables are used to help the programmer either plan their programs or check to see what is
Trace Tables happening in the program after it has been created. It sets out the variable names and allows the
developer to see what will happen to each variable as the program runs.
You will add a new row when:
Q48
• You reach a decision diamond/loop or
• You need to write a new value to a variable and it has already been filled in for
that row
Now we get to the decision diamond. Total contains 2 and therefore is less than 10
INPUT Num1
Total Num1
so we follow the “Yes” pathway. We will now input 5 as Num2.
WHILE Total <= 10 DO Num1 Total Num2 Output
INPUT Num2
Total = Total + Num2 2 2
ENDWHILE 7 5
OUTPUT Total
Trace tables are used to help the programmer either plan their programs or check to see what is
Trace Tables happening in the program after it has been created. It sets out the variable names and allows the
developer to see what will happen to each variable as the program runs.
The algorithm loops back to the decision. Total is still less than 10 so another row is required. Lets
enter 4 this time. Num1 Total Num2 Output
2 2
7 5
11 4
Once again we loop back to the decision, but this time Total is NOT less than or equal to 10. We
follow the “No” pathway and so we require an output.
INPUT Num1
Total Num1 Num1 Total Num2 Output
WHILE Total <= 10 DO
2 2
INPUT Num2
Total = Total + Num2 7 5
ENDWHILE 11 4
OUTPUT Total 30 11
Q49-50
Trace Tables
Trace Table Summary Num1 Total Num2 Output
2 2
• Add a column for each variable and one the output 7 5
• Initial variable values should be set on the first row 11 4
11
• Each loop iteration should be a new row
• If a variable value does not change, you do no need to repeat it on each row
• The final output should be on a new row
• If there is any string output, ignore the quote marks