The Basics of Programming

What is programming?

In dictionary terms, it is the act or process of planning or writing a program. A program is a set of instructions which process an input and produces an output. For example, a word processor processes input from a keyboard, using letter, digits and punctuations to produce a document. A spreadsheet will input numbers and produces sums, averages and those values into charts and graphs.

A program is designed, written and tested by a programmer. The tools of a programmer can vary depending on the operating system, the programming language and the development tools available which can vary from a simple text editor and a command line to a fully integrated development environment (IDE) with a graphical front end and built in features to make it easier for the programmer.

There is a huge range of programming languages from assemblers that can write programs in native machine code to Microsoft's Visual Studio using the .NET Framework. Typical languages include C/C++, BASIC, COBOL, Java, Pascal, Delphi, Fortran, ADA, Logo, Lisp, Forth and many others.

Three constructs of programming

A program can be written with three main constructs: sequence, decisions and loops.

a) Sequence
A sequence is a series of instructions that follow one another in order. For example to add two numbers together will take five steps as follows:

1. Set total to 0.
2. Add number 10 to total.
3. Add number 15 to total.
4. Display total
5. End

The result will be 25 in this case. To add more numbers you can add more instructions between steps 3 and 4.

b) Decision
A decision is where you want to test for things, for example, if you count from 1 to 100, you would need to test whether you have reached 100. Also, if you had a database of people, and you want to test if they lived in London. Or that you wanted to test people if they are aged between 20 and 49. To use decisions, you need to use conditional operators that compare values, the result of a comparison will give a true or false result.

Condition Description
= Equals
!= Not equals
< Less than
> Greater than
<= Less or equal to
>= Greater or equal to
! Not

For example, the test 12 = 24 would give a false result as 12 does not equal 24. Another test, 45 > 5 would be true as 45 is greater than 5. Also, 80 < 40 would be false as 80 is not less than 40. If you used a string of characters, then the condition would test each character in turn. For example, London < Londonderry would be true as Londonderry is longer, or Berlin < Hamburg would be true, as B would occur before H in the alphabet. The Not condition can reverse a result so ! (12 = 24) would return true or ! ( 95 = 95) would return false.

In programming, to make decisions you would use the if else instruction, for example:

if ( age < 50) {
    /* some instructions here */
}
else {
    /* some other instructions here */
}

If the person's age is less than 50 then perform some instructions otherwise (or else) perform some other instructions. Use curly brackets { } to define the instructions to be executed after the if or else commands. If you miss out the curly brackets, then only one instruction is performed after the if or else commands.

You can also combine conditions using some additional operators known as boolean operators, because they produce true or false values. They are AND, OR, XOR and NOT.
x and y = Returns true if both conditions x and y are true.
x or y = Returns true if either condition x or y are true.
x XOR y = Exclusive Or. Returns true if only one of the conditions x or y is true.
! x = Returns true if x is false, or false if x is true (The exclamation character = NOT)

For mutiple decisions, you can use the switch case statement. Given a specific value, you can compare it to other values and run other instructions, you will also need to break out of a switch case when those instructions are done.

switch (age) {
  case 10:
    printf("You are aged 10.\n");
    break;
  case 20:
     printf("You are aged 20.\n");
     break;
  case 30:
     printf("You are aged 30.\n");
     break;
default:
    printf("You are aged %d.\n", age);
    break;
}

Here, age is compared with the value after the case statement, if there is a match the the instructions are run until it reaches a break statement, if it finds one, it continues the instructions after the closing bracket. If not of the values match, then it will use the instructions after the optional default statement. If you leave out the break statement, then all instructions are run after the matching case statement until the closing bracket.

c) Loop
The third construct is a loop. Computers are very good at processing vast amounts of data. So to do that, the loop can perform the same set of instructions over and over again. There are three types of loop:

a) A Counted Loop
b) A loop which is performed at least once and the condition is tested at the end of the loop.
c) A loop which is performed none or more times and is tested at the start of the loop.

A counted loop is used when you are counting between an initial and a final value, for example, between 1 to 50. A counted loop can also count, not just in steps of 1 but in other values and even count downwards using negative step values. The typical instruction used is the for instruction. The format is for( initialisation; condition; increment) for example:

for ( count = 10; count <= 50; count=count+5) {
 /* some instructions */
}

The word count is a 'variable' which stores the count value and it count from the initial value 10 to the final value 50 in steps of 5, e.g. 10, 15, 20, 25, 30, 35, 40, 45 and 50. The instructions between the { and } brackets will be run in each iteration of the loop.
If counting up by 1 you can use the shorthand method using variable++ or if counting down by 1 you can use variable--.

Another type of loop is the while loop. It will repeat the instructions in the loop while the condition is true. e.g.

while ( number != 999) {
 /* some instructions */
}

This will repeat the some instruction between the curly brackets {} while a variable called number does not equal 999. Beware, you have make sure that the number does equal 999 at some point, so that the loop does exit, and continue with instructions after the closing curly bracket otherwise the program will never finish.

Finally, another type of loop is the do loop. The difference between the while and the do loop, is that do loops are always run at least one, before it tests the condition, which appears at the end of the loop. e.g.

do {
   /* some instructions */
} while ( number != 999);

This will run the some instructions once before reaching the while instruction, it will test if number do not equal 999 and will repeat the loop if it is true, otherwise it will exit the loop on the next line.

Next Page