Coding is built on 3 basic logic patterns

Coding, or computer programming, is built on three basic logic patterns:

  1. Sequencing
  2. Branching
  3. Looping

Sequencing - the computer executes one command after another in the order in which they are listed in the program

Branching - the computer makes a choice by checking for conditions which determine the choice, i.e. conditional programming logic

Looping - the computer returns back to a previous statement in the program for repetition, so that a given sequence of statements can be performed as many times as needed.

Let's bake a cake

Let's look at what sequencing, branching and looping means in real life. Let's use the example of baking a cake using a recipe. 


The recipe lists a series of steps that tells you how to bake a cake. the steps are in a specific order. If you follow the steps out of order, the outcome will something else and not the intended cake. Even in a recipe the order of the steps is crucial in achieving the desired outcome. 


The recipe to bake a cake give explicit instructions on how to bake a cake. However, there may be cases where the recipe requires you to check for a condition and make a decision. Here are two examples.

  1. The recipe provides two different oven temperatures depending on the type of oven used. The recipe instructs that the oven temperature is 190 degrees Celsius for a conventional oven and 170 degrees Celsius for a fan-forced oven. You are required to check what type of oven you have and instructions for the oven temperature is based on this condition.
  2. The recipe provides optional instructions. The base recipe is for a plain sponge cake. If you want to bake a chocolate cake, there is an optional step to add 3/4 cup unsweetened cocoa powder. Or if you want to bake an orange and almond cake, there is an optional step to add orange juice, orange rind and almond meal.  


Let's say you own a bakery and you bake many cakes everyday. Let's say you want your baker to bake 10 cakes today. The cake recipe is made up of 10 steps. Rather than giving your baker 100 steps so that they can make 10 cakes from the cake recipe with 10, simply tell your baker to make 10 cakes and give them one copy of cake recipe. 


Another example that you can use in a classroom is navigation. Let's say you want to give directions to get from home to school.

Sequencing - The order of the directions is crucial to arriving at the destination, the school (sequencing).

Branching - You may choose to travel from home to school in one of various modes of transport: walk, drive or take public transport. The directions given will depend on which mode of transport is chosen, for example arriving by foot may allow you to make use of a pedestrian shortcut or driving may take a different route to taking the bus. Just like Google Maps which offers directions based on mode of transport.  

Looping - Assuming the conditions are the same, e.g. mode of transport or traffic conditions, the instructions are the same if you wanted to repeat it. For example if you choose to travel by foot every day this week, the directions to travel from home to school is the same.