Pseudocode
IB Syllabus: Pairs with B1.1.4 (the text-based partner to tracing flowcharts) and supports B1.1.2 (algorithmic design). Pseudocode is a planning skill rather than a separate exam topic in the current course, where finished code is written in Java or Python.
Table of Contents
- Why This Page Exists
- What Pseudocode Is (and Is Not)
- A Common Set of Keywords
- The Three Building Blocks
- From Pseudocode to Real Code
- Reading and Tracing Pseudocode
- Pseudocode, Flowcharts, or Code: Which and When?
- Quick Check
- Trace Exercise
- Code Completion
- Practice Exercises
- Connections
Why This Page Exists
Before you write real code, you need to work out what the code should do. Pseudocode is how you sketch that logic in plain, structured English, without getting stuck on the exact rules of a programming language.
A flowchart shows an algorithm as a picture. Pseudocode shows the same algorithm as text. Both are planning tools: they let you think through the steps, spot mistakes, and explain your idea to someone else before a single line of Java or Python is written. Pseudocode has one extra advantage: it sits very close to real code, so translating it into Java or Python at the end is almost mechanical.
There is no single official pseudocode standard, and that is the point. Pseudocode is for humans. As long as your meaning is clear and your steps are unambiguous, you are doing it right. The conventions below are a common, readable set that this site uses everywhere, and they match the text you already saw in the flowchart trace tables.
What Pseudocode Is (and Is Not)
Pseudocode is a way of writing the steps of an algorithm using keywords for the common actions (input, output, decisions, loops) and plain English for everything else.
Pseudocode is not a language a computer can run. You cannot “execute” pseudocode. It has no compiler, no strict grammar, and no single correct spelling. If two people write the same algorithm slightly differently, both can be right.
The test of good pseudocode is simple: could another student follow it, step by step, and get the result you intended? If yes, it works. If they would have to guess what you meant, it needs more detail.
A Common Set of Keywords
These are the building blocks used across this site. Each maps cleanly onto Java and Python.
| Pseudocode | Meaning | Java | Python |
|---|---|---|---|
INPUT name | Read a value from the user | name = input.nextLine(); | name = input() |
OUTPUT total | Display a value | System.out.println(total); | print(total) |
count = 0 | Assignment (store a value) | int count = 0; | count = 0 |
IF ... THEN ... ELSE ... END IF | Selection (a decision) | if (...) { } else { } | if ...: else: |
WHILE ... END WHILE | Repeat while a condition holds | while (...) { } | while ...: |
FOR i FROM 0 TO n - 1 | Repeat a counted number of times | for (int i = 0; i < n; i++) | for i in range(n): |
REPEAT ... UNTIL ... | Repeat until a condition becomes true | do { } while (!...); | (loop with a flag) |
scores[i] | An item in a list or array | scores[i] | scores[i] |
length of scores | How many items | scores.length | len(scores) |
MOD / DIV | Remainder / whole-number division | % / / (integers) | % / // |
Indentation matters. The lines inside an IF, WHILE, or FOR are indented so a reader can see exactly where the block begins and ends. This is the single most important habit, because it is the one Python actually enforces in real code.
The Three Building Blocks
Every algorithm is built from just three kinds of step. Pseudocode has a shape for each.
1. Sequence (do this, then that)
Steps run top to bottom, in order.
START
price = 20
tax = price * 0.1
total = price + tax
OUTPUT total
END
2. Selection (choose a path)
An IF picks between paths based on a condition.
START
INPUT age
IF age >= 18 THEN
OUTPUT "Can vote"
ELSE
OUTPUT "Cannot vote"
END IF
END
3. Iteration (repeat)
A loop repeats steps. Use WHILE when you do not know in advance how many times, and FOR when you do.
START
count = 5
WHILE count > 0
OUTPUT count
count = count - 1
END WHILE
OUTPUT "Liftoff!"
END
From Pseudocode to Real Code
The reason pseudocode is worth learning is how directly it becomes real code. Here are three complete algorithms, each shown first as pseudocode, then translated into Java and Python. Use the tabs to compare the two languages.
Example 1: Average of a list
Add up every score, then divide by how many there are.
START
scores = [72, 85, 90, 64]
total = 0
FOR i FROM 0 TO length of scores - 1
total = total + scores[i]
END FOR
average = total / length of scores
OUTPUT "Average:", average
END
The same algorithm as runnable code:
public class Average {
public static void main(String[] args) {
int[] scores = {72, 85, 90, 64};
int total = 0;
for (int i = 0; i < scores.length; i++) {
total = total + scores[i];
}
double average = (double) total / scores.length;
System.out.println("Average: " + average);
}
}
scores = [72, 85, 90, 64]
total = 0
for i in range(len(scores)):
total = total + scores[i]
average = total / len(scores)
print("Average:", average)
Output:
Average: 77.75
Notice one real-code detail the pseudocode glosses over: in Java, dividing two integers throws away the decimals, so the total is turned into a double first. Pseudocode lets you ignore that while planning, then you handle it when you translate. That is exactly the division of labour pseudocode is for.
Example 2: Find the largest value
Assume the first item is the largest, then check every other item against it.
START
heights = [150, 172, 168, 181, 159]
tallest = heights[0]
FOR i FROM 1 TO length of heights - 1
IF heights[i] > tallest THEN
tallest = heights[i]
END IF
END FOR
OUTPUT "Tallest:", tallest
END
public class Tallest {
public static void main(String[] args) {
int[] heights = {150, 172, 168, 181, 159};
int tallest = heights[0];
for (int i = 1; i < heights.length; i++) {
if (heights[i] > tallest) {
tallest = heights[i];
}
}
System.out.println("Tallest: " + tallest);
}
}
heights = [150, 172, 168, 181, 159]
tallest = heights[0]
for i in range(1, len(heights)):
if heights[i] > tallest:
tallest = heights[i]
print("Tallest:", tallest)
Output:
Tallest: 181
Start the search at the first item (tallest = heights[0]), not at zero. If you start at zero, a list of all-negative numbers would wrongly report zero as the largest.
Example 3: Count with a condition
A WHILE loop that keeps a running count. This one counts down and then reports.
START
count = 5
evens = 0
WHILE count > 0
IF count MOD 2 = 0 THEN
evens = evens + 1
END IF
count = count - 1
END WHILE
OUTPUT "Even numbers seen:", evens
END
public class CountEvens {
public static void main(String[] args) {
int count = 5;
int evens = 0;
while (count > 0) {
if (count % 2 == 0) {
evens = evens + 1;
}
count = count - 1;
}
System.out.println("Even numbers seen: " + evens);
}
}
count = 5
evens = 0
while count > 0:
if count % 2 == 0:
evens = evens + 1
count = count - 1
print("Even numbers seen:", evens)
Output:
Even numbers seen: 2
Counting down from 5, the even values are 4 and 2, so the answer is 2. MOD (written % in both Java and Python) gives the remainder, and a remainder of 0 means the number divides evenly.
Reading and Tracing Pseudocode
Reading pseudocode is the same skill as tracing a flowchart: follow it one line at a time and keep a trace table of every variable.
The single most useful habit is to write down a variable’s value after every step that could change it, even if it stayed the same. A blank cell in a trace table is where mistakes hide.
For the countdown-and-count algorithm in Example 3, the trace looks like this:
| count | count > 0? | count MOD 2 = 0? | evens |
|---|---|---|---|
| 5 | Yes | No | 0 |
| 4 | Yes | Yes | 1 |
| 3 | Yes | No | 1 |
| 2 | Yes | Yes | 2 |
| 1 | Yes | No | 2 |
| 0 | No | (exit) | 2 |
Output: Even numbers seen: 2.
Pseudocode, Flowcharts, or Code: Which and When?
All three describe algorithms. They are good at different moments.
| Tool | Best for | Weak at |
|---|---|---|
| Flowchart | Seeing the shape of the logic at a glance, especially branches and loops | Long or detailed algorithms (the diagram gets huge) |
| Pseudocode | Planning the exact steps quickly, close to code, easy to edit | Showing the overall shape visually |
| Real code | The finished, runnable solution | Planning and communicating an idea (too much syntax noise) |
A common workflow: a flowchart to sketch the shape, pseudocode to nail down the steps, then real code to build it. You will not always use all three, but knowing what each is for saves time.
Quick Check
Q1. What is the main purpose of pseudocode?
Q2. Which pseudocode keyword is used to repeat a block of steps?
Q3. Given temperature = 30, what does this print?
IF temperature >= 25 THEN OUTPUT "Warm" ELSE OUTPUT "Cool" END IF
Q4. Trace this algorithm. What is the final output?
total = 0 · n = 3 · WHILE n > 0: total = total + n · n = n - 1 · END WHILE · OUTPUT total
Q5. You want to do something once for each position from 0 up to length - 1. Which construct fits best?
Q6. Why does indentation matter in pseudocode?
Trace Exercise
Trace this algorithm and fill in the table. It finds the smallest value in a list.
Trace: the list is prices = [8, 3, 11, 5]. Fill in smallest after each comparison.
smallest = prices[0]
FOR i FROM 1 TO length of prices - 1
IF prices[i] < smallest THEN
smallest = prices[i]
END IF
END FOR
OUTPUT smallest| i | prices[i] | prices[i] < smallest? | smallest |
|---|---|---|---|
| Init | -- | -- | |
| 1 | 3 | Yes | |
| 2 | 11 | No | |
| 3 | 5 | No |
Code Completion
Translate this pseudocode into Java. It doubles every number in a list and prints each result.
Pseudocode: FOR i FROM 0 TO length of nums - 1 · OUTPUT nums[i] * 2 · END FOR
int[] nums = {4, 7, 2};
for (int i = ; i < nums.; i++) {
System.out.println(nums[i] 2);
}
Practice Exercises
Core
-
Write pseudocode for an algorithm that reads a number and outputs “Positive”, “Negative”, or “Zero”.
-
Write pseudocode that adds up the numbers 1 to 10 and outputs the total. Use a
FORloop. -
Translate the pseudocode from Example 1 (average of a list) into whichever language you are learning, changing the list to five numbers of your choice.
Extension
-
Write pseudocode that reads numbers from the user until they enter 0, then outputs how many numbers were entered (not counting the 0). Then trace it with the inputs 4, 9, 2, 0.
-
Write pseudocode for an algorithm that counts how many values in a list are above a given threshold. Include a trace table for the list
[12, 5, 20, 8]and threshold10.
Challenge
- Design and translate. Write pseudocode for an algorithm that finds both the largest and the smallest value in a list in a single pass, then translate it into Java or Python. Trace it with
[6, 2, 9, 4, 7]to prove it works.
Connections
- Prerequisites: CT Concepts – pseudocode is how you write down an algorithmic design
- Related: Flowcharts – the visual partner to pseudocode; the same logic drawn as a diagram
- Forward: Selection – IF/THEN/ELSE pseudocode becomes if/else in real code
- Forward: Iteration – WHILE and FOR pseudocode becomes real loops
- Forward: 1D Arrays – the list examples above in full