Friday, August 2, 2013

Introduction to programming

When you don't know how it works, it seems very complicated. But it's not, of course. It's a matter of understanding the basic principles which are very simple. It's a lot like cooking: you need to know the concepts of cutting and cooking, then all the rest is just details.

Programming relies on few stuffs:

  • input data
  • tests (if this, then do that, else do something else)
  • loops (repeat a similar operation enough times to process all of the input data)
  • output data

We call "algorithm" the idea of using everyday language to describe a simple program.

Input data

When you create a program, you will need input data. Maybe the current date, maybe the name of the user, maybe the size of the hard drive, maybe the name of an Excel worksheet where you have information that you want to process.

Programs use "input data". In order to manipulate this input data, we must be able to access it, and this is done with variables. Variables are words that store the data. Here's an example:
x = 4

Here, my variable is "x" and I assign the value "4" to x. So, further in my program, I can refer to "x" and my program will replace x by its value: 4. It's very much like our everyday use of names. If you have a boat and your boat's name is "Alice", then you can say: "I'll go out with Alice for an hour or two". And everybody understands that Alice is your boat. You can substitute one for the other.

Of course, variables are not limited to "input data". When you process your input data, it produces output data, which can, in turn, be used as input data for more processing. Example:
x = 4
y = x + 6
There you go! We've created a new variable: "y".


Programs react differently given different conditions. So, at the core of all programs is a mechanism that tests the conditions and allows to process the information in different ways, based on the conditions. Imagine the following example: taking care of the washing-up is conditional to the presence of dirty dishes in your sink. This situation could be described in a program with the following algorithm:
if (there are dirty dishes in my sink) then
  do the washing-up
  go out and enjoy life

This structure of if...then...else...endif exists in all languages. After the endif instruction, the program resumes processes that are independent of whether you had dirty dishes in your sink or not.


Imagine that you are a salesman. You've made a lot of sales and you don't get the same number of customers every month. So, if you have a program that takes action, like sending an email, for every customer you will need to repeat the same operation "x" times (where x is the number of your customers). There are 3 kinds of loops but they're all very similar to each other and will look like this:

numberOfCustomers = 37
currentCustomer = 0 
repeat numberOfCustomers times:
  currentCustomer currentCustomer + 1
  send email to customer (currentCustomer )

Here, we have 37 customers and all of them are identified by a value between 1 and 37. As many times as needed, we will move on to the next customer (currentCustomer +1) and send him an email.

Output data

Generally, a program returns information to the user. It can be an information of success in performing its tasks or an information of failure, or a return value in the case of calculators for example. Because we are talking of returning a value, programming languages often include a function called return which is intended exactly for this purpose. Here's an example of a program that calculates the square of an input value:
program square(x)
  y = x * x
  return y

As you see, we have this program named "square" and it takes "x" as input data. Then it calculates the square value, which is x*x and puts this value in the variable y. And finally it returns y.


That's it! This is the core of all programs. Once you know this, you can move on to learn the details of different programming languages.

No comments:

Post a Comment

Creative Commons License
Erik Lallemand's blog by Erik Lallemand is licensed under
a Creative Commons Attribution 3.0 Unported License.