Friday, January 13, 2012

A Generic Introduction To Computer Programming

I originally published this article on Yahoo Voices under the pen name John Mario.

This article does not focus on any particular computer language. It introduces computer programming by explaining some of the programming instructions that exist in all computer languages. It is directed toward those who are curious about computer programming, but have not yet decided to devote time and money to it.
 
Computer Programming is the art of using a set of instructions to force the computer to perform a certain task. The programming instructions described in this article will be written in plain english.
The following computer language instructions will be explained in this article:

The assignment statement
The PRINT instruction
The IF instruction
The ELSE instruction
The INPUT instruction
The WHILE instruction
The FOR loop
Instructions that perform math operations.

The Assignment statement
Any computer program needs to store temporary data provided by the programmer in memory. The programmer simply provides a name for the data being stored and the value of that data. The programmer does not have to know the exact memory location. The program automatically takes care of that.

For example: If you want to store the price of an apple, you could use the word apple_price as the name of the memory the price of apples is stored in. Then you store a price in the memory named apple_price. The instuction would look like this:

apple_price = 0.25

The statement simply states to store the value 0.25 in the memory named apple_price. This statement is known as an assignment statement. It assigns a value to the memory named apple_price.

The PRINT Instruction

The PRINT instruction can be used to display a value on the monitor. You can use the print instruction to display words, phrases and values.

Here is an instruction that displays the price of an apple.
print apple_price

Different languages will have different rules for typing the PRINT instruction. Some languages demand a set of parenthesis around the value to be printed.

print (apple_price)

You could also use the print statement to print a phrase. The instruction

print "Hello world!"

will display the message Hello world! on the monitor.

The IF Instruction

The IF instuction tells the computer when a specific action should be performed. For example, we want the computer to print the price of an apple if the price was less than one dollar:

If apple_price is less then 1.00, print apple_price.

The way to type less than is different for different languages.

The ELSE statement

The ELSE statement is part of the IF statement.
For example:

if apple-price is less than a quarter, then buy apples, else don't buy apples

The INPUT Instruction

The INPUT instruction tells the computer to read a value from a particular input. For purposes of this article, that input will be the keyboard. Not all programming languages name this instruction INPUT. The input instruction defines where it wants the input to be stored. For example

apple_price = input

tells the computer to input a value from the keyboard and store it in the memory named apple_price.

The WHILE Instruction

The while instruction commands the computer to keep doing something as long as specified condition exists. Here is a real life example of the WHILE instruction:

while tornado is near, stay in cellar

The FOR Instruction

The FOR instruction is used to repeat a something a specific number of times. The FOR instruction uses a counter to count the number of times the action is performed. Suppose we wanted to print apple_price five times. We would create a new memory name index to use as a counter.
For example

For index = 0 to 4, print apple_price

This instruction will set the value of index to 0 and prints the price of an apple, then it sets the value of index to 1 and prints the price of an apple. It increments the value of index and prints the price of an apple again. When the value of index is greater than 4, it stops printing the price of an apple. Hence, the price of an apple is printed five times: once for each value of index between 0 and 4.

Math
Math statements are also allowed in all programming languages. For example;

number_of_apples = 5
number_of_oranges = 3
total_number_of_fruit = number_of_apples + number_of_oranges

The memory named total will contain the sum of 5 and 3. Note that in this example, we wrote a short program containing three instructions. The first instruction sets the number_of_apples to 5. The second instruction sets the number_of_oranges to 3. The third instruction adds the number_of apples and the number_of_oranges to obtain the total_number_of_fruit. If we added an instruction to print the total_number_of_fruit, our program would look like this:

number_of_apples = 5
number_of_oranges = 3
total_number_of_fruit = number_of_apples + number_of_oranges
print total_number_of_fruit

References:

I have a Bachelor of Science degree in Electrical Engineering and I worked as an Embedded Systems Software Engineer. I designed and modified programs in several assembly languages and two high level languages.

 

No comments: