Basic

Writing pseudocode

Describe an algorithm in plain, structured English that reads like code but is not tied to any language.

What you will see

4

About 2 min of reading

Quick check

Before this new lesson, one small question about the lesson you just finished. Recall it, do not look it up.

Try It

Pseudocode reads like code but belongs to no language. I translate its SET, OUTPUT, IF/ELSE, and FOR keywords into runnable Python so the same logic runs in every environment.

1

I translate the SET keyword by assigning a value to a name, then translate OUTPUT by printing it. A stored value written out shows how the keywords become real Python.

total = 0 print(total)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
0
SET just assigns, it shows nothing. Only OUTPUT / a print makes the value visible on screen.
2

I translate an IF/ELSE login check. Two conditions must both be true at once, so the success branch runs and prints the result; otherwise the failure branch would run.

username = "ada" password = "hunter2" if username == "ada" and password == "hunter2": print("Login successful") else: print("Invalid credentials")

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Login successful
The Python 'and' maps directly to the pseudocode 'AND'. Both sides must be true for the branch to run.
3

I translate a FOR counting loop. The body runs once per item, adding one each pass, then I print the final count to show how many items were visited.

items = ["apple", "banana", "cherry", "date"] count = 0 for item in items: count = count + 1 print(count)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
4
A FOR loop visits every item in the collection exactly once, so the count equals the number of items.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

Pseudocode reads input, runs a loop or branch, and outputs the result in language-independent form.
1 Write INPUT for the data needed
↓
2 Use IF to branch on a condition
↓
3 Use FOR/WHILE to repeat
↓
4 OUTPUT the final answer
Line by line

Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.

total = 0 sets nothing on screen
print(total) 0
Why this works
Common keywords

Pseudocode uses a few standard keywords:

  • INPUT / READ: take data in.
  • OUTPUT / PRINT: show data.
  • IF / ELSE / END IF: branch.
  • FOR / WHILE: loop.
  • SET: assign a value.

SET total = 0
WHILE items remain
    SET total = total + next item
END WHILE
OUTPUT total

You can translate these keywords almost directly into Python, Java, or JavaScript.

Why not just write real code?

Pseudocode is a design tool. You can plan the logic before you worry about semicolons or type declarations. Writing it first means fewer bugs later.

# Pseudocode (logic)
IF player has no lives:
    PRINT "Game over"

# later -> real Python
if player.lives <= 0:
    print("Game over")

The pseudocode stays correct even if you switch languages.

Examples

A simple login check

Pseudocode for checking a login

INPUT username, password
STORE real_user, real_password
IF username == real_user
    AND password == real_password
    PRINT "Login successful"
ELSE
    PRINT "Invalid credentials"
END IF

Why it works: pseudocode focuses on the logic, not the syntax, so anyone can read it.

Looping to count

Counting with a loop

SET count = 0
FOR each item in the list
    SET count = count + 1
PRINT count

Pseudocode lets you sketch a loop without remembering whether a language uses for, while, or foreach.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Reading & drawing flowcharts
Previous Lesson
Reading & drawing flowcharts
Next Lesson
Conditionals & branching
Conditionals & branching →