Pseudo-code is a way of writing algorithms that reads like code written in actual programming languages.

Pseudo-code is not a real programming language. The idea is that pseudo-code clearly shows an algorithm's steps without worrying about the syntax (finer details) of any particular programming language.


There are three basic programming constructs which we use when writing pseudo-code:

Sequence is just a matter of writing of writing down the steps in the correct order.

Selection allows you to choose between multiple options.

Example 1 shows how an IF statement can be used in pseudo-code.

Example 1
age ← USERINPUT
IF age ≥ 18 THEN
OUTPUT "OK"
ELSE
OUTPUT "Too young"
ENDIF

Iteration means looping. There are three different types of loops in pseudo-code.

Example 2 shows an algorithm containing a FOR ... ENDFOR loop. The algorithm asks the user for five numbers and outputs their sum.

Example 2
total ← 0
FOR count ← 1 TO 5
  num ← USERINPUT
  total ← total + num
ENDFOR
OUTPUT total

Example 3 shows an algorithm containing a REPEAT ... UNTIL loop. The algorithm counts up until the user stops it.

Example 3
count ← 0
REPEAT
  count ← count + 1
OUTPUT count
  end ← USERINPUT
UNTIL end = "yes"

Example 4 shows an algorithm containing a WHILE ... ENDWHILE loop. The algorithm keeps asking the user for a password until they get it correct.

Example 4
password ← USERINPUT
WHILE password ≠ "abcd"
OUTPUT "Try again"
  password ← USERINPUT
ENDWHILE
OUTPUT "Correct password"

REPEAT ... UNTIL loops and WHILE ... ENDWHILE loops are very similar, but there are some key differences.

Firstly, REPEAT ... UNTIL loops keep looping until the condition is true, whereas WHILE ... ENDWHILE loops keep looping while the condition is true.

Secondly, REPEAT ... UNTIL loops check the condition at the end, so will always run at least once, but WHILE ... ENDWHILE loops check their condition at the start, so won't run if the condition starts off as false.


Example 5 shows an algorithm written in pseudo-code.

Example 5
result ← 1
FOR num ← 1 TO 10
IF num MOD 2 = 0 THEN
    result ← result * num
ENDIF
ENDFOR
OUTPUT result

Note: num MOD 2 means the remainder from dividing num by 2. We'll learn more about this in 2.03 (Operations).



What does the code in Example 5 do?

Multiplies all the even numbers from 1 to 10 and outputs the result.