---
title: "What Is Pseudocode? Rules, Examples and Your First Programs: Programming Fundamentals (2)"
date: "2023-12-20"
description: "Learn pseudocode with StepCode: program structure, comments, variables, assignment, Read and Write. Write and run your first programs in the browser."
tags: ["Programming Fundamentals", "Tutorials"]
url: "https://rolandoandrade.me/blog/programming-fundamentals-pseudocode/"
author: "Rolando Andrade"
---

# What Is Pseudocode? Rules, Examples and Your First Programs: Programming Fundamentals (2)

> Learn pseudocode with StepCode: program structure, comments, variables, assignment, Read and Write. Write and run your first programs in the browser.

Pseudocode is a simple language for writing algorithms. It sits between plain English and a real programming language: it has a clear structure, but far fewer rules. After this lesson, you can read and write a complete pseudocode program that asks the user for data, does a calculation and shows the result, and you can run it in your browser.

In this lesson you will:

* Learn why pseudocode exists and where it fits.
* Learn the rules of StepCode, the pseudocode this series uses.
* Split any program into its three parts: input, process and output.
* Write and run three programs from scratch.

It takes about 40 minutes. You only need the [first lesson](/blog/programming-fundamentals-design-and-development-of-algorithms/), where you designed the salary algorithm and saw your first StepCode program.

## Why pseudocode exists

In the last lesson, you wrote the salary algorithm in plain English first, and then as a program. Plain English is easy to write but easy to make vague. A programming language is exact, but it has strict rules that get in the way when you are still thinking. Pseudocode gives you the structure of code without the details of any one language.

That makes it a bridge. You write the logic once in pseudocode, and then you can translate it to Python, JavaScript or any other language.

*Figure: The same three steps in plain English, in StepCode and in Python. Pseudocode keeps the exact meaning of code, but reads almost like English.*

This bridge matters even more now that AI agents write so much code. When you ask an agent for a program, the clearer your steps are, the better its code is. When it gives you code back, you need to read it as a list of steps and check each one. Pseudocode trains exactly that skill: thinking in precise steps without getting lost in the details of a language.

## Pseudocode you can run

For years, pseudocode lived only on paper and on whiteboards. Nobody ran it on a computer, so students could not test their algorithms. My course used the pseudocode of [PSeInt](http://pseint.sourceforge.net/), a free tool that runs Spanish pseudocode.

For this series, every example uses [StepCode](/projects/stepcode/), a pseudocode language I built for my students. It follows the same rules as PSeInt, it can use English keywords, and it runs in the browser. Every editor in these lessons runs StepCode, so you can test every idea as you read.

You will find other pseudocodes with different keywords. Do not worry about that. The ideas are the same in all of them, and once you know one, you can read the others.

## The rules of StepCode

Each rule below is short. Together, they are everything you need to write your first programs.

### Programs start and end

Every program starts with `Program` and a name, and ends with `EndProgram`. Every instruction goes between those two lines. This is the smallest complete program:

**The smallest program** (StepCode)

```stepcode
Program Hello
    Write "Hello, world";
EndProgram
```

### Instructions end with a semicolon

Each instruction ends with a semicolon (`;`). It tells StepCode where one instruction stops and the next one begins. The `Program` and `EndProgram` lines are not instructions, so they have no semicolon.

### Comments start with two slashes

A comment is a note for people who read the code. It starts with `//`, and StepCode ignores everything after it on that line. Use comments to explain why a step exists, or to match lines of code with the steps of your algorithm.

### Indentation shows structure

Indentation is the space at the start of a line. Lines inside `Program` and `EndProgram` are indented with four spaces, so you can see at a glance which lines belong together. StepCode runs the code without it, but people read code much more easily with it. In later lessons, blocks will go inside other blocks, and indentation will show each level.

### Names follow two rules

An identifier is the name you give to something, like a program or a variable. It must start with a letter (or an underscore) and have no spaces. `total`, `n1` and `hourlyRate` are valid. `my name` (with a space) and `2name` (starting with a number) are not. When a name has several words, join them and start each new word with a capital letter, like `hourlyRate`.

*Figure: Type any name, or pick one, to see if StepCode accepts it. Words StepCode already uses, like Write, are not allowed either.*

### Declare before you use

A variable is a named box that holds one value, like a number or a piece of text. Before you use a variable, you declare it with `Define`, its name and its data type:

* `Define age As Integer;` for whole numbers, like `17`.
* `Define price As Real;` for numbers with decimals, like `12.5`.
* `Define name As String;` for text, like `"Ana"`.

You can declare several variables of the same type at once: `Define n1, n2, total As Real;`. There are more types, and the [lesson on variables and data types](/blog/programming-fundamentals-variables-and-data-types/) explains all of them.

### Assignment uses an arrow

To store a value in a variable, use an arrow: `<-` (or `←`). Read `x <- 5;` as "x gets 5". The right side is calculated first, and then the result goes into the variable on the left, so `x <- x + 1;` means "x gets its current value plus one".

*Figure: The right side is calculated first, with the old value of x. Only then does the arrow put the new value in the box.*

Now try it in the editor. Use the step buttons to run one line at a time and watch `x` change in the **Variables** panel.

**Assignment** (StepCode)

```stepcode
Program Assignment
    Define x As Integer;
    x <- 5;
    Write "x is ", x;
    x <- x + 1;
    Write "Now x is ", x;
EndProgram
```

Press **Run**. You should see:

```
x is 5
Now x is 6
```

On the right side, you can use the basic math operators: `+` (add), `-` (subtract), `*` (multiply), `/` (divide) and `^` (power), with parentheses to group. The [lesson on operators and expressions](/blog/programming-fundamentals-operators-and-expressions/) covers them all.

### Write shows and Read asks

`Write` shows values in the console. You can list several values separated by commas, and StepCode writes them one after the other on the same line: `Write "The sum is ", total;`. Notice the space inside the quotes. StepCode adds no spaces for you.

`Read` waits for the user to type a value and stores it in a variable: `Read n1;`. The variable must be declared first, and the value is converted to the variable's type. Always `Write` a message before a `Read`, so the user knows what to type.

## The three parts of a program

Most programs, from a calculator to a banking app, do three things in order:

1. **Input.** The program gets the data it needs, from the user, a file or another program.
2. **Process.** The program works with that data to solve the problem.
3. **Output.** The program shows, saves or sends the result.

![Input, processing and output: the three parts of a program](https://storage.rolandoandrade.me/blog/programming-fundamentals-pseudocode-program-parts.webp)

In a StepCode program, you can point at each part. The `Read` lines are the input, the assignments are the process and the final `Write` is the output. Declarations come before all of them. In [Input, process and output](/blog/programming-fundamentals-sequential-structures/) you build a complete program around these three parts.

*Figure: Every line of the sum program has one job. Run it line by line: the input fills n1 and n2, the process fills total, and the output writes the result.*

When you read code, from a classmate or from an AI agent, start by finding these three parts. They tell you what the program needs, what it does and what it gives back.

### Four kinds of instructions

Every instruction in a program belongs to one of four kinds:

* **Start and end.** They mark where the program begins and stops. StepCode uses `Program` and `EndProgram`. Python uses indentation. [C family languages](https://en.wikipedia.org/wiki/List_of_C-family_programming_languages) use braces (`{` and `}`).
* **Assignment.** It gives a value to a variable, like `total <- n1 + n2;`.
* **Input and output.** They talk to the user or to files, like `Read` and `Write`.
* **Control.** They decide which instruction runs next: choosing between paths or repeating steps. You will learn them in the lessons on [conditionals](/blog/programming-fundamentals-conditionals/) and [loops](/blog/programming-fundamentals-loops/).

## Guided practice: your first programs

Now you write the code. Each part gives you a problem from the course and a working solution. Type it yourself in the editor before you look at the solution: you learn much more that way.

### 1. Add two numbers

This was the first example of the unit: ask the user for two numbers and show their sum. It uses every rule from this lesson. Find the comments: they mark the input, the process and the output.

**Sum of two numbers** (StepCode)

```stepcode
Program SumTwoNumbers
    // Declare every variable before you use it
    Define n1, n2, total As Real;

    // Input: ask for two numbers
    Write "Enter the first number:";
    Read n1;
    Write "Enter the second number:";
    Read n2;

    // Process: add them
    total <- n1 + n2;

    // Output: show the result
    Write "The sum is ", total;
EndProgram
```

Press **Run** and type `5` and `3.5`. You should see:

```
Enter the first number:
Enter the second number:
The sum is 8.5
```

For comparison, here is the same program in Python, the language the course used next to pseudocode:

```python
n1 = float(input("Enter the first number: "))
n2 = float(input("Enter the second number: "))
total = n1 + n2
print("The sum is", total)
```

The steps are the same. Only the words change. You will learn how that translation works in the next lesson.

### 2. Calculate the area of a triangle

The class activity: calculate the area of a triangle from its base and its height. The formula is base times height, divided by 2. You wrote these steps in plain English in exercise 3 of the last lesson.

**Area of a triangle** (StepCode)

```stepcode
Program TriangleArea
    Define base, height, area As Real;

    Write "Base of the triangle:";
    Read base;
    Write "Height of the triangle:";
    Read height;

    area <- base * height / 2;

    Write "The area is ", area;
EndProgram
```

Run it with `10` and `4`. You should see:

```
Base of the triangle:
Height of the triangle:
The area is 20
```

### 3. Greet the user by name

Programs work with text too. This one, from the practice guide, asks for a first name and a last name and answers with a greeting. The variables are `String`, and the `Write` puts a space between the two names.

**Greeting** (StepCode)

```stepcode
Program Greeting
    Define firstName, lastName As String;

    Write "Enter your first name:";
    Read firstName;
    Write "Enter your last name:";
    Read lastName;

    Write "Hello ", firstName, " ", lastName;
EndProgram
```

Run it with `Pedro` and `Perez`. You should see:

```
Enter your first name:
Enter your last name:
Hello Pedro Perez
```

Try removing the `" "` in the middle of the last line and run it again. You get `Hello PedroPerez`, because StepCode adds no spaces for you.

## Common mistakes

**If you see `"total" is not declared.`, you used a variable without a `Define`.** Add a line like `Define total As Real;` before the first line that uses it. StepCode checks this before the program runs.

**If you see `I did not expect "=" here.`, you used `=` to assign a value.** Many languages, like Python, assign with `=`. StepCode uses the arrow, so write `total <- 5;`. In StepCode, `=` compares two values, which you will use in conditionals.

**If you see `This statement is missing its ";".`, an instruction has no semicolon.** The editor marks the line. Add the `;` at the end of it. You saw this error in the [first lesson](/blog/programming-fundamentals-design-and-development-of-algorithms/).

**If the output looks glued together, like `The sum is8.5`, you forgot a space inside the quotes.** Write `"The sum is "` with the space before the closing quote.

## Pseudocode or programming language?

| | Pseudocode | Programming language |
|---|---|---|
| Made for | People who design and share algorithms | Computers that run programs |
| Rules | Few and forgiving | Many and strict |
| Keywords | Close to plain language (`Read`, `Write`) | Set by each language (`input`, `print`) |
| Runs on a computer | Only with a tool like StepCode or PSeInt | Yes, that is its purpose |
| Used for | Learning, planning, explaining | Building real software |

Pseudocode does not replace a programming language. It is where you think before you build. Professional programmers still write pseudocode in comments, on whiteboards and in the instructions they give to AI agents.

## Practice exercises

These exercises come from the course practice guide. Try each one in an empty editor first. Then open the solution and compare. There are many correct solutions to each problem; yours does not need to match line by line.

**Exercise 1.** Ask the user for three numbers. Add the first two and divide the sum by the third. With `5`, `3.5` and `2`, the result is `4.25`.

<details>
<summary>Solution to exercise 1</summary>

**Sum and divide** (StepCode)

```stepcode
Program SumAndDivide
    Define n1, n2, n3, total, result As Real;

    Write "Enter the first number:";
    Read n1;
    Write "Enter the second number:";
    Read n2;
    Write "Enter the third number:";
    Read n3;

    total <- n1 + n2;
    result <- total / n3;

    Write "The result is: ", result;
EndProgram
```

With `5`, `3.5` and `2`, you should see:

```
Enter the first number:
Enter the second number:
Enter the third number:
The result is: 4.25
```

</details>

**Exercise 2.** Ask the user for their name and their age, and show both in one sentence. Use `String` for the name and `Integer` for the age.

<details>
<summary>Solution to exercise 2</summary>

**Name and age** (StepCode)

```stepcode
Program NameAndAge
    Define name As String;
    Define age As Integer;

    Write "Enter your name:";
    Read name;
    Write "Enter your age:";
    Read age;

    Write "I am ", name, " and I am ", age, " years old";
EndProgram
```

With `Pedro` and `17`, you should see:

```
Enter your name:
Enter your age:
I am Pedro and I am 17 years old
```

</details>

**Exercise 3.** Scientists on a hidden island found a particle called the Energion. It releases energy when it breaks apart. Calculate that energy with Einstein's formula, E = mc², where m is the mass in kilograms and c is the speed of light, 299792458 meters per second.

<details>
<summary>Solution to exercise 3</summary>

**Energion energy** (StepCode)

```stepcode
Program Energion
    Define mass, lightSpeed, energy As Real;

    // The speed of light in meters per second
    lightSpeed <- 299792458;

    Write "Mass of the Energion in kilograms:";
    Read mass;

    // Einstein's formula: E = m * c^2
    energy <- mass * lightSpeed * lightSpeed;

    Write "The Energion releases ", energy, " joules";
EndProgram
```

With a mass of `0.001` (one gram), you should see:

```
Mass of the Energion in kilograms:
The Energion releases 89875517873681.77 joules
```

That is about 90 trillion joules from one gram. The last digits come from small rounding in how computers store decimal numbers. You solve this problem again with a constant in [Variables and data types](/blog/programming-fundamentals-variables-and-data-types/#4-use-a-constant).

</details>

**Exercise 4.** In a world where snails are the fastest animals, you organize the Great Snail Race. Each snail moves at a constant speed. Calculate how long a snail takes to finish the track. In physics, time equals distance divided by speed.

<details>
<summary>Solution to exercise 4</summary>

**Snail race** (StepCode)

```stepcode
Program SnailRace
    Define distance, speed, time As Real;

    Write "Length of the track in meters:";
    Read distance;
    Write "Speed of the snail in meters per second:";
    Read speed;

    // Uniform motion: time = distance / speed
    time <- distance / speed;

    Write "The snail needs ", time, " seconds";
EndProgram
```

With a track of `3` meters and a speed of `0.002` meters per second, you should see:

```
Length of the track in meters:
Speed of the snail in meters per second:
The snail needs 1500 seconds
```

What happens if a snail has a speed of `0`? Try it. You get the runtime error from the first lesson.

</details>

## What you learned

* Pseudocode is a bridge between plain language and code: exact enough to run, simple enough to read.
* A StepCode program starts with `Program Name` and ends with `EndProgram`, and each instruction ends with `;`.
* You declare variables with `Define`, store values with `<-`, show values with `Write` and ask for values with `Read`.
* Most programs have three parts in order: input, process and output. Finding them is the first step to reading any program.

## Next steps

* Continue with [Programming languages](/blog/programming-fundamentals-programming-languages/), where you learn how code like this becomes something a computer runs, and how to translate pseudocode to Python.
* Go back to [Algorithms](/blog/programming-fundamentals-design-and-development-of-algorithms/) if you want to review the four traits and the three kinds of errors.

* * *

If you enjoy the content, please don't hesitate to subscribe and leave a comment! I would love to connect with you and hear your thoughts on the topics I cover.

[Book a call with Rolando](https://rolandoandrade.me/pricing/)
