GoalExplain what pseudocode is, why designers use it and what it must still contain.
New words
pseudocode · a way of writing an algorithm that looks like code but is not a real language, so a computer cannot run itsyntax · the rules for how words and symbols must be writtenlanguage-independent · can be turned into any programming languagestatement · one instruction on one line
Explanation
An algorithm can be shown in plain English, as a flowchart, as pseudocode or as a Python program. Pseudocode sits halfway between a flowchart and a real program. It looks like code, with command words and variables, but it is not a real language, so no computer can run it. It has no fixed syntax, so one instruction can be written in several correct ways. It is language-independent: a Python programmer and a programmer who uses another language can both read the same plan. Designers use it to think about the steps, variables and calculations without worrying about colons and brackets. It must still use the same constructs as real programs: sequence, selection and iteration. In these lessons we use one house style, but other books and teachers may write pseudocode differently.
Plain Englishdraw→Flowchartwrite→Pseudocodetranslate→Python program
Worked examples
In English
Flowchart box
Pseudocode
Python
Ask how many loaves are wanted
INPUT loaves
INPUT loaves
loaves = int(input("Loaves? "))
Show the words Thank you
OUTPUT "Thank you"
OUTPUT "Thank you"
print("Thank you")
The same two instructions are shown four ways. Read across each row. Pseudocode is closer to Python than English is, but it has no colons, brackets or int().
Line
Valid pseudocode?
Reason
INPUT loaves
yes
command word and a variable
READ loaves
yes
another command word, but the meaning is clear
INPUT
no
no variable, so the value would not be stored
get something from someone
no
no command word and no variable
Not every line is valid pseudocode. A valid line needs a clear command word, and a variable if data must be stored. Different command words are allowed when the meaning is clear.
Pseudocode
1INPUT loaves
2total ← loaves * 33OUTPUT "Pay ", total
Output
Pay 12
This pseudocode is a plan for a bakery till. A bread roll costs 3. With 4 rolls the program shows Pay 12. A Python programmer could now write total = loaves * 3, and a programmer using another language could do the same.
Class activity
Plan in pairs (no computer). Each person invents a tiny program for a bike shop, a choir or a market stall, and writes it as three or four lines of pseudocode with at least one INPUT and one OUTPUT. Swap papers. Your partner writes one plain English sentence for each line, then checks with you whether the meaning is the same. Next, change one command word (for example INPUT to READ) and decide together whether the pseudocode is still valid. If you have a computer or tablet, type the program in Python afterwards.
Practice
1
Which of these lines are valid pseudocode? Give a reason for any that are not.
Line
Statement
A
INPUT cost
B
ask the user, then maybe keep it somewhere
C
OUTPUT "Hello"
D
READ cost
Lines A, C and D are valid. Line B is not valid: it has no command word and no variable, so nobody can turn it into code.
2
Rewrite this Python program as pseudocode in the house style.
INPUT team, INPUT players, OUTPUT "Team ", team, OUTPUT players * 2. Other command words (READ, WRITE) would also be valid if used in the same way all the way through.
3
A designer in one country writes an algorithm in pseudocode. A programmer in another country uses a different programming language. Give two reasons why pseudocode helps them.
Both can read it, because it is language-independent and written in everyday words. The programmer can translate it into any language. The designer does not need to know the exact syntax of that language, and mistakes such as a missing colon do not matter yet.
4
Say whether each statement is true or false, and correct the false ones. (a) A computer can run pseudocode directly. (b) Pseudocode has no fixed syntax. (c) Pseudocode can ignore sequence, selection and iteration.
(a) False: it is not a real language, so it must be translated first. (b) True. (c) False: it must use the same constructs as real programs.