Watch on Instagram

Code Bytes · "Start Here: What Is Coding?" · Episode 8 · Thinking like a coder

Pseudocode

Pseudocode is a plan for a program written in plain English but shaped like code. A computer can't run it, but it lets you do the hard thinking first. Each line of the plan then turns into a line of real code, like ask becoming input and say becoming print in Python.

Posted · About a 1 minute watch, 3 minute read · Take the quiz

What pseudocode is

Before you write any code, it helps to write a plan. Pseudocode is a plan written in everyday words but laid out like code: one step per line, with indents to show which steps belong together.

Pseudo means fake (it comes from the Greek word for false). Pseudocode is not a real programming language, so a computer can't run it. It's written for people, including future you.

There are no fixed rules. Different teachers and exam boards each use their own style; what matters is that the steps are clear.

A plan: do I need a jacket?

Say you want a tiny program that tells you whether to bring a jacket. The plan:

`` ask for the temperature if it is below 10 say "Bring a jacket" otherwise say "No jacket needed" ``

(Temperatures here are in Celsius: 10 °C is about 50 °F.)

Notice the shape: the two say lines are indented, so you can see which one belongs to if and which one belongs to otherwise.

Turning the plan into Python

Now the hard thinking is done. Each line of the plan becomes one line of real Python:

``python temp = int(input("Temperature? ")) if temp < 10: print("Bring a jacket") else: print("No jacket needed") ``

| Plan word | Python | |---|---| | ask | input | | if | if | | otherwise | else | | say | print |

Run it and type 5:

`` Temperature? 5 Bring a jacket ``

Why plan first?

When the plan is right, writing the code is mostly translating. If something is wrong, you fix your thinking in plain English, before you have to fight with typos, brackets and colons at the same time.

Try it yourself

  1. Copy the Python code above into any free online Python editor and run it. Type 3 and check that it says Bring a jacket.
  2. Change the plan so the cut-off is 18 instead of 10. Change the matching Python line to if temp < 18:, run it again and type 15. What does it print now?
  3. Write your own pseudocode for an umbrella helper: ask if it is raining, and say "Take an umbrella" if it is. Then try turning it into Python.

Good to know

  • Every snippet here was run with Python 3: typing 5 prints Temperature? 5 then Bring a jacket.
  • "Ask becomes input" is a simplification: int(...) also turns the typed text into a whole number. A later Code Bytes episode on data types explains why that matters.
  • Typing something that isn't a whole number, like warm, makes this snippet crash with a ValueError. Real programs check for that.
  • Pseudocode is usually shown in a monospace font so it looks like code, but it is still just a plan, not something you can run.

Answer to the comment question

Using this plan, what does it say at exactly 10 degrees? No jacket needed. The plan says "if it is below 10", and 10 is not below 10, so the otherwise line runs. In Python, 10 < 10 is False, so the else branch prints No jacket needed. (If you wanted 10 to count, you'd write "if it is 10 or below", which is temp <= 10 in Python.)

Test your knowledge

Quick quiz: Pseudocode

4 multiple-choice questions. Pick one answer for each, then check how you did.

1What is pseudocode?
2Can a computer run pseudocode?
3In the jacket example, what does "say" become in Python?
4Why write the plan first?

Sources

Video transcript

What should you write before you write any code?

A plan in plain English, shaped like code. It's called pseudocode. Pseudo means fake: it's not a real language, so a computer can't run it.

Say you want to know if you need a jacket. The plan: ask for the temperature. If it's below ten, say bring a jacket. Otherwise, say no jacket needed.

Now the hard thinking is done. Each line turns into real Python. Ask becomes input, if stays if, otherwise becomes else, and say becomes print.

Run it, type five, and it prints bring a jacket. Plan first, and you fix your thinking before you fight with typos.

Using this plan, what does it say at exactly ten degrees? Comment your answer below!

That's your byte.

More from "Start Here: What Is Coding?"

All "Start Here: What Is Coding?" lessons