An algorithm is a precise, step-by-step method for solving a problem. Before writing real code, programmers usually express algorithms in pseudocode or flowcharts. This lesson shows you how to write and read both, using the style familiar from IB exams.

What makes an algorithm?

A good algorithm is:

  • Unambiguous: each step is precise.
  • Finite: it ends after a finite number of steps.
  • Has inputs and outputs.
  • Effective: every step is something that can actually be done.
  • Correct: it produces the right result for all valid input.

Pseudocode

Pseudocode is structured plain language that resembles code but is not tied to any programming language. IB style uses these conventions:

// assignment
total = 0

// input and output
input name
output "Hello " + name

// selection
if mark >= 50 then
   output "Pass"
else
   output "Fail"
end if

// loop with a counter
loop I from 1 to 5
   output I
end loop

// loop with a condition
loop while count < 10
   count = count + 1
end loop

Example: an algorithm to find the largest value in an array NUMS of length N.

max = NUMS[0]
loop I from 1 to N - 1
   if NUMS[I] > max then
      max = NUMS[I]
   end if
end loop
output max

Flowcharts

A flowchart draws an algorithm using standard shapes joined by arrows:

  • Oval (terminator): Start and End.
  • Parallelogram: Input or Output.
  • Rectangle: a Process, such as an assignment or calculation.
  • Diamond: a Decision, with two exits labelled Yes/No (or True/False).
  • Arrows: the direction of flow.

A loop appears as an arrow that returns from a later step to a decision diamond earlier in the chart.

Tracing

To check an algorithm, trace it by hand using a trace table, with a column for each variable and a row for each step.

Tracing the max algorithm with NUMS = [3, 8, 5]:

I | NUMS[I] | max
- | 3       | 3     (initial)
1 | 8       | 8
2 | 5       | 8

The output is 8. Tracing finds logic errors and confirms correctness.

Choosing between them

Flowcharts are visual and good for beginners or for showing overall flow. Pseudocode is quicker to write and closer to real code, so it scales better to bigger algorithms.

Check your understanding

Which flowchart shape represents a decision?

  • Rectangle
  • Diamond
  • Oval
  • Parallelogram

Answer: Diamond. It has two or more labelled exits.

What must an algorithm do after a finite number of steps?

  • Repeat forever
  • Terminate
  • Crash
  • Print twice

Answer: Terminate. An algorithm must finish.

What is the purpose of a trace table?

  • To track variable values while stepping through an algorithm
  • To store data permanently
  • To draw flowcharts
  • To compile code

Answer: To track variable values while stepping through an algorithm. It reveals logic errors.

Which pseudocode repeats while a condition is true?

  • loop while … end loop
  • if … then
  • input
  • output

Answer: loop while … end loop. The condition is checked before each repetition.