Planning · Before you write any code

Pseudocode & Flowcharts

Every program is thought out before it is typed. Pseudocode is that thinking, written down: plain English arranged like code. Flowcharts are the same plan drawn as shapes. Both turn up in the exam, so this module teaches you to write them, not just recognise them.

1. What pseudocode actually is

Pseudocode means “fake code”. It is a plan for a program, written in short English sentences and laid out like code, with the important words in CAPITALS.

The thing to get straight first: no computer ever runs pseudocode. A person reads it. That is the whole point of it existing.

  • It has no fussy rules. No colons, no brackets, no semicolons to forget. You cannot get a syntax error, because nothing is checking.
  • It works for any language. The same plan can be turned into Python, Java, JavaScript or C#. The plan is the idea; the language is just how you spell it.
  • It is how you think first and type second. Sort out what the program does while it is cheap to change your mind, then translate.
  • It is on the exam. Writing pseudocode shows you can think like a programmer without the marker needing to check whether you remembered Python’s punctuation.

Think of a recipe. “Boil the water, add the pasta, wait 10 minutes, drain it.” Nobody can eat the recipe. It is instructions for a human, in order, and that is exactly what pseudocode is for a program.

Pseudocode (a plan)

OUTPUT "What is your name?"
INPUT name
OUTPUT "Hello " + name

Python (the real thing)

name = input("What is your name?")
print("Hello " + name)

Same idea, twice. The pseudocode says what happens. The Python says it in a way a computer will accept.

Check your understanding: What happens if you type pseudocode into Python and press Run?

2. The four shapes

A flowchart is the same plan as a picture. You follow the arrows from the top, and the shape of each box tells you what kind of step it is. There are only four you need.

Terminator

Where the program starts and stops. There is one at the top and one at the bottom, always.

BEGIN END

Input / Output

Data going in or coming out: asking a question, printing an answer, reading the keyboard.

INPUT OUTPUT

Process

The program doing something to a value: working it out, changing it, storing it.

SET

Decision

A question with two ways out, Yes and No. This is what an IF looks like.

IF

Arrows join the shapes and show the order. Only the diamond ever has two arrows leaving it, and they must be labelled Yes and No.

The same program, both ways

Here is a program that asks your age and tells you whether you can vote. On the left is the flowchart, on the right is the pseudocode. Read them side by side: every shape becomes a line.

BEGIN OUTPUT "How old are you?" INPUT age age >= 18 ? No Yes OUTPUT"Too young" OUTPUT"You can vote" END
BEGIN
    OUTPUT "How old are you?"
    INPUT age
    IF age >= 18 THEN
        OUTPUT "You can vote"
    ELSE
        OUTPUT "Too young"
    ENDIF
END

The diamond is the IF. The Yes arrow is the line under THEN; the No arrow is the line under ELSE. Where the two arrows join back up is exactly where ENDIF goes.

Which shape holds this line?

Six lines of pseudocode. Tap the shape each one would be drawn in.

Check your understanding: Why is the decision shape a diamond and not a rectangle?

3. The words you are allowed to use

Pseudocode keywords go in CAPITALS. Everything else, your own variable names and messages, stays normal. That contrast is what makes it readable at a glance.

KeywordWhat it meansExample
BEGIN / ENDThe program starts here / stops hereBEGIN
OUTPUTShow something on the screenOUTPUT "Hello"
INPUTRead what the user types, and keep itINPUT age
SETStore or work out a valueSET total = price * 2
IF ... THENAsk a question; do the next bit only if the answer is yesIF age >= 18 THEN
ELSEWhat to do instead, when the answer is noELSE
ENDIFThe decision is finished, carry onENDIF
Markers accept sensible wording. PRINT or DISPLAY instead of OUTPUT, READ or GET instead of INPUT: all fine, as long as the meaning is obvious. What you must not do is mix them up halfway through. Pick a set and stay with it.

Check your understanding: Which line correctly reads a number the user types and keeps it?

4. Writing an IF, line by line

This is the bit worth practising, because it is the bit the exam asks for. We will build one up a line at a time. The job: ask how tall someone is, and tell them whether they can go on the ride (140 cm or more).

Step 1. Every program is wrapped in BEGIN and END. Write those first and fill in the middle.

BEGIN

END

Step 2. Ask the question. Asking is something the user sees, so it is an OUTPUT.

BEGIN
    OUTPUT "How tall are you in cm?"

END

Step 3. Read the answer, and keep it in a variable. This is the line the first mark is for. INPUT height, not just INPUT, because you are about to need that number.

BEGIN
    OUTPUT "How tall are you in cm?"
    INPUT height

END

Step 4. Now the decision. Test the variable, indent what happens if the answer is yes, and close it with ENDIF.

BEGIN
    OUTPUT "How tall are you in cm?"
    INPUT height
    IF height >= 140 THEN
        OUTPUT "You can ride"
    ENDIF
END

Step 5. As it stands, someone too short gets nothing at all. ELSE covers everything the IF did not.

BEGIN
    OUTPUT "How tall are you in cm?"
    INPUT height
    IF height >= 140 THEN
        OUTPUT "You can ride"
    ELSE
        OUTPUT "Sorry, too short"
    ENDIF
END

That is the finished answer, and it is the same skeleton every question of this type wants:

  • Ask, then read. An OUTPUT question, then an INPUT that stores the answer in a variable.
  • Test the variable. The IF line has to mention the thing you just read in. Testing anything else scores nothing.
  • Both branches. THEN covers yes, ELSE covers no. Exactly one of them happens.
  • Close it. ENDIF, then END. Indent so the shape is obvious.
One equals or two? In pseudocode a single = inside an IF means “is equal to”, because a human is reading it and there is no confusion. IF password = "hallam" THEN is correct pseudocode. In Python you must write == for a comparison, because = already means “store this”. Same idea, two spellings: see Making Decisions for the Python side.

Check your understanding: A student writes INPUT on its own, then IF height >= 140 THEN. What is wrong?

5. ENDIF, curly brackets and indentation

Every language needs a way of saying “the IF stops here”. Without it, nothing could tell which lines belong inside the decision and which come afterwards. Languages just disagree about how to say it.

Pseudocode

says it in a word: ENDIF

IF age >= 18 THEN
    OUTPUT "You can vote"
ELSE
    OUTPUT "Too young"
ENDIF

Java

uses curly brackets: { }

if (age >= 18) {
    System.out.println("You can vote");
} else {
    System.out.println("Too young");
}

Python

uses the indentation itself

if age >= 18:
    print("You can vote")
else:
    print("Too young")

Three ways of drawing the same box round the same two lines. Java writes }, pseudocode writes ENDIF, and Python has nothing to write at all: in Python the block ends when the indenting stops. That is why a stray space matters so much in Python and matters not at all in Java.

So do I indent my pseudocode? Yes. Pseudocode does both: ENDIF for the marker to see, and indentation so the shape reads at a glance. Indenting is never wrong, and it is the habit that saves you when you move to Python.

Check your understanding: How does Python know where an if block finishes?

6. Put the lines in order

Here are the nine lines of a weather program, shuffled. It should ask how hot it is, then say Hot day if it is 30 or more and Not too bad if it is not.

Tap the lines in the right order. The indenting is drawn in for you, so watch the shape appear. Tap a line you have already placed to take it back out.

7. How the marks are given

Pseudocode questions are marked on structure, not spelling. Here is a real one, worth 3 marks, with the marks pointed out.

Exam practice · Year 9

Write pseudocode for a program that does all of this: (3 marks)

  1. asks the user how old they are
  2. prints You can vote if they are 18 or older
  3. prints Too young if they are not
Show the answer and where the marks go
BEGIN
    OUTPUT "How old are you?"
    INPUT age                        <- mark 1
    IF age >= 18 THEN              <- mark 2
        OUTPUT "You can vote"       <- mark 3
    ELSE
        OUTPUT "Too young"          <- mark 3
    ENDIF
END
  • 1 mark for an INPUT that stores the age in a variable.
  • 1 mark for an IF that tests 18 or older.
  • 1 mark for both messages, each on the correct branch.
  • Sensible wording is accepted: the keywords do not have to match exactly.

Two answers that dropped marks

Lost mark 1

BEGIN
    OUTPUT "How old are you?"
    INPUT
    IF age >= 18 THEN
        OUTPUT "You can vote"
    ELSE
        OUTPUT "Too young"
    ENDIF
END

The answer was never stored, so age in the IF refers to nothing. 2 out of 3.

Lost mark 3

BEGIN
    OUTPUT "How old are you?"
    INPUT age
    IF age >= 18 THEN
        OUTPUT "You can vote"
    ENDIF
END

No ELSE, so a 12 year old gets no answer at all. 2 out of 3.

Both students knew what they were doing. Both lost a mark on one missing line, which is why the four rules in part 4 are worth memorising as a shape rather than as a paragraph.

8. Quick check Part 1 of 2

Eight quick questions. This is half of completing the module. The exam task below is the other half.

What is pseudocode?

In a flowchart, which shape is used for a decision (an IF)?

Which shape holds INPUT and OUTPUT lines?

Which line correctly reads an age and keeps it?

Which keyword says “the decision is finished, carry on”?

A program says one thing when the answer is right and something different when it is wrong. Which keyword gives you the second message?

In pseudocode, what does the single = in IF name = "Sam" THEN mean?

Pseudocode uses ENDIF and Java uses a closing curly bracket. What does Python use to end an if block?

9. Exam task: write it yourself Part 2 of 2

Same shape as the exam question above, different program. Write your pseudocode in the box and press Check my pseudocode. You will get the three marks back one at a time, with a note on each. Full marks completes the module.

Your question · 3 marks

Write pseudocode for a school gate program that does all of this:

  1. asks the user for the password
  2. prints Welcome in if the password is equal to hallam
  3. prints Access denied if it is not

Note the difference from the voting question: that one tested 18 or older with >=. This one tests is exactly equal to, so you need = (or EQUALS) instead.

Tab indents by four spaces · your draft is kept in this browser, so you can come back to it.

How the module is marked: the quick check is worth 50% and this exam task is worth 50%. Do both to reach 100%. Your teacher can see your score on the progress page.