Find a course

Module 3: AlgorithmsLesson 6 of 1213 min

Designing Algorithms with Pseudocode

Build algorithms from sequence, selection and iteration, write them in pseudocode, and trace them by hand to find mistakes before writing code.

Step 1 of 15Learn

Three building blocks for every algorithm

In 1966, two computer scientists, Corrado Böhm and Giuseppe Jacopini, proved something remarkable: any algorithm can be written using just three control structures.

  1. Sequence: do steps one after another, in order. "Crack the eggs, then whisk them, then pour them into the pan."
  2. Selection: choose between paths based on a condition. "IF the pan is hot, add the eggs; OTHERWISE wait."
  3. Iteration: repeat steps, either a set number of times or while a condition holds. "WHILE the eggs are runny, keep stirring."

That's it. Every app, from a calculator to a search engine, is built from these three structures, nested and combined in endless ways.

The building blocks show up in plain language long before code. A school's rule for snow days might be: check the forecast (sequence); if more than 6 inches of snow is expected, close (selection); repeat the check every morning in winter (iteration).

Learning to see problems in these terms is the first step toward programming. Before writing a single line of code, experienced developers sketch the algorithm: which steps happen in order, where decisions are made and what repeats.

Step 2 of 15Practice · Sort into groups

Classify each step by its control structure.

Tap an item, then tap its group (or drag it there).

Step 3 of 15Diagram

The three building blocks

The three building blocksHierarchy diagram: Any algorithm, Sequence, Selection, Iteration, do A, then B, IF ... ELSE, WHILE condition, FOR EACH item.Any algorithmSequenceSelectionIterationdo A, then BIF ... ELSEWHILE conditionFOR EACH item

Tap a shape with to see more.

Böhm and Jacopini (1966): these three are enough for any algorithm.
Any algorithm
Sequence

Steps in order, one after another.

Selection

Choose a path based on a condition.

Iteration

Repeat steps.

do A, then B
IF ... ELSE
WHILE condition
FOR EACH item

Step 4 of 15Learn

Pseudocode: algorithms without syntax

Pseudocode is a way of writing algorithms in structured plain language. It isn't a real programming language, so there are no strict rules, but it borrows the shape of code: indentation, IF, WHILE, FOR EACH and named values.

Here's an algorithm that computes a student's average and letter grade:

SET total TO 0
FOR EACH score IN scores
    SET total TO total + score
SET average TO total / number of scores
IF average >= 90 THEN
    OUTPUT "A"
ELSE IF average >= 80 THEN
    OUTPUT "B"
ELSE
    OUTPUT "Below B"

Pseudocode is useful because it lets you focus on logic without worrying about semicolons or brackets. You can show it to someone who doesn't program, spot a flaw and fix it in seconds. Then translating it into Python or JavaScript becomes almost mechanical.

Notice the order of the IF checks. If you tested "average >= 80" first, a student with 95 would get a B, because 95 is also at least 80. The order of conditions matters: put the most specific or highest threshold first. This kind of logic error is common and doesn't produce any error message; the program just gives wrong answers.

Step 5 of 15Note

Step 6 of 15Practice · Spot the mistake

This pseudocode should output "Freezing" below 0, "Cold" from 0 to 14, and "Warm" from 15 up. Tap the line that causes wrong results.

Step 7 of 15Learn

Tracing: run the algorithm in your head

How do you know an algorithm is correct before you code it? You trace it: walk through it step by step with sample input, writing down the value of every variable after each step. A trace table keeps this organized.

Let's trace an algorithm that adds up the even numbers in [3, 4, 7, 10]:

SET sum TO 0
FOR EACH n IN list
    IF n is even THEN SET sum TO sum + n
OUTPUT sum
StepnEven?sum
start––0
13no0
24yes4
37no4
410yes14

Output: 14. Correct.

Tracing catches the most common algorithm bugs:

  • Off-by-one errors: a loop runs one time too many or too few.
  • Wrong initial values: starting a running maximum at 0 fails when all numbers are negative.
  • Wrong order of steps: updating a value before using its old value.

Always trace with edge cases too: an empty list, a list with one item, negative numbers, duplicates. Many algorithms work on the "normal" example and break on the edges.

Step 8 of 15Compare

A trace table for: SET total TO 0; FOR EACH n IN [3, 5, 2]: SET total TO total + n

Aspectntotal
Start(none yet)0
Pass 133
Pass 258
Pass 3210
Output10

Step 9 of 15Practice · Work it out

Trace this algorithm: SET x TO 1. REPEAT 4 TIMES: SET x TO x * 3. OUTPUT x. What is the output?

Step 10 of 15Note

Step 11 of 15Try the calculator

Average with an edge-case guard

Set the count to 0. A naive average would divide by zero; the guarded version returns 0 instead.

Guarded average
7

Step 12 of 15Note

Step 13 of 15Practice · Code challenge

Code challenge

Translate this pseudocode into JavaScript. countAbove(scores, limit) should return how many scores are strictly greater than limit.

SET count TO 0; FOR EACH s IN scores: IF s > limit THEN add 1 to count; OUTPUT count.

Runs in a sandbox in your browser.
  • countAbove([50, 80, 90, 70], 75)
  • countAbove([10, 20], 5)
  • countAbove([], 10)
  • countAbove([75, 75], 75)

Step 14 of 15Practice · True or false

An algorithm that gives correct results for one typical example has been shown to be correct.

Step 15 of 15Practice · Choose one

An algorithm to find the highest temperature starts with "SET highest TO 0" and then compares each reading. For which data set will it give a wrong answer?