Coding is just like baking a cake
Coding is just like baking a cake. Think about it: in order to make a cake, you need to follow a set of instructions — a recipe, in other words — to make something, right? Coding is much the same: we give the computer a set of instructions (an algorithm, as it’s properly called) to make something.
In no particular order, here are the six fundamental concepts of coding:
- Sequencing
- Looping
- Branching
- Arithmetic
- Data storage
- Data in / data out
But what do these words mean? Let’s look at them them in terms of the cake baking analogy we described earlier:
[#1] Sequencing
Sequencing refers to the order of steps that we are required to follow in order to complete a task. Let’s say that the recipe for baking a cake is made up of 8 steps: step 1 to step 8. What happens if we were to follow the instructions out of order? Say, we complete step 3, step 7, step 5, and then step 1. Would we expect to achieve the outcome of a cake as promised by the recipe?
The outcome you can expect if you follow the recipe.
The outcome you can expect if you don’t follow the recipe.
We need to complete the steps in the correct order, so as to achieve the desired result. Coding is exactly the same. The steps in the algorithm are arranged in a specific order that we need to respect!
[#2] Looping
Looping refers to iteration or repetition: in other words, it allows us to instruct the computer to do the same task multiple times in an efficient manner.
Bearing in mind that our cake recipe has 8 steps, let me give you an example of how looping would apply in our cake baking anecdote. Let’s say we want to bake 3 cakes:
Option A) I could tell you to do:
Step 1 of cake recipe.
Step 2 of cake recipe.
Step 3 of cake recipe.
Step 4 of cake recipe.
Step 5 of cake recipe.
Step 6 of cake recipe.
Step 7 of cake recipe.
Step 8 of cake recipe.
Step 1 of cake recipe.
Step 2 of cake recipe.
Step 3 of cake recipe.
Step 4 of cake recipe.
Step 5 of cake recipe.
Step 6 of cake recipe.
Step 7 of cake recipe.
Step 8 of cake recipe.
Step 1 of cake recipe.
Step 2 of cake recipe.
Step 3 of cake recipe.
Step 4 of cake recipe.
Step 5 of cake recipe.
Step 6 of cake recipe.
Step 7 of cake recipe.
Step 8 of cake recipe.
A visual representation of Option A: tedious, isn’t it?
Option B) I could tell you to do:
Do 3 times:
Step 1 of cake recipe.
Step 2 of cake recipe.
Step 3 of cake recipe.
Step 4 of cake recipe.
Step 5 of cake recipe.
Step 6 of cake recipe.
Step 7 of cake recipe.
Step 8 of cake recipe.
A visual representation of Option B.
In our above example, the person following the instructions would do Step 1 to Step 8, then start back at Step 1 and run through the instructions to Step 8 again. They would then repeat the set of instructions for a third time in order to achieve the desired outcome: three baked cakes.
Looping is an efficient way of giving instructions. Notice that Option A is made up of 24 lines, whereas Option B is made up of 9 lines! Both options give the same result (i.e. 3 baked cakes), but Option B is more efficient at giving instructions, as it involves less lines. Another way coding is just like baking a cake!
Quick Article: Visual-based vs. Text-based programming languages
[#3] Branching
Branching — also known as decision making — provides us with options in the instructions. There are certain conditions that dictate which options we take. Let’s take a look at an example of branching using our cake baking analogy:
Step 1 of cake recipe.
Step 2 of cake recipe.
Step 3 of cake recipe.
Step 4 of cake recipe.
Step 5 of cake recipe.
Step 6 of cake recipe.
Step 7 of cake recipe:
If you have a fan-forced oven, bake cake at 180°C for 40 minutes.
If not, bake cake at 200°C for 50 minutes.
Step 8 of cake recipe.
A visual representation of the procedure mentioned earlier.
Step 7 of the recipe makes use of a “branching statement”: in other words, Step 7 provides us with two alternative instructions. That being said, we can’t simply choose which option we want willy-nilly: there are conditions that have to be met before the appropriate decision can be made.
In this case, the option that we take depends on the type of oven that we use. If the oven is fan-forced, the first instruction is used, i.e. the cake is baked at 180°C for 40 minutes. Otherwise, the second option is taken, i.e. the cake is baked at 200°C for 50 minutes.
[#4] Arithmetic
Arithmetic is often used in both algorithms and in recipes. Let’s look at our cake recipe again: let’s go back to the scenario where we wanted to bake three cakes. In a previous example, we looked at this set of instructions to do the aforementioned task:
Do 3 times:
Step 1 of cake recipe.
Place 3 whole eggs in a mixing bowl.
Place 125g of butter in another mixing bowl.Step 2 of cake recipe.
Step 3 of cake recipe.
Step 4 of cake recipe.
Step 5 of cake recipe.
Step 6 of cake recipe.
Step 7 of cake recipe.
Step 8 of cake recipe.To make the icing, place 75g of butter in a mixing bowl.
We have a dozen eggs in the carton. If we use 3 eggs in one cake, how many cartons of eggs will we need?
We have a 500g block of butter. Can we cut the butter to get 125g without weighing it? How many blocks of butter will we need for 3 cakes?
We want to slice the cake into 8 equal pieces. What angle do we make at the tip of each cake slice?
[#5] Data storage
Data storage is a key concept in coding. Computers are powerful because they have great memories and can process data — they are much better than humans at this particular task!
Now, back to our cake baking analogy: when baking a cake, we need to store our different ingredients (data) and mixtures as we go. All ingredients come in some form of packaging (data storage):
- Eggs come in a carton
- Flour comes in a paper bag
- Butter comes in wax paper
As we proceed with the recipe (our algorithm), we start to place these ingredients (our data) in various other storage containers. Sometimes, we may even mix various ingredients together (data processing).
In a mixing bowl, we can add the eggs, flour, butter, and sugar (our data). We mix the ingredients (data processing), and then pour everything into a baking tin (data storage).
The ingredients move from one storage container to another — from its original packaging, to the mixing bowl, to the baking tin. Once baked, the cake will be sliced and then placed onto a plate.
Each storage container has a specific purpose:
- The mixing bowl stores the ingredients as they are mixed together
- The baking tin holds the mixture as it is baked in the oven
- The plate holds the slice of cake before it is served to the user
In programming, we use data storage to store things like numbers, letters, truth values (Booleans), graphics, audio, and so forth.
[#6] Data in / data out
Data in / data out is linked to data storage. Computers are very useful to us because they can store and process data. Let’s look at our cake recipe:
Data in:
We start with data inputs:
- Ingredients
- Utensils and equipment
- Humans (for labour)
Data processing:
First, the input data is processed. For our cake analogy, the “process” involves measuring the ingredients, placing them into the mixing bowl, and mixing them. The mixture is then poured into a baking tin, and baked in the oven. The baked cake is then removed from the oven, left to cool, covered with icing, sliced, and — finally — served on a plate.
Data out:
The output data is a plate with a slice of cake. This output data is then served to the user.
There you have it, coding is just like baking a cake. If you’re interested in learning more helpful analogies, check out our Digital Technologies Lesson Planning e-book and our Teacher PD workshops!
If you’re interested to find out more