Skip to content
Programming contents

Lesson 1.2: Algorithms and pseudocode

Before writing code you need to know which steps to take. That is an algorithm, and pseudocode is how you write it down.

The idea

An algorithm is the list of steps that solves a problem. It's what you think through before writing code.

Pseudocode is a way to write that algorithm with plain words and a few rules. It doesn't run: it lets you think without fighting a programming language.

The first words

PseudocodeWhat it does
Algorithm … EndAlgorithmMarks the start and the end
WriteShows something on screen
ReadAsks the user for a value and keeps it

Example

A program that says hello:

Pseudocode
Algorithm Greet
    Write "What's your name?"
    Read name
    Write "Hello, ", name
EndAlgorithm

If you type "Ana":

On screen
What's your name?
Ana
Hello, Ana

Line by line:

  1. It writes the question.
  2. It waits for you to type something and keeps it under the name name.
  3. It writes "Hello, " followed by what it kept.

What goes in quotes is written as it is. What goes without quotes is a name, and the program swaps it for what it kept.

Try it

Exercise

Write an algorithm that asks for a name and a city, and answers, for example, "Ana lives in Seville".

Show the solution
Pseudocode
Algorithm WhereYouLive
    Write "What's your name?"
    Read name
    Write "Which city do you live in?"
    Read city
    Write name, " lives in ", city
EndAlgorithm

Summary

  • An algorithm is the steps that solve a problem.
  • Pseudocode writes it with plain words and a few rules.
  • Write shows; Read asks and keeps the answer.