You Already Know How to Code

Maybe you’ve never tried programming. Maybe you’re interested, but afraid you wouldn’t be able to do it. What if I were to tell you that you already know how to code? Don’t believe me? Then read on…

You Already Know How to Write Algorithms

“That’s not true”, I hear you say. “I don’t even know what algorithms are.”

Except you do, you just don’t realise it. An algorithm is a set of instructions to carry out in a certain order. Some of the instructions are carried out conditionally, i.e. different instructions must be performed depending on the outcome of a ‘check’. For example, if the traffic lights are red: stop. If the traffic lights are green: keep going. And there’s just one example of where you already know algorithms — you use them every time you drive!

Cooking recipes are another example of algorithms. They are a set of instructions that you follow, and they do have conditional instructions too. For example, they might say something like: “If the mixture is still runny, keep stirring for another two minutes.” That’s an algorithm you’re following.

You create algorithms for yourself too. Have you ever looked up the weather forecast in order to determine whether or not to take an umbrella? You’ve just created an algorithm for yourself to follow! If the weather forecast is rain: take umbrella. If the weather forecast is sunny: don’t take umbrella.

Algorithms are the starting point for writing code. If you want to write code, you generally write an algorithm first (even if it’s just in your head). They can get complex, with many branches, but the basic principles are there in the every-day algorithms we encounter and make for ourselves.

You Already Know How to Construct a Conditional

“No I don’t”, you say. “If I knew how to construct a conditional, I’d be a coder!”

And then I laugh and say, well, you just constructed one right there. That’s right, any sentence which has the general form ‘if…then…’ is a conditional. We saw some in the last section too:

‘If the weather forecast is rain: take an umbrella.’

‘If the traffic light is red: stop.’

A conditional is a statement that is evaluated to see if it is true or false. Based on the outcome, the next clause is performed or not. So for our examples above, ‘if the weather forecast is rain’ and ‘if the traffic light is red’ are the conditionals to be evaluated. If they are true, the next clause is the action taken. So let’s say the weather forecast is rain, and the traffic light is red. Both the conditionals will evaluate to true. This means the action in the next clause is performed: We take an umbrella; we stop.

What if the conditionals evaluate to false? In that case, the action in the next clause is not performed. Perhaps we may have specified an alternative action instead. For example:

‘If the weather forecast is rain: take an umbrella. Otherwise, leave the umbrella at home.’

‘If the traffic light is red: stop. Otherwise, continue driving.’

So for these conditional statements, if the weather forecast is not rain, the action performed is ‘leave the umbrella at home’. If the traffic light is not red, the action performed is ‘continue driving’.

Pretty simple so far. But what if there are more than two options? Is that still a conditional? The answer is yes, you can have conditionals with multiple possible actions. Some examples are:

‘If the weather forecast is rain: take an umbrella. If the forecast is overcast: take a coat. If the forecast is sunny: take sunglasses.’

‘If the traffic light is red: stop. If the traffic light is amber: slow down. If the traffic light is green: continue driving.’

For these conditionals, we evaluate the first statement, and if we find it to be false, we move to the next statement and evaluate that. When we find a true statement, we then perform the related action.

We do this all the time in our every day lives. Conditionals form the backbone of decision-making in life, and they form the backbone of decision-making in code too.

Let’s see an example in real code. We’ll use python for this:

traffic_light = 'red'
action = None

if traffic_light == 'red':
    action = 'stop'
if traffic_light == 'amber':
    action = 'slow down'
if traffic_light == 'green':
   action = 'continue driving'
print(action)

So, given what you already know about conditionals, what do you think this code will print? Yep, you got it. The code will print ‘stop’ because the traffic light is red.

You Already Understand Boolean Logic

“That’s not true!” I hear you cry. And I smile, because you’ve just invoked boolean logic in constructing that sentence!

If you can speak a language like English, you understand boolean logic. You have to understand it to be able to construct sentences about things that ‘are the case’ and ‘are not the case’. For example, I might ask you, “Is it raining today?” You could respond, “Yes, it is raining,” or “No, it is not raining”. Whether it is raining or not is a boolean set of states, because it can either be raining nor not raining. By boolean, I mean that the statement ‘it is raining’ can either be true or false. It can’t be sort of raining; it either is, or it isn’t. You’ve looked at the sky and determined the correct state. You’ve determined if the question ‘is it raining today?’ should have the answer ‘true’ or false’. But if it isn’t raining, you’ve also done something else. You’ve taken the state ‘raining’ and preceded it with ‘not’ to generate the state ‘not raining’, the boolean opposite of ‘raining’. Did you realise you’d just performed a boolean logic operation on ‘raining’? Probably not, but you did. If you take any boolean state that evaluates to ‘true’ and put ‘not’ in front of it, you make a statement that evaluates to boolean ‘false’. And you did that, by saying, ‘it’s not raining’.

Boolean logic like this is one of the fundamentals of coding. The key rule here is that something that is not true is false. The NOT is an example of a boolean operator. Other examples include AND and OR. A boolean operator is a word or symbol that precedes or joins statements to form larger logical statements. Most work just like their English language equivalents. Boolean logical operators are used with conditionals (see previous section), where we evaluate one or more statements and proceed based on how many of them were true. We would use them in our every day lives like so:

‘If it’s raining AND the traffic light is red: brake early and stop.’

‘If the traffic light is red OR the traffic light is amber: stop.’

In the first example, both ‘it’s raining’ and the ‘traffic light is red’ must be true in order for us to perform the action of braking early and stopping. If one or more of them is not true, we don’t perform the action.

In the second example, if the traffic light is either red or amber, we stop. We’re starting to build up constructs that look something akin to code! But we’ve only written plain English so far. But that’s what I’m saying: if you can write or speak any language like English, you can write code.

Now let’s represent our logic in actual python code:

traffic_light = 'red'
weather = 'raining'
brake_early = False
stop = False
if weather == 'raining' and traffic_light == 'red':
    brake_early = True
if traffic_light == 'red' or traffic_light == 'amber':
   stop = True
print(f"It is {weather}. The traffic light is {traffic_light}.")
print(f"Will I need to brake early: {brake_early}. Do I need to stop: {stop}") 

When run, this code should output:

It is raining. The traffic light is red.
Will I need to brake early: True. Do I need to stop: True

So that’s all code is, really. Especially high-level languages. It is the written word, but in a shortened form that machines can read more easily. It is the middle ground between human-readable and machine-readable.

You Already Know About Arrays

“OK, now listen here. Firstly, all these assumptions about me have to stop. Secondly, I don’t know anything about arrays and never will.”

Do I need to tell you that you constructed one in that sentence? Arrays, in their simplest form, are just lists. A list is just a numbered set of items. In your shopping list, you may have something like:

  1. Eggs
  2. Sugar
  3. Milk

In a programming list, the numbers are used to index the items in the list. This just means that list item 1 would refer to the value ‘Eggs’. However, there is a slight difference with a programming list, which is that the first item has index 0 as opposed to 1. Our shopping list becomes:

0. Eggs
1. Sugar
2. Milk

It may seem a little strange, to begin at 0, but you soon get used to it as a programmer.

Here’s how this list would be represented in python:

shopping_list = ['Eggs', 'Sugar', 'Milk']

The list item (index) numbers are not explicitly present, but they are inferred from the list item position. As ‘Eggs’ is listed first, it gets assigned index 0, ‘Sugar’ gets index 1 and ‘Milk’ gets index 2.

If we want to retrieve an item from the list, we ‘reference’ it by its index value. So to retrieve the first item on the list, which is ‘Eggs’, we reference the 0 index:

first_item = shopping_list[0]
print(first_item)

If you run that code, you’ll get the output ‘Eggs’. This isn’t so bad, is it? (Hopefully, neither are the eggs.)

You Already Understand Loops

“Look, I’ve told you before, and I’ll tell you again: I don’t understand programming! Just to reiterate: I don’t understand programming! Why don’t you give me a break!”

Have you ever seen instructions that say something like ‘and repeat’ or ‘x2’? This is another way of expressing a loop, one of programming’s basic and important constructs. I mean, why would you write out the same instructions over and over again in an instruction manual? You wouldn’t, you’d just write ‘repeat step 5’ or ‘follow steps 3 to 5 until the mixture turns brown’ etc. The same principle applies to programming. Programmers don’t want to write the same code over and over again either. We need a shorthand way of saying, ‘repeat these same steps until something occurs that means you can stop’. This is a loop.

You go through loops in daily life, even if you don’t realise it. Let’s take the shopping list example in the last section. Let’s say you’re in the shop, and you’re checking your list to see if you have everything. By doing this, you’re running a loop. You look at the first item on the list, and you check if you have it in your trolley. If you do, you check it off. If you don’t, you go and find it in the shop. Then you move to the next item, and you check if you have it in your trolley. if you do, you check it off. If you don’t, you go and find it in the shop. You repeat this for each item. When you get to the end of the list, you stop. The loop ends, and you probably at that point, go to the checkout.

We could represent this in python code like so:

trolley_items = ['Eggs', 'Milk']
shopping_list = ['Eggs', 'Sugar', 'Milk']
items_still_to_get = []

for item in shopping_list:
    if item not in trolley_items:
        items_still_to_get.append(item)
print(items_still_to_get)

Can you work out what this code will print, given all that you already know about code? The code loops over all the items in the shopping list, and for each one, it checks if it is present in the trolley items list (which is a list of items in the trolley). If it is not present in the trolley list, the item gets added to the list of items still to get. The only item on the shopping list that is not in the trolley is Sugar, so the code prints a list containing a single item: ['Sugar'].

Conclusion

Do you believe me now, that you already know how to code? You’re already doing it, every day! If you’re interested in learning how to code, go ahead and give it a try. Don’t be put off by these notions of it being too hard or not for you. It could just spark a whole new career! However, if you need more convincing, check out my post You Already Know How to Code: Part 2. Thanks for reading, I’ll see you next time.