What Is Pseudo Code? Rules, Examples and How to Write It

Pseudo code, usually written pseudocode, describes the steps of an algorithm (a plan for solving a problem) in words a person can follow. A computer does not run pseudocode. Start by writing one action per line. Use IF for a choice and WHILE for steps that repeat. Indent the lines that belong inside a choice or a repeat.

Painted notebook with a hand writing rows of indented steps beside a laptop
The indented lines group the steps before coding begins.

What is pseudocode, and why do programmers use it?

Pseudocode is a plan for a program, written in plain words before you write any code. Programmers use it to test an idea quickly, without writing the whole program.

The plan is not tied to one programming language. Teammates can read it and comment on it, whichever language they code in.

A programming language has syntax (rules for how to write code). Pseudocode lets you choose a format as long as a reader can follow it.

What are the rules for writing pseudocode?

Write one action per line, in plain words that say what should happen. Before the first line, name the result you want and the information the program needs. For Cal Poly’s temperature example further down, the result is a count of readings above freezing and the information is each reading.

Use the words of the problem, not the words of a programming language. In each pair below, from Cal Poly’s course guide, the first line is poor pseudocode and the second is good.

set word to get next token
Extract the next word from the line

The first line uses a programmer’s word, token, and the reader has to guess what it stands for. The second line says plainly what to take: the next word.

name = name + extension
Append the file extension to the name

The first line is already code, and its symbols belong to a programming language. The second line says what should happen: add the file extension (the last part of a file name) to the name.

Give a decision or loop (steps that repeat) its own line. Indent a nested step (a step inside a decision or loop) so its place is clear.

Use names that say what each variable holds. A variable stores a value such as a temperature or a total. Write the keywords in capitals so they stand out from the names you choose.

Which keywords does pseudocode use?

Use the word that tells the reader what kind of action comes next. This table follows the Cal Poly course guide. Your class or team may use different forms.

PatternWhat it doesKeywords
SequenceDoes one action after another, from top to bottom.READ, SET, PRINT
ChoiceChecks a condition (a test that can be true or false). The ELSE part is optional.IF, THEN, ELSE, ENDIF
Test firstRepeats while a condition is true. The first test can skip the whole loop.WHILE, ENDWHILE
Test lastRuns the steps once before the test. Stops when the condition becomes true.REPEAT, UNTIL
Counting loopRepeats while moving through a set of counts.FOR
Many choicesPicks a path based on a value.CASE

GET can ask for a value. DISPLAY can show one. COMPUTE tells the reader to work out a value. INCREMENT adds one to a count, and CALL means to use a separate set of steps.

Cal Poly writes a choice with an ELSE part like this:

IF HoursWorked > NormalMax THEN
    Display overtime message
ELSE
    Display regular time message
ENDIF

If the hours are above the normal maximum, the first message shows; if not, the second one does.

A test-first loop from the same guide looks like this:

WHILE Population < Limit
    Compute Population as Population + Births - Deaths
ENDWHILE

What does a real pseudocode example look like?

  1. Read the next temperature
  2. Add one to the total if the temperature is above freezing
  3. Check whether the temperature is below zero
  4. The loop runs again if the temperature is zero or higher

A real pseudocode example is a short block of lines, like this one from Cal Poly’s course guide. It puts a choice inside a loop, and the indentation shows where the choice starts and ends.

SET total to zero
REPEAT
    READ Temperature
    IF Temperature > Freezing THEN
        INCREMENT total
    END IF
UNTIL Temperature < zero
Print total

The first line sets the total to zero. REPEAT starts the loop, and the lines down to UNTIL run once for each reading.

READ tells the program to read the next temperature. The IF line and the INCREMENT line under it make the choice. The program will add one to the total only if the temperature is above freezing. END IF closes the choice.

UNTIL makes the program check whether the temperature is below zero. The loop runs again if the temperature is zero or higher. A reading below zero ends the loop, and the last line then shows the total.

The block is shown as Cal Poly published it. That is why the last line says Print, not PRINT, and the choice ends with END IF, not ENDIF. In your own pseudocode, pick one spelling and keep to it.

How is AP CSP exam pseudocode different?

The College Board gives AP Computer Science Principles (AP CSP) students a reference sheet with its own rules. Use those forms when you read an exam question.

InstructionWhat it does on the exam
DISPLAY(expression)Shows a value with a space after it.
INPUT()Gets a value from the user.
17 MOD 5Gives 2, the amount left over after dividing 17 by 5.
REPEAT n TIMESRuns the steps the number of times given by n.
REPEAT UNTIL(condition)Repeats the steps until the test is true.
FOR EACH item IN aListUses each item in order from first to last.
aList[1]Gets the first item. An index is the item’s place in the list.

If an index is below 1 or past the end of the list, the exam rules say the program shows an error and stops.

How do I check my pseudocode before I write code?

Work through the steps by hand. This is a dry run. Use a trace table (a table of the values after each step) to see where the result changes.

For the temperature loop, make a column for the reading and a column for the total. Try a reading above freezing, then zero, then a reading below zero. Check that you only count readings above freezing. If zero is the freezing point, the zero reading leaves the total alone and the loop keeps going.

How do I turn pseudocode into real code?

Keep the plain steps as comments (notes the computer skips) while you turn each step into code. Add the code next to the step it carries out. If a comment merely repeats a clear line of code, cut the comment.

If you cannot turn a line into code without making a new decision, the line needs more detail. Work out the decision in your plan first.

Frequently asked questions

How can I convert code to pseudocode?

Read the code in order and write down what each step does. Keep the tests and repeats, and name the result each group of steps produces.

Do I have to write pseudocode in capital letters?

No. Capitals are a way to make commands easy to spot. Use the form your teacher or team asks for.

Sources

Published

Chat on WhatsApp