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.
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 | 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 THEN ELSE ENDIF instruction, for example:
IF age < 50 THEN REM some instructions here ELSE REM some other instructions here ENDIF
If the person's age is less than 50 then perform some instructions otherwise (or else) perform some other instructions. You can put one or more commands after the THEN and ELSE instructions. The ELSE part is optional, so you can just have a IF THEN ENDIF statement. Sometime you can fit the command on one line like this:
IF age = 100 THEN PRINT "You are a century old."
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.
NOT x = Returns true if x is false, or false if x is true.
For mutiple decisions, you can use the IF ELSEIF ENDIF statement. Given a specific value, you can compare it to other values and run other instructions.
IF age = 10 THEN PRINT "You are aged 10." ELSEIF age = 20 THEN PRINT "You are aged 20." ELSEIF age = 30 THEN PRINT "You are aged 30." ELSE PRINT "You are aged "; age ENDIF
Here, age is compared with the value in the first IF statement, if there is a match the the instructions are run until it reaches a ELSEIFIF statement, if it finds one, it continues the instructions. . If not of the values match, then it will use the instructions after the optional ELSE statement. When the instructions are done, it will continue after the ENDIF statement.
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 variable =initialisation TO final [STEP increment] for example:
FOR count = 10 TO 50 STEP 5 PRINT count NEXT count
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 FOR and NEXT stementts will be run in each iteration of the loop. By default the STEP value is 1.
Another type of loop is the while loop. It will repeat the instructions in the loop while the condition is true. e.g.
number = 0
WHILE number <> 999 INPUT number PRINT number WEND
This will repeat the instructions between WHILE and WEND, 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 WEND statement otherwise the program will never finish.