Welcome to Designing and Writing Programs!

Have you ever wondered how your favourite video games know when you score a point? Or how a robot knows which way to turn? It all comes down to programming!

In this chapter, we are going to explore how programmers plan, build, and fix computer programs that accomplish specific goals. Don't worry if this seems tricky at first—coding is just like learning a fun puzzle, and we will take it one step at a time!


1. Algorithms vs. Programs: What is the Difference?

Before we touch a keyboard, we need to understand two very important words: algorithm and program.

What is an Algorithm?

An algorithm is a clear, step-by-step set of precise instructions used to solve a problem or complete a task. Algorithms are written for humans to read and plan with. You can write an algorithm using plain words, a flowchart, or a storyboard.

Everyday Analogy: Think of a recipe for baking a cake or instructions for building a Lego set. If you miss a step, the cake won't bake properly!

What is a Program?

A program is an algorithm that has been translated into code so that a computer or digital device can run it. At Key Stage 2, you might write programs using visual block-based languages like Scratch or text-based languages like Logo or Python.

Did You Know? Computers do not have common sense! They cannot guess what you mean. If your instructions are not completely precise and unambiguous, the computer will do exactly what you told it to do—even if it is wrong!

Key Takeaway: An algorithm is the step-by-step plan; a program is that plan turned into computer code.


2. Computational Thinking Superpowers

To design great programs, computer scientists use three special thinking tools:

1. Decomposition (Breaking It Down)

Decomposition means breaking a big, complicated problem down into smaller, easier-to-manage parts.

Example: Imagine designing a computer game. Instead of doing everything at once, you decompose it into:

• Part 1: How the player moves.
• Part 2: How points are scored.
• Part 3: What sound plays when the game ends.

2. Abstraction (Focusing on What Matters)

Abstraction means removing unnecessary details so you can focus only on the most important parts of the problem.

Example: If you are programming a car to drive around a track, you care about its speed and steering. You do not need to code the colour of the car's seatbelts or the brand of its radio!

3. Logical Reasoning (Thinking It Through)

Logical reasoning is using step-by-step rules to predict what a program will do, explain how an algorithm works, and figure out why something might have gone wrong.

Key Takeaway: Use Decomposition to break problems into chunks, Abstraction to ignore useless details, and Logical Reasoning to predict and explain how code behaves.


3. The Four Core Building Blocks of Programming

Almost every computer program in the world is built using four main concepts: Sequence, Selection, Repetition, and Variables.

A. Sequence (Order Matters!)

A sequence is the exact order in which instructions are executed, one after another from top to bottom.

If you change the order of commands, the whole program changes!

Example: Think of getting dressed in the morning:
1. Put on socks.
2. Put on shoes.
If you reverse the sequence and put your shoes on before your socks, you will run into a big problem!

B. Selection (Making Decisions)

Selection lets a program make choices based on whether a condition is true or false. We use if... then or if... then... else blocks for this.

Example:
If the player touches the coin, then increase the score by \(1\).
If the timer reaches \(0\), then display "Game Over", else keep playing.

C. Repetition (Loops and Iteration)

Repetition (also called iteration or loops) means running the same set of instructions over and over again without having to write them out multiple times.

There are two main types of loops:

Count-Controlled Loops: The instructions repeat a set number of times (e.g., "repeat \(4\) times: move forward \(10\) steps, turn right \(90\) degrees" to draw a square).
Condition-Controlled Loops: The instructions keep repeating until something changes, or repeat forever (e.g., "repeat until touching edge" or a "forever" loop).

D. Variables (Changing Storage Boxes)

A variable is a named space in the computer's memory that stores information (a value) that can change while the program is running.

Memory Trick: Imagine a cardboard box with a label written on the front. You can open the box and swap the number inside whenever you like!

Common examples of variables in KS2 projects include:

Score: Starts at \(0\) and increases by \(1\) each time you collect an item (\(score = score + 1\)).
Timer: Starts at \(60\) and counts down to \(0\).
Lives: Starts at \(3\) and decreases by \(1\) when an obstacle is hit.

Key Takeaway: Sequence is the order of steps; Selection makes decisions; Repetition loops actions; Variables store values that change.


4. Inputs, Outputs, and Physical Systems

Programs don't just stay inside a computer screen—they can interact with the real world!

Inputs and Outputs (I/O)

Inputs are signals or information sent into the program. Examples include pressing a keyboard key, clicking a mouse, making a sound into a microphone, or pressing a button on a physical device.
Outputs are signals or actions sent out by the program. Examples include moving a character on screen, playing a sound through speakers, turning on an LED light, or spinning a motor.

Controlling and Simulating Physical Systems

You can use code to control real physical devices (such as a micro:bit or a Crumble controller) or to simulate (model) real-world systems on screen.

Real-World Examples:

Traffic Lights: Using sequence and timers to switch lights from Red \(\rightarrow\) Red & Amber \(\rightarrow\) Green \(\rightarrow\) Amber \(\rightarrow\) Red.
Automated Crossing Gate: Using an input sensor (detecting a car) to trigger an output motor (lifting the gate).
Robotic Rover: Using tilt sensors or light sensors to guide a rover around obstacles.

Key Takeaway: Programs receive data via inputs, process that data with code, and produce actions through outputs.


5. The Software Development Process & Debugging

Creating a successful program is a journey that follows three important stages:

Stage 1: Designing

Never start by randomly snapping code blocks together! First, plan your algorithm using a diagram, flowchart, or written steps. Break the goal down using decomposition.

Stage 2: Writing (Coding)

Turn your planned algorithm into actual code in your programming environment (like Scratch). Build your code in small steps rather than trying to build the whole program all at once.

Stage 3: Debugging (Hunting for Bugs!)

A mistake or error in an algorithm or program is called a bug. Debugging is the process of finding, isolating, and fixing those errors.

How to Be an Expert Debugger:

Do NOT Guess: Randomly moving blocks around usually creates more bugs!
Use Logical Tracing: Read through your code block by block from the top, just like the computer does.
Check Your Variables: Make sure your variables are set to the correct starting values when the green flag is clicked.
Test Small Sections: Test each part of your program individually before connecting them together.

Key Takeaway: Plan your design first, write code in small steps, and debug calmly using logical reasoning instead of guessing.


6. Common Mistakes to Avoid

Here are some of the most common pitfalls students run into—and how you can avoid them:

The "Sleeping If" Block: Placing an if... then block by itself means the computer only checks the condition once for a split-second at the start. If you want the computer to constantly check (e.g., "am I touching the maze wall?"), place the if block inside a forever loop!

Confusing a Variable with a Static Label: A variable is not just text on the screen; it is a container in memory that can be changed by code (e.g., changing by \(+1\) or \(-1\)).

Ignoring Sequence: Computers run code from top to bottom. If your sprite hides before it plays a sound, make sure that is what you intended!

Thinking Computers Have Intuition: A computer cannot guess your intentions. If your code tells a sprite to move \(1000\) steps off the edge of the screen, it will do exactly that!


7. Quick Review Summary

Algorithm: A step-by-step set of precise instructions written to solve a problem.
Program: An algorithm written in code that a computer can run.
Decomposition: Breaking big tasks into smaller, manageable chunks.
Sequence: Running commands in a strict order from top to bottom.
Selection: Making decisions using if... then... else.
Repetition: Repeating instructions using count-controlled or condition-controlled loops.
Variables: Named memory boxes that hold values that can change.
Debugging: Finding and fixing errors using logical reasoning.