The Basis of Computer Programming

A computer program, as we have seen, is a set of instructions that we issue to a computer for it to carry out a task for us. To be able to tell the computer what to do, we need to have the basic idea or steps involved in our mind that would be required to complete our task.
This brings us to the concept of “logic”, which infact is the base and core of every computer program that you write – however simple or however complex the program might be. The logic, in simple terms could be seen as the “trick” behind the solution of a problem. It is that step(instruction) or set of steps that form the most important part of the program and is the basis of the problem solving.
Consider for example – finding the average of 5 numbers input by a user. What is the first thought that crosses your mind when you think of the solution of this problem? Simple mathematics tells you, we need to add the numbers and divide them by 5. This very step, which forms the core to the solution of this problem is infact the “logic” behind the solution of this task and what our program would be based upon.
When we write a program for the above task, we will
1. Take the 5 numbers from the user
2. Add them
3. Divide them by 5
4. Give the result back to the user
Out of all these steps, as we can clearly see, the most pertinent steps on which depends our entire output and the action the program performs are 2 & 3 – adding the 5 numbers and dividing them by 5. If we were to change these steps, the whole purpose and behavior of the program would change.
Consider for a moment that we replace steps 2 and 3 by the following:
1. Take the 5 numbers from the user
2. Take the first number & store it in a variable “SMALLEST”
3. Take next number
4. Compare with “SMALLEST”
5. If number < "SMALLEST" Then, store this number to "SMALLEST" 6. Repeat step 3 to 5 for all remaining 4 numbers 7. Give the result stored in "SMALLEST" back to the user What do you notice here? The first and last steps are still the same. What we have changed is the core or the "logic" of the program i.e. once the user has input the 5 numbers, we have changed what we do with the numbers! And this change now gives us the smallest number from the 5 numbers input as the result. This thus becomes a program to find the smallest number from a list of given numbers! Having now seen the importance of logic to write a program, we would next be looking into the tools that help us draft out the logic before we formulate it into a ready computer program.