IAN LANG ELECTRONICS

And as the title suggests, in this chapter we are going to look at  algorithms (lots of fun) and logic (even more fun!) as it applies to programming. Alright, I'm telling lies about the fun. It's not fun, it's hard and difficult, but I promise I'll try to keep it as flippant as possible so that you don't fall asleep on your keyboard.

 

Let's start with the word  "algorithm". It actually is a westernisation of the name Al - Khwarizmi who is the man who introduced Algebra to the Arabic and eventually Western Worlds.  If you are now wanting to join the queue to give him a good slap  for it  I'm sorry but he has been dead for over 1000 years so it's a bit late.

 

I digress. an algorithm is merely a posh way to say a set of instructions. Here's one for making a cuppa:

Coding for Primary Schools

Back to Title

Algorithms and Logic

breaktime

define  kettle  as water heating device

 

pick up   kettle  

 

define  tap   as source of  water

 

take   kettle to  tap

 

put   kettle under  tap

turn   tap on.

 

repeat     until   kettle full

 

{let   water pour into kettle}

 

when   kettle full   turn off tap.

 

define electric socket as source of energy to heat water

 

plug  kettle into electric socket

 

switch   kettle on

 

define cup as receptacle

define teabag as source of tea

define milk as dairy additive

define sugar as sweetener

define taste as  two sugars

 

let all in cup = teabag + milk + hot water + sugar

 

pour  milk   into  cup

 

repeat  taste times

{spoon  sugar  into cup}

 

put  teabag  into cup

 

wait

 

until kettle boiled

 

if kettle not automatic (switch kettle off)

pick up kettle

 

pour hot water into cup

 

define spoon as stirrer

 

stir  all in cup with spoon

remove teabag with spoon

if spoon still in cup (remove spoon)

 

pick up cup

 

define chair as comfortable

 

sit on chair

 

drink all incup

 

if you smoke

light nice cigarette to go with cuppa

else

define digestive biscuit as nice accompaniment to all in cup

dunk digestive biscuit

eat digestive biscuit

 

 

 

 

 

This is of course extremely silly but it does illustrate nicely an important point about computer programming. Computers are extremely good at maths. I don't care if you have a degree in doing hard maths really quickly whilst playing a medley of tunes from The Sound of Music on a trombone and a doctorate in doing calculus whilst climbing Mount Killimanjaro stark naked except for a loincloth. You can't do maths as quickly as the computer can. At the same time computers are as dumb as toast. The little box of blinky electronics you are currently staring at is completely hopeless at taking the initiative and needs to be constantly told what to do. Furthermore it needs it in bite-size chunks and in as much detail as possible or it will do something unexpected. Microcontrollers are a kind of cut-down computer used for giving autonomous machines a brain. Still, you need to program that brain with some code and you have to anticipate every possible situation the machine might find itself in. Which is why if you spend any time with electronic engineers you will shortly hear the words "why are you doing that?"  followed by a long stream of astoundingly creative swearing.

 

Let's look at a portion of the above algorithm and assume that a human with average intelligence (of a King Edward potato) is doing the actions. So:

 

define  kettle  as water heating device

 

pick up   kettle  

 

define  tap   as source of  water

 

take   kettle to  tap

 

put   kettle under  tap

 

turn   tap on.

 

repeat     until   kettle full

 

{let   water pour into kettle}

 

when   kettle full   turn off tap.

 

define electric socket as source of energy to heat water

 

plug  kettle into electric socket

 

switch   kettle on

 

 

Alright then, our thickie could probably handle that assuming he or she knows what a kettle, tap and socket is and he/she knows where the on switch is. A computer could not. It has to be told what and where the kettle, tap and socket are. Let's remove one part of the algorithm and let our thickie go through it:

 

 

define  kettle  as water heating device

 

pick up   kettle  

 

define  tap   as source of  water

 

take   kettle to  tap

 

turn   tap on.

 

repeat     until   kettle full

 

{let   water pour into kettle}

 

when   kettle full   turn off tap.

 

define electric socket as source of energy to heat water

 

plug  kettle into electric socket

 

switch   kettle on

 

If you look we removed the line put   kettle under  tap  and so our thickie, following the process exactly, would fail to do so. The next instruction is  turn  tap on which our thickie would do, but oh dear, look what happens next:

 

 

repeat     until   kettle full

 

{let   water pour into kettle}

 

when   kettle full   turn off tap.

 

Because our thickie has not put the kettle under the tap the kettle will never be full. Nothing has been put in place to say if the kettle is not already under the tap put it there and so this part of the algorithm will wait until the kettle gets full before doing anything else and as the kettle never gets full then it will wait forever. The upshot is the thickie will either starve to death waiting for the kettle to fill or will be rescued when the water board come round to find out why half the reservoir has been drained into the thickie's house.

 

The above is exactly what a computer would do. It does not have any sense of its own -  it relies entirely on the programmer for instructions. If you don't make the instructions precise enough unexpected things will happen. You rarely, even if you are experienced, get it right in one go. My advice is to learn creative swearing. (Incidentally, when a computer enters a loop it can't get out of because the condition for breaking out of it can never be fulfilled it's called an infinite loop and means your program will do that loop for all eternity, or at least until you switch the power off).

 

It's common to use the words algorithm and program as synonyms for computer instructions but a better definition is that an algorithm is an indepently functioning section of code which contributes along with other algorithms working in unison to the running of a program. Code is what the algorithm is made of and consists of words and numbers that make a machine readable sentence. Don't worry if you don't understand any of that, it will become clear as we go along.

 

On to logic. If you have had a classical education, you will know that logic in Ancient Greek terms is a rational series of arguments leading to a clear conclusion. In computing it can mean that, but there's also a thing called Boolean logic that has answers either true or false and leads to an answer that is either true or false.

 

What is important at the moment is that in programming the order of the logic (ie instructions) can be of critical importance. For instance, if you want to count clicks on a button and put up a display as the button is clicked,  you must count the click before putting up the display. If not, the display will show one less click than the actual count. If you make a washing-machine drum spin before it fills with water, the water will go everywhere, so fill the drum first and then make it spin.

 

 

The final topic of this lesson is the difference between event-driven and line-driven programming.

The term event-driven refers to a user input such as a mouse click, pressing a key, dragging or anything else.

Line driven is what algorithm does in response to the above and is so called because the computer reads the code line by line and does as it's told.

 

 

 

The summary of this lesson is that you need to think about the precision and the position   of any instructions you give.  There's a lot more underpinning theory in computer sciences and we'll learn some as we go but armed with the above you're ready to start coding and now you need to learn the language. Ready?

 

 

 

Next Lesson >

Joke:  A computer programmer's wife rings him up and says "go to the shop on your way home and buy a loaf of bread. If they've got any eggs, bring a dozen". The programmer arrives home with twelve loaves. His wife looks at him quizzically and the programmer says "the shop had eggs".