Coding 2
Coding 2
Computer Science
Coding
1 – Alogorithms
Colour Coding
Tips!
Key Terms
To input a value:
While the Cambridge International AS Level syllabus does not require you to be
able to write program code, the ability to do so will increase your understanding,
and will be particularly beneficial if you are studying the full Cambridge
International A Level course.
Python
Visual Basic
Java
To perform iteration using FOR, REPEAT–UNTIL and WHILE loops:
A FOR loop has a fixed number of repeats, the STEP increment is an optional
expression that must be a whole number.
Statements in a REPEAT loop are always executed at least once.
In pseudocode, write an algorithm to set a password for a user when they have to
input the same word twice. Then allow the user three attempts to enter the correct
password. Complete an identifier table for your algorithm.
Finally, check your pseudocode algorithm works by writing a short program from your
pseudocode statements using the same names for your identifiers.
Writing pseudocode from a structured English description
There are no set rules for writing structured English – the wording just needs to be
unambiguous and easily understandable. Pseudocode is more precise and usually
follows an agreed set of rules.
From a structured English description, the following things need to be possible:
• Any variables that need to be used can be identified and put in an identifier table
– these can be items input or output as the results of calculations.
• Input and output can be identified from the wording used, for example, Enter,
Read, Print, Write.
• Selection can be identified from the wording used, for example, If, Then, Choose.
• Any iteration required can be identified from the wording used, for example,
Loop, Repeat.
• Any processes needed can be identified from the wording used, for example, Set,
Calculate.
When the identifier table is complete, each structured English statement can be used
to write one or more pseudocode statements, keeping the same order as the
structured English.
Here is an example of an algorithm to calculate a runner’s marathon time in seconds,
using structured English.
This can be used to identify the variables required and complete the identifier table.
Using these identifiers, each step of the structured English algorithm can be converted
to pseudocode:
Flowcharts are diagrams showing the structure of an algorithm using an agreed set of
symbols:
Flowcharts can be used to identify any variables required and you can then complete
an identifier table.
Each flowchart symbol can be used to identify and write one or more pseudocode
statements.
Here is an example of a flowchart of an
algorithm that can be used to check an exam
grade:
The same algorithm is presented in pseudocode
on the left.
Below is the identifier table:
3, 4, 5 and 6 form a nested selection (IF)
structure, as each following statement is part
of the ELSE clause.
It is only at 7 that the selection is complete.
The flowchart shows this clearly and the
pseudocode uses indentation to show the
nesting.
Stepwise refinement
Look at the algorithm to calculate the area of a chosen shape written in structured
English below.
Use stepwise refinement to break each step into more manageable parts then rewrite
the algorithm using pseudocode.
Then check your pseudocode algorithm works by writing a short program from your
pseudocode statements using the same names for your identifiers.