Algorithms & Flowcharts — Thinking Before Coding

How to map out your ideas, design flowcharts, and squash bugs before writing code.

Have you ever tried to bake a chocolate cake by throwing all the ingredients into a cold oven at the exact same time? Of course not! To get a delicious cake, you have to follow a specific plan, step by step. In computer science, we call this plan an algorithm.

Words to own

Algorithm
A precise, step-by-step set of instructions for solving a problem or completing a task.
Pseudocode
A simple way of writing down computer steps using plain English instead of a real coding language.
Flowchart
A visual diagram that uses shapes and arrows to show the path of an algorithm.
Decomposition
The process of breaking a big, complicated problem down into smaller, easier-to-solve steps.
Debugging
Finding and fixing mistakes or logical errors in your code or algorithm.

The Recipe of Computing: What is an Algorithm?

Before you write a single line of Python code, you need a plan. An algorithm is just a precise recipe: a finite list of unambiguous steps that always produces the right result. If your instructions are fuzzy or in the wrong order, the computer will get confused and do the wrong thing.

Computers are extremely obedient, but they are also very literal. They cannot guess what you meant to say. If you write an algorithm to make a peanut butter sandwich and forget to tell the computer to open the jar first, it will try to smash the knife right through the lid! Order and clarity are everything.

The 5 Steps of Solving a Problem
  1. Step 1

    1. Decompose

    Break the big goal down into smaller, bite-sized tasks.

  2. Step 2

    2. Plan

    Write pseudocode or draw a flowchart to map out your steps.

  3. Step 3

    3. Desk Check

    Test your logic on paper with simple numbers to make sure it works.

  4. Step 4

    4. Write Code

    Translate your planned steps into real, working Python code.

  5. Step 5

    5. Debug

    Run your program, look for errors, and fix any logic mistakes.

Pseudocode: Writing in 'Human' Code

Pseudocode is like a bridge between human speech and computer code. 'Pseudo' means fake, so pseudocode is literally 'fake code'. You do not have to worry about missing colons or brackets here. You just write the logical steps down in plain English.

For example, if you want to check if a student passed a test, you might write: GET the score, IF the score is greater than 50 THEN print 'Pass', ELSE print 'Try Again'. See how easy that is to read?

Translating simple pseudocode into real Python code
score = 85
if score > 50:
    print("You passed!")
else:
    print("Keep trying!")
Output
You passed!

Flowcharts: Mapping the Path

Some people prefer to see their plans visually. A flowchart draws your algorithm using standard shapes connected by arrows. These shapes tell you exactly what kind of step you are looking at.

An oval represents the Start or Stop of your program. A rectangle is used for an action, like adding two numbers together. A diamond shape is for a decision, which asks a Yes/No question and splits your flowchart into two different pathways.

Decomposition: Eating the Elephant

How do you eat a giant elephant? One bite at a time! Decomposition is the fancy computer science word for breaking a huge, scary problem down into tiny, friendly tasks.

Imagine you are building a video game. Instead of trying to build the whole game at once, you decompose it. First, you write an algorithm just to move the character. Then, you write an algorithm to count the score. Finally, you write an algorithm for the obstacles. By solving these small problems one by one, the big problem solves itself.

Breaking down a shopping total: Item price first, then adding tax
price_before_tax = 100
tax_amount = price_before_tax * 0.15
final_total = price_before_tax + tax_amount
print("Your total is:", final_total)
Output
Your total is: 115.0

Debugging: Hunting the Bugs

Even the best programmers make mistakes. A mistake in an algorithm is called a 'bug'. Debugging is the process of testing your steps systematically to find where your logic went wrong.

One of the best ways to debug is to do a 'desk check'. You grab a piece of paper and write down what your variables should be at every step of your algorithm. This helps you spot exactly where the computer does something different from what you expected.

Your turn — run real Python

Finding the Smallest Number

The current algorithm looks through a list of numbers to find the biggest one. Change the algorithm so that it searches for and prints the smallest number instead.

1
2
3
4
5
6
7

Bug hunt

The program is printing the running total on every loop iteration instead of just giving the final answer at the very end. What is causing this?

# Goal: Count how many odd numbers are in the list
numbers = [3, 4, 7, 10, 11]
odd_count = 0
for n in numbers:
    if n % 2 != 0:
        odd_count = odd_count + 1
    # Oh no! Why is this printing every single time the loop runs?
    print("Total odd numbers:", odd_count)

Myth-busting corner

  • MythYou should start coding in Python immediately when you get a task.

    TruthProfessional programmers plan first. Writing code without an algorithm is like building a house without blueprints; it will likely fall apart.

  • MythComputers are smart and can fix minor mistakes in your steps.

    TruthComputers are actually quite simple-minded! They will follow your instructions exactly, even if your instructions make no sense.

Exam answer that scores full marks

Describe an algorithm for finding the largest number in a list, and explain why the order of steps matters.

To find the largest number in a list, start by assuming the first number is the largest and store it in a variable. Next, look at each remaining number in the list one by one. If a number is bigger than the stored value, replace the stored value with this new number. Once every number in the list has been checked, the stored value is the final answer. The order of these steps is critical. If you announce the answer before checking all numbers, or if you do not set a starting value before doing comparisons, the algorithm will produce an incorrect result.

Why it scores

  • It clearly defines the starting condition (using the first number as the default largest).
  • It accurately describes the loop process (comparing each remaining number one by one).
  • It explains how the update condition works (replacing the stored value if a bigger one is found).
  • It explains why sequencing matters (errors occur if you stop early or compare before setting a baseline).