Coding Guide

How to Write Pseudocode: Rules and Examples

The conventions most courses expect, the standard keywords, worked examples from a simple loop to binary search, and how to turn pseudocode into real code.

Updated October 2026 · 7 min read

To write pseudocode, you describe an algorithm step by step in structured plain language that any programmer could turn into code. It sits between an English description and a real programming language.

This guide covers the rules most courses expect, a table of standard keywords, worked examples from a simple loop to binary search and sorting, how to convert pseudocode into Python, and the mistakes that cost marks.

The Rules of Good Pseudocode

  • Write one action per line, in the order it happens.
  • Use capitalised keywords for structure: IF, ELSE, WHILE, FOR, RETURN.
  • Indent the body of every loop and condition, and close blocks clearly (END IF, END WHILE).
  • Name variables meaningfully and use ← or = for assignment consistently.
  • Leave out language details such as semicolons, type declarations and library calls.

What Pseudocode Is and Why Courses Use It

Pseudocode lets you focus on the logic of a solution without worrying about syntax. That is why algorithms courses, exam papers and design documents use it.

There is no single official standard. Textbooks, exam boards and lecturers each have their own conventions, so check your course materials first. What every version shares is clarity: a reader should be able to trace the algorithm by hand and get the right answer.

Good pseudocode is precise about logic and relaxed about syntax. "Sort the list" is too vague if sorting is the task; "swap A[j] and A[j + 1]" is the right level of detail.

Standard Pseudocode Keywords

PurposeCommon keywordsExample
Input and outputINPUT, READ, OUTPUT, PRINTINPUT n
Assignment← or SET ... TOtotal ← 0
SelectionIF ... THEN, ELSE IF, ELSE, END IFIF x > max THEN
Counted loopFOR ... TO ... DO, END FORFOR i ← 1 TO n DO
Condition loopWHILE ... DO, END WHILEWHILE low ≤ high DO
Post-test loopREPEAT ... UNTILREPEAT ... UNTIL valid
SubroutinesFUNCTION, PROCEDURE, RETURN, CALLFUNCTION Max(A)

Pick one form for each idea and stick to it. Mixing "←" and "=" for assignment, or "END IF" and "ENDIF", looks careless.

How to Write Pseudocode, Step by Step

  1. Understand the problem. Write the inputs, the output and any constraints.
  2. Solve a small example by hand. Note each step you take.
  3. Turn those steps into lines. One action per line, in order.
  4. Add structure. Wrap repeated steps in loops and decisions in IF blocks, with indentation.
  5. Trace it. Run your example through the pseudocode line by line and check the result.
  6. Test edge cases. Empty input, one item, duplicates and the largest values.

Worked Example: Finding the Largest Value

Start with a simple loop. This algorithm finds the largest number in a non-empty list.

FUNCTION FindMax(A)
    max ← A[0]
    FOR i ← 1 TO length(A) - 1 DO
        IF A[i] > max THEN
            max ← A[i]
        END IF
    END FOR
    RETURN max
END FUNCTION

Trace with A = [4, 9, 2, 7]. max starts at 4. At i = 1, 9 > 4, so max ← 9. At i = 2, 2 is not greater than 9. At i = 3, 7 is not greater than 9. The function returns 9, which is correct.

The loop runs n - 1 times, so the algorithm is O(n). Note the assumption: the list must not be empty, and good pseudocode states that, for example in a comment.

Worked Example: Binary Search

Binary search is a favourite exam question because it tests loop conditions and index updates. The list must already be sorted.

FUNCTION BinarySearch(A, target)
    low ← 0
    high ← length(A) - 1
    WHILE low ≤ high DO
        mid ← floor((low + high) / 2)
        IF A[mid] = target THEN
            RETURN mid
        ELSE IF A[mid] < target THEN
            low ← mid + 1
        ELSE
            high ← mid - 1
        END IF
    END WHILE
    RETURN -1
END FUNCTION

Trace with A = [3, 8, 15, 21, 30] and target = 21. low = 0, high = 4, mid = 2, A[2] = 15 < 21, so low ← 3. Now mid = floor(7 / 2) = 3, A[3] = 21, so the function returns 3.

Each pass halves the search range, so binary search takes O(log n) comparisons; a sorted list of 1,000 items needs at most 10 passes. Returning -1 signals that the target is not in the list.

The two classic bugs are writing low < high instead of low ≤ high, which misses a one-element range, and setting low ← mid instead of mid + 1, which can loop forever.

Worked Example: Bubble Sort

Nested loops are where indentation really matters. This version stops early if a pass makes no swaps.

PROCEDURE BubbleSort(A)
    n ← length(A)
    REPEAT
        swapped ← FALSE
        FOR i ← 0 TO n - 2 DO
            IF A[i] > A[i + 1] THEN
                swap A[i] and A[i + 1]
                swapped ← TRUE
            END IF
        END FOR
        n ← n - 1
    UNTIL swapped = FALSE
END PROCEDURE

After each pass the largest remaining value has moved to the end, so the next pass can stop one place earlier. The worst case is O(n²) comparisons; on an already sorted list the early exit makes it O(n).

Turning Pseudocode into Code

Well-written pseudocode converts almost line for line. Here is the binary search in Python.

def binary_search(a, target):
    low, high = 0, len(a) - 1
    while low <= high:
        mid = (low + high) // 2
        if a[mid] == target:
            return mid
        elif a[mid] < target:
            low = mid + 1
        else:
            high = mid - 1
    return -1

Notice the small translations: ← becomes =, the comparison = becomes ==, floor division becomes //, and END blocks disappear because Python uses indentation. In Java or C++, integer division of two ints already discards the remainder.

Common Pseudocode Mistakes

MistakeFix
Writing real code with semicolons and typesStrip the syntax and keep the logic
Steps that are too vague ("process the data")Break them into concrete actions
No indentation or unclosed blocksIndent bodies and close every block
Same symbol for assignment and comparisonUse ← for assignment and = for comparison
Off-by-one loop boundsState whether arrays start at 0 or 1, and trace the first and last pass
Ignoring empty or edge casesHandle them or state the assumption

Pseudocode is like loading a pack animal: put things in a sensible order and the journey is smooth. Trace every algorithm by hand before you hand it in.

Pseudocode Versus Flowcharts

Some briefs ask for a flowchart instead of, or as well as, pseudocode. Both show the same logic; they suit different readers.

  • Pseudocode is compact, easy to edit and close to real code, so it suits algorithms and longer programs.
  • Flowcharts use ovals for start and end, rectangles for processes, diamonds for decisions and parallelograms for input and output. They suit short processes and non-technical readers.

How STEM Donkey Helps with Pseudocode

Send the problem, your course's pseudocode conventions or a sample from your notes, the language for any code version and the rubric.

You receive a custom solution written from scratch: clear pseudocode in your course's style, a hand trace on an example, matching code if needed and a complexity analysis. It is for study and reference. Free revisions within the original scope are included for 14 days.

Want Clear Pseudocode and Code for Your Task?

Send your brief and any style rules your course uses. You get a custom solution with readable pseudocode, matching code and an explanation of each step.

Order Your Pseudocode Solution

Free revisions within scope for 14 days · Full refund if late · Written from scratch for your order

Frequently Asked Questions

Is there one correct way to write pseudocode?

No. There is no official standard, so follow your textbook or exam board. What matters is consistent keywords, clear structure and enough detail to trace by hand.

Should arrays start at 0 or 1 in pseudocode?

Either, as long as you are consistent and say which. Many textbooks start at 1; most programming languages start at 0.

How detailed should pseudocode be?

Detailed enough that a programmer could write code from it without guessing, but without language-specific syntax.

Can I use functions in pseudocode?

Yes. Define them with FUNCTION or PROCEDURE, give them parameters and use RETURN for results.

What is the difference between pseudocode and an algorithm?

An algorithm is the method itself. Pseudocode is one way to write it down, alongside flowcharts and real code.

Do exam markers accept small syntax slips in pseudocode?

Usually yes, if the logic is clear and correct. Unclear structure or wrong logic costs far more than a missing keyword.

How do I write a loop in pseudocode?

Use FOR i ← 1 TO n DO for a fixed number of passes, WHILE condition DO when you repeat until something changes, and REPEAT ... UNTIL when the body must run at least once. Indent the body and close it with END FOR or END WHILE.

Should I include comments in pseudocode?

Yes, briefly. A short comment stating an assumption, such as "A is sorted in ascending order", or the purpose of a block helps the reader and the marker.

Can pseudocode show complexity?

Not directly, but it makes complexity easy to analyse. Count how many times each loop runs in terms of n, then state the result in Big O notation beneath the pseudocode.