Monday, October 22, 2012

Basics of Programming

To become a programmer, you need time. So it may be a good idea to push other things aside for a while and find more idle time for self. Let me tell you why. Consider the evolution of mankind. In the beginning, there were a lot of wars. Kingdoms overthrown and established again. People could seldom find time for studies or inventions or discoveries. Over the period of last one million years that mankind walked this earth, things began to slow down. Less and less of wars, and more people taking their time out to do, invent and discover new things. This is quite evident in the developments that took place in the last century. Today there are more researches going on in almost all fields of science, maths, religion, sports than it ever did. Computers showed up late implying that it was a direct resultant of all the developments that had been done before it. This pretty much concludes that when you spend a lot of time into studies, you end up with some thing like the computers. And so when you want to master this latest piece of artwork, you will want to spend time for it. So much for the philosophy. Now that you have decided to become a programmer, you will need a couple of other things besides time, which would be a computer and a teacher. Get these, and you are ready to go. And what is it that you are taught first? About the Computer itself. You probably are already aware that a basic computer consists of a CPU, a memory and input/output devices or peripherals. So the immediate question is why these 3 parts? Why not something else? Let us see why. Computers, at the end of the day, should do a job that you instruct it to do. Sounds like a slave, yea? Thats right and thats exactly why there are courses like "Masters in Computers"; you are taught to become the master and the computer, a slave. Lets analyse the slave first. So what should a slave do? 3 things: 1) Listen to what you have to say 2) understand what you say 3) do what you say For the first point to materialize, you would expect a human slave to have ears. Well the computer, it has peripherals. For eg: a keyboard or a mouse. A keyboard is an input device of the computer, much like ears for the humans. But why a keyboard? Consider for eg, your friend wants you to do something for him. The thought of getting some help from you originated in his brain and had you guys been practicing telepathy, it would have been great. As in he would think and you would get it. But its not how it works in the real world. He thinks in his brain and uses sound to communicate his thoughts across to you. Your ears receive the sound, your brain interprets and saves them in the memory. You later remember or retrieve his thoughts from your memory and you do it. If you analyse carefully, your ears were the starting point of interpreting your friend's thought. Similarly, the keyboard is starting point of the computer in interpreting your thoughts. The keyboard produces electrical pulses that the computer can store and interpret later on to do the job. The computer needs electrical pulses and not sound(there are computers today with peripherals that can respond to your sound as well but they are not as reliable as a keyboard yet) and that is exactly what a keyboard supplies. You may have also noticed that I used the term memory now and then while explaining the paragraphs above. Time to analyse the importance of memory. Lets consider for a moment that you did not have memory and your friend requests you for help. Thus the moment your friend makes the request, you will have to do it. In fact without memory, every word that your friend says is immediately converted to action, as your brain now only knows to execute but not to store and execute. Although you get the job done, the result is less helpful because if your friend wants the request to be done after some time, he will have to wait until that time and then instruct you. In case he wants the help again, he will have to do that again. Chances are he wont use you again. Thus intelligence is not just knowing how to execute but also storing and executing. Thus memory is a vital part of being intelligent. This is more or less the case in computers as well. When you type into the keyboards, the computer effectively uses its memory to store your instructions so that it can go through your instructions to understand and then execute them. It can also then use the same memory to store the results of the execution in order for it supply the results to an output peripheral like the screen. Next point for the slave is to understand what you've got to say. Well when the keyboard is already converting our thoughts into electrical pulses that the computer understands, why is there again an understanding step? This is quite simple in fact. Recollect your friend requesting a help. What if he is requesting it in a language you do not know? Well your ears will still transmit the sounds to your brain, only your brain wont be able to make out a single word of it. Request fails. Similarly, the computer has a pattern of electrical signals that it can identify as instructions and any other pattern would be considered random or noise. There are 2 ways to solve this. Either we type in the computer's pattern or its native language(which is a pattern of 1s and 0s) or you could type in a pattern or language that is comfortable for you and then teach the computer to convert your pattern to its native pattern. The latter, although sounding like more work, is the preferred way in todays world. Here is the reason why. The computer is an electrical device and all electrical devices basically just understand 2 things. Voltage on or Voltage off. Take the case of humans instead. We understand a lot more. For eg: we understand the various colors, smells, quantities like distances, weights etc. Now if, for the sake simplicity, we lived in a world that had 10 different things, we could uniquely identify each one of them using any of the different senses that we possess. Say we could use symbols or quantities in maths like numbers or even use the different colors to identify them. But since a computer has just 2 states so in order for it identify 10 different things, it will have to combine its only available logic to do this. For eg: The computer could provide a peripheral that had 4 keys where pressing a key would send a voltage else it would not. Thus the 1st item would be (Voltage off, Voltage off, Voltage off, Voltage off - none of the keys pressed) and then 2nd item could be identified as (Voltage off, Voltage off, Voltage off, Voltage on) and the third item as (Voltage off, Voltage off, Voltage on, Voltage off) etc. It is this very Voltage on and Voltage off states that later got represented as 1 and 0 for the sake of simplicity. Yet still the representation of all the things in our world(which is quite a big and mad place) in 1s and 0s turned out to be hectic and there was required a way to bypass this burden.