Pseudo Code
Pseudo Code
solving
Key objectives
The objectives of this chapter are to revise: ● validation and verification checks when data is
● the stages in the program development cycle: input
– analysis, design, coding, testing ● use of different types of test data including:
● computer systems and sub-systems – documentation of a dry run using a trace
● problem decomposition into component parts table
● methods used to design and construct ● writing, amending identifying, and correcting
solutions to problems errors in:
● the purpose of an algorithm and the processes – flowcharts, programs, pseudocode
involved in it
● standard methods of solution:
– totalling, counting, finding maximum,
minimum, average, linear search, bubble
sort
Stage
Process of Uses structure charts, Writing and iterative Testing the completed (Not a part of the
investigation, using flowcharts and testing of the program to make sure syllabus)
abstraction and pseudocode with the program or suite of that it works under all
decomposition to program specification programs. conditions.
specify what a from analysis to
program does. show to how the
program should be
developed.
It is illegal to photocopy this page
98 Cambridge IGCSE™ and O Level Computer Science Study and Revision Guide Second Edition
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 99 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd
a) Analysis is used to investigate the problem that requires a solution and find Part a) of this question
out what the program needs to do to provide that solution. asks for the purpose, so
b) Two other stages are: your answer must state
why the analysis stage is
design – document the tasks required for the solution using structure charts
needed, not what actions
and pseudocode
are to be performed. It
coding – write the programs that are required. is always very important
to read the question
carefully. Part b) of
Sample low-level answer this question involves
a) A systems analyst will use interviews and questionnaires to find out what is ‘identify’. This means
needed. your answer must either
name the two stages of
b) The stages of program development are analysis, design, coding and testing.
the program life cycle or
There is lots to do as the programs need to be written and fully tested.
clearly identify each one
with a short statement.
Teacher’s comments
The first answer would gain full marks since there is a clear description of
the purpose of analysis. Two other stages of types of the program life cycle
have been identified and described with each description clearly shown for
the stage identified.
The second answer would gain one mark for part a) since the statement
‘analysis is to find out what is needed’ partially describes the purpose of
analysis. Including the methods used answered a different question about
how analysis is carried out. For part b) all the stages of the program life
cycle are identified not just the two required. There is a description of the
actions required but it is unclear which stage these apply to. A maximum of
two marks could be awarded.
Hodder & Stoughton Limited © David Watson and Helen Williams 2022 99
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 100 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd
7.2.2 Flowcharts
A flowchart shows diagrammatically the steps required to complete a task
and the order that they are to be performed. These steps, together with
the order, are called an algorithm.
Flowcharts are drawn using standard flowchart symbols as shown in
this table.
Use Symbol Description
The same flowchart symbol is used to show the input of data and
Input/output INPUT X output of information.
7.2.3 Pseudocode
Pseudocode is 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 a high-level programming language and meaningful
identifier names.
It is illegal to photocopy this page
Mathematical operators
Operator Action
+ Add
− Subtract
* Multiply
/ Divide
^ Raise to the power
() Group
100 Cambridge IGCSE™ and O Level Computer Science Study and Revision Guide Second Edition
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 101 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd
Pseudocode statements
Pseudocode statement Examples
Assignment Cost ← 10
A value is assigned to an item/variable using the ← operator. SellingPrice ← Price + Tax
Conditional 1 IF Age < 18
A condition that can be true or false: IF … THEN … ENDIF or IF … THEN
THEN … ELSE … ENDIF OUTPUT "Child"
For an IF condition the THEN path is followed if the condition is true, ELSE
and the ELSE path if it is false (an ELSE may not be required). The
OUTPUT "Adult"
end of the statement is followed by ENDIF
ENDIF
Conditional 2 CASE OF Grade
A choice between several different values: CASE OF … OTHERWISE "A" : OUTPUT "Excellent"
… ENDCASE "B" : OUTPUT "Good"
For a CASE statement, the value of the variable decides the path "C" : OUTPUT "Average"
taken. Several variables are usually specified. OTHERWISE path is
OTHERWISE OUTPUT "Improve"
taken for all other values. The statement is ended by ENDCASE
ENDCASE
Iteration 1 FOR Counter ← 1 to 10
FOR … TO … NEXT a variable is set up, with a start value and an OUTPUT "*"
end value, this variable is incremented in steps until the end value is
NEXT Counter
reached and the iteration finishes.
Iteration 2 Counter ← 0
REPEAT … UNTIL is used when the number of repetitions/ REPEAT
iterations is not known, and the actions are repeated UNTIL a
OUTPUT "*"
given condition becomes true. The actions in this loop are always
completed at least once. Counter ← Counter + 1
UNTIL Counter >= 10
Iteration 3 Counter ← 0
WHILE … DO … ENDWHILE is used when the number of WHILE Counter < 10 DO
repetitions/iterations is not known, and the actions are only
OUTPUT "*"
repeated WHILE a given condition is true. If the WHILE condition is
untrue then the actions in this loop are never performed. Counter ← Counter + 1
ENDWHILE
Input INPUT Name
INPUT used for data entry. INPUT StudentMark
Output PRINT "Your name is", Name
OUTPUT or PRINT used to display information. OUTPUT Name1, "Ali", Name3
Nesting 1 IF Age < 18
Nested IF makes use of two IF statements; the second IF THEN
statement is part of the first ELSE or THEN path
It is illegal to photocopy this page
OUTPUT "Child"
ELSE
IF Age > 65
THEN
OUTPUT "Senior"
ELSE
OUTPUT "Adult"
ENDIF
ENDIF
Hodder & Stoughton Limited © David Watson and Helen Williams 2022 101
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 102 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd
Comparison operators
Operator Comparison
> Greater than
< Less than
= Equal
>= Great than or equal
<= Less than or equal
<> Not equal
AND Both
OR Either
NOT Not
Method 1
no
It is illegal to photocopy this page
Accept ← FALSE
STOP
ii) Identify another formal method that could be used to check if the
password has more than eight characters.
iii) Use the formal method you identified in part b)ii) to show the
same routine to check if the password has more than eight
characters. [8]
102 Cambridge IGCSE™ and O Level Computer Science Study and Revision Guide Second Edition
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 103 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd
Tip
Part a) of this question asks for two other component parts; you
must not use the part mentioned in the question. It is surprising how
many students do this and throw away a mark. Part b)i) and ii) of this
question involves ‘identify’. This means your answer must either name
the methods or clearly identify each one with a short statement. Part
b)iii) needs the same routine to be rewritten using pseudocode, not a
programming language.
Teacher’s comments
The first answer would gain full marks since two other components are
identified, the methods are identified correctly, and the routine rewritten
exactly matched the flowchart given and used the same identifiers,
Password and Accept.
The second answer would gain one mark for part a) since only one other
method was identified. For part b)i) the methods are identified, but the
terminology used was not accurate, so one mark. For parts b)ii) and iii)
code is used for the actual solution and the student has gone on to write a
It is illegal to photocopy this page
solution in Python that did not exactly match the flowchart. A maximum of
two marks could be awarded.
Hodder & Stoughton Limited © David Watson and Helen Williams 2022 103
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 104 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd
Worked example
START INPUT Num1, Num2
IF Num1 > Num2
INPUT
THEN PRINT NUM1, " is largest"
Num1, Num2 ELSE PRINT NUM2, " is largest"
ENDIF
PRINT Num2,
" is largest"
STOP
Counting
It is illegal to photocopy this page
104 Cambridge IGCSE™ and O Level Computer Science Study and Revision Guide Second Edition
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 105 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd
Linear search
Sample pseudocode Notes
OUTPUT "Enter name to find " A linear search inspects each
INPUT Name name in a list in turn to see if
the name matches the name
Found ← FALSE that was input.
Counter ← 1 The search stops as soon as the
REPEAT name is found.
IF Name = Name[Counter]
THEN
Found ← TRUE
ELSE
Counter ← Counter + 1
ENDIF
UNTIL Found OR Counter > ClassSize
IF Found
It is illegal to photocopy this page
THEN
OUTPUT Name, " found"
ELSE
OUTPUT Name, " not found."
ENDIF
Hodder & Stoughton Limited © David Watson and Helen Williams 2022 105
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 106 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd
Bubble sort
Sample pseudocode Notes
First ← 1 A bubble sort compares each
Last ← ClassSize name in the list is with the next
name and swaps them if the
REPEAT names are in the wrong order,
Swap ← FALSE starting from the first name and
finishing with the next to last
FOR Index ← First TO Last - 1 name.
IF Name[Index] > Name[Index + 1] The last name is in the right
THEN place once the list has been
checked, this is repeated,
Temp ← Name[Index]
decreasing the size of list
Name[Index] ← Name[Index + 1] by one, until no names are
Name[Index + 1] ← Temp swapped or there are no names
left to be swapped.
Swap ← TRUE
ENDIF
NEXT Index
Last ← Last -1
UNTIL (NOT Swap) OR Last = 1
Data entry
Verification Validation
The data is A manual Checks that Checks that Checks that Checks to Checks that
entered twice check to the value of the data the data ensure that the characters
and compared ensure the a number is entered is a entered is of some data has entered
to ensure both data on the between an reasonable a given data been entered conform to a
It is illegal to photocopy this page
entries are the screen is the upper value number or an type and the value pre-defined
same same as the and a lower exact number has not been pattern
form value of characters left blank
106 Cambridge IGCSE™ and O Level Computer Science Study and Revision Guide Second Edition
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 107 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd
Test data
Abnormal/
Normal Boundary Extreme
erroneous
Test data that is Test data that is At each The largest and
accepted and the rejected by the boundary two smallest values
algorithm is algorithm as not values are that normal data
expected to suitable required; one can take
work with value is accepted
and the other
value is rejected
Test data is used to dry run the flowchart and record the results in the trace
table.
04 INPUT Num
05 IF Num < Small
06 THEN
07 Small ← Num
08 ENDIF
09 Counter ← Counter + 1
10 UNTIL Counter ← 10
11 INPUT "Smallest number is ", Small
Hodder & Stoughton Limited © David Watson and Helen Williams 2022 107
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 108 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd
a) Find the three errors in the algorithm and suggest a correction for Tips
each error.
b) Write the corrected algorithm as a flowchart. Part a) of this question
asks you to identify the
c) Describe the purpose of the corrected algorithm.
errors and provide a
d) Complete the trace table for the corrected algorithm using this corrections; there will
input data: probably be only one
7, 11, 3, 9, 6, 8, 4, 2, −3, 13, −1, −99 mark for this. You need to
e) Extend the pseudocode algorithm to validate that data input is ensure that each error is
positive. clearly identified, quoting
f) Write an extended pseudocode algorithm to find the average of the the line number if given
data input. [18] in the question can help.
Part b) requires a
Sample high-level answer flowchart. For full marks
you will need to have the
a) 01 should be Small ← 10000
correct structure and
10 should be UNTIL Counter = 10
the correct content. For
11 should be OUTPUT Small the structure, you must
b) use the correct flowchart
START
symbols as given in the
syllabus, ensure that
each flowline has an
arrow and clearly label
Small ← 10000 the flowlines out of a
Counter ← 0 decision.
In part c) for the purpose,
you answer must
INPUT state exactly what the
Num algorithm does.
In part d), the trace table
must only contain the
data that the algorithm
NUM < yes uses; sometimes there
Small ← Num
Small?
can be extra values
included that are not
no needed.
In part e) where
Counter ← validation is required,
Counter + 1
the algorithm must only
allow further actions
if data that meets the
required rules is entered.
no Counter
In part f), any pseudocode
= 10?
written should be
correctly indented
It is illegal to photocopy this page
108 Cambridge IGCSE™ and O Level Computer Science Study and Revision Guide Second Edition
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 109 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd
c) Input ten numbers, find and output the smallest number input.
Teacher’s comments
d)
The first answer would
Counter Num Small OUTPUT
gain full marks since in
0 10000 part a) all the errors have
1 7 7 clearly identified and
corrected, quoting the
2 11
line number of the error.
3 3 3 The student has drawn
4 9 a flowchart showing the
corrected algorithm for
5 6 part b), it has the correct
6 8 structure and the correct
content. Part c) the
7 4
student states exactly
8 2 2 what the algorithm does.
9 −3 −3 Part d) the trace table
contains the data that the
smallest
10 13
number is −3 algorithm uses, and the
correct output.
e) Replace line 4 with:
04 REPEAT Part e) the input is
05 INPUT Num validated, and the
06 UNTIL Num > 0 algorithm only allows
f) further actions if data
that meets the required
01 Small ← 10000 rules has been entered. An
error message could have
02 Counter ← 0
been included. Part f) the
03 Total ← 0 pseudocode written is
04 REPEAT accurate and readable; the
only improvement could
05 REPEAT be to add comments to
06 INPUT Num explain the functionality
of the pseudocode.
07 UNTIL Num > 0
08 IF Num < Small
09 THEN
10 Small ← Num
11 ENDIF
12 Total ← Total + Num
13 Counter ← Counter + 1
14 UNTIL Counter = 10
It is illegal to photocopy this page
Hodder & Stoughton Limited © David Watson and Helen Williams 2022 109
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 110 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd
110 Cambridge IGCSE™ and O Level Computer Science Study and Revision Guide Second Edition
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 111 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd
f)
01 Small ← 0
02 Counter ← 0
04 REPEAT
05 INPUT Num
06 IF Num < 0 THEN OUTPUT "Not positive"
07 IF Num < Small THEN Small ← Num
08 T ← T + Num
09 Counter ← Counter + 1
10 UNTIL Counter = 10
11 OUTPUT Small, T / Counter
1 Tick (✓) the appropriate column, in the following table, to indicate whether the description
applies to validation, verification or neither.
You may need to tick more than one column in each row. [5]
Description Validation Verification Neither
Ensuring a number is positive
Ensuring a password is as intended
Ensuring a password is correct
Ensuring a password contains a number
Ensuring a password contains a special character
Hodder & Stoughton Limited © David Watson and Helen Williams 2022 111
318489_07_IGCSE_CO_SE_OL_098-112.indd Page 112 12/07/22 1:13 PM F-0250 /145/HO02580/work/indd
2 In this question you will be given a statement followed by four possible answers. Select which
of the four answers you think is correct and tick (✓) the box next to your answer.
a) This is a conditional statement:
® Answer ← Number1 + Number2
® REPEAT … UNTIL Number1 = Number2
® IF Number1 = Number2 THEN …
® WHILE Number1 = Number2 …
b) What is meant by the term decomposition?
® Discarding unnecessary details.
® Breaking a complex problem into smaller parts.
® Showing how a program should be developed.
® Writing and testing a program.
c) What is meant by the term abstraction?
® Identifying data that needs to be stored.
® Breaking sub-systems down into smaller sub-systems.
® Keeping the key elements and discarding information not needed.
® Stage of the program development life cycle. [3]
3 a) Write pseudocode to input 12 numbers and store them in an array.
b) Change your pseudocode to use a different loop structure.
c) Identify another loop structure you could have used.
d) Write pseudocode to find the largest, smallest and the average of the numbers you have stored.
e) Write pseudocode to sort the numbers you have stored in descending order. [16]
4 Explain the difference between mathematical and comparison operators. Include an example
of each type in your answer. [4]
5 Identify three types of test data. Include an example of each
type in your answer with an explanation of why it is appropriate. [9]
6 Identify four errors in this algorithm. [4]
14 IF Found
15 THEN
16 OUTPUT Product, " found at position ", Counter, " in the list."
17 ELSE
18 OUTPUT Name, " not found."
19 ENDIF
112 Cambridge IGCSE™ and O Level Computer Science Study and Revision Guide Second Edition