Lesson 02: Developing Algorithms using pseudo-code
Specification Points Addressed in this lesson:
- Use a systematic approach to problem solving and algorithm creation representing those algorithms using pseudo-code, program code and flowcharts.
- Explain simple algorithms in terms of their inputs, processing and outputs.
- Determine the purpose of simple algorithms.
1. Specifying Algorithm
Algorithms can be specified by:
- pseudocode
- flow diagrams
- bullet list
- programming language
In this lesson we will focus on using pseudocode for writing algorithms.
Pseudocode
Pseudo-code is a design tool that specifies an algorithm before committing to a formal programming language. It is a stylised form of English in that you will find everyday English words such as if, then, and while, and you will also find mathematical operators. When an extensive program is being developed, it is usually specified in pseudo-code first, tested, and errors corrected. A programmer for a particular language would translate the pseudo-code into their language. The word pseudo-code is a compound word, with pseudo meaning false and therefore, you can think of pseudo-code as false code; it is false in the sense that any compiler cannot execute it.
Whereas pseudocode is not standardised in Computer Science. AQA will use their standard pseudocode which can be accessed here.
The following pseudocode constructs are discussed below.
- Variable Assignment
- Iteration
- Selection
- Subroutine
- Input / Output
Variable Assignment
A variable is a named memory location, that stores a value. The value can change while the program is running. There are three important things to note with a variable and they are highlighted in the definition:
- named memory location
- stores a value
- the value can change while the program is running
In this course we will use the ← for variable assignment. the following line will assign the value 23 to the variable called age.
age <-- 23
In the example able the named memory location is age. You can think about this as tagging a location in memory with the name age. The second part is that we are storing the value 23 in the location. At some stage in the program we can change the value from 23 to some other value. For example:
age <-- 23
age <-- 25
will change the value stored in age to 25.
Constants
Constants are named memory location that stores a value. the value cannot change while the program is running. Constants are assigned a value in the same way that variables are, however constants are normally written in all uppercase letters. eg
VAT <-- 20
Iteration
The WHILE loop will repeat a block of code as long as the condition is true.
count <-- 1
WHILE count < 20
OUTPUT count
count <-- count + 1
ENDWHILE
On the other hand the REPEAT – UNTIL executes a block until a condition is met.
count <-- 1
REPEAT
OUTPUT count
count <-- count + 1
UNTIL count >= 20
The FOR loop is the definite iteration or count controlled iteration. It is used in situations where the number of iterations / repetition is known before hand. The syntax for the FOR loops is.
FOR var ← initialValue TO endValue [STEP val]
# statements here
ENDFOR
where var is set to some starting value, and it increments to the endValue with an optional step.
The FOR loop can also be expressed as:
FOR val in collection
# body of the FOR loop
ENDFOR
where the val takes on the actual value of each element in the collection.
Selection
IF Statement
IF condition THEN
# body of the IF statement
ENDIF
The IF Statement executes a block if the condition is true.
IF-ELSE
IF condition THEN
# body to execute if condition is true
ELSE
# body to execute if the condition is false
ENDIF
With the IF-ELSE statement one of two conditions is executed.
Both IF and IF – ELSE statements can be nested.
Subroutine
Subroutines are named section of a program that does a specific task.
SUBROUTINE find_larger(a, b)
IF a > b THEN
OUTPUT a, " is larger than ", b
ELSE
OUTPUT b, " is larger than ", a
ENDIF
ENDSUBROUTINE
The subroutine above takes two values as input, namely a and b.
Input / Output
age <-- USERINPUT
will take a user input and store the value in the variable called age.
age <-- 23
OUTPUT "Your age is ", age
Will print the string “Your age is 23” to the screen.