A-level Computing/Computer Systems, Programming and Network Concepts/Introduction to Programming

From

Jump to: navigation, search

Contents

What is Programming?

Programming is the ability to get a computer to perform tasks by writing instructions that a computer can understand and execute. You must remember that the only language a computer understands is binary, a combination of 1s and 0s. This section looks at how we can write instructions and get the computer to understand those instructions.

Programming Languages

When computers were first developed in the 1940s the only way to send instructions to them was by sending them 1s and 0s through flick switches and buttons. Even though the process of sending a computer instructions got better evolving through punch cards, paper tape, magnetic tape and disks, the language computers understand has not changed from the pattern of 1s and 0s, machine code. However the way humans construct these 1s and 0s has gotten easier.

Levels of Programming Language

High/Low level programming languages are terms used to describe how abstracted the language is from the raw machine code and hardware manipulation. Whether a language is high or low level is purely subjective towards the comparison made. Assembly is considered a low level language but is a higher level language than machine code as it resembles English more closely. B.A.S.I.C is even further abstracted and can thus be considered a high level language.

Machine Language - First Generation

The 1s and 0s that a computer can understand and execute

100100101110101000101011010010100101

It is very difficult for humans to understand and difficult to write unaided. Machine code tends to be produced for a specific hardware set up and thus it is not portable to different machines.

Low Level Language - Second Generation

In a low level language the programmer controls and specifies the specifics of how to handle the data, where to store it, how to access it and so on. Assembly is said to be a low level language. Here is a source extract to print "Hello World!" to the screen

; This program displays "Hello, World!"
dosseg
.model small
.stack 100h

.data
hello_message db 'Hello, World!',0dh,0ah,' '

.code
main  proc
      mov    ax,@data
      mov    ds,ax

      mov    ah,9
      mov    dx,offset hello_message
      int    21h

      mov    ax,4C00h
      int    21h
main  endp
end   main
File:B plus.svg easier to write and understand when compared to machine code
File:B plus.svg allows low level access to hardware features
File:B plus.svg can produce small program sizes
File:B plus.svg can produce very fast code
File:B minus.svg not as easy to understand and write when compared to high level language
File:B minus.svg may be written specifically for a certain hardware set up and will therefore not be portable

High Level Language - Third Generation

A high level programming language is one in which the specifics of data handling and manipulation is abstracted away (more so than in a low level language). A high level language generally has more built in functionality than a lower level language and is closer to English for a person to understand. C++ is considered a high language. Here is the source to print "Hello World!".

#include <iostream.h>
using namespace std; 
int main()
{
    cout << "Hello World!";
    return 0;
}
//Note: This code may or may not be compilable, as it varies between compilers and platforms.
File:B plus.svg easy to write and understand
File:B plus.svg built in libraries to perform routine tasks
File:B plus.svg can be ported to multiple hardware setups from same code
File:B minus.svg may be slower than second generation languages
File:B minus.svg may produce larger program files for same functionality than second generation language
File:B minus.svg may not allow for low level hardware access

Program Translators

We already know that computers can only execute Machine Code, so we need a way to convert from 2nd and 3rd generation languages to 1st generation, machine code.

There are three different methods that you may have to use. Assemblers, Compilers and Interpreters

Assemblers

When using Assembly language you will have to use an Assembler. The Assembler will take the assembly code and convert it into the corresponding Machine Code 1s and 0s (actually current flowing and current off). There is a one to one correspondence between a single assembly language instruction and a machine code instruction. In general there will be one line of Machine code produced per line of assembly language. This translation or conversion is called 'assembling' and is performed by a piece of software called an assembler before execution of the code can take place.

Compilers

When using a high level language you will have to use a compiler to turn your code into the corresponding machine code. This is done using a compiler and performed before the execution of the code. A program like a Playstation computer game will be written in a High level language and compiled into machine code. This code will then be burnt to disk at the factory and sent to the shops, when you buy it and place it in the DVD drive of the PS2, the processor will execute the machine code.

Interpreters

Another way of running code written in High level languages is to use an interpreter. This will convert the code you write in machine code for the processor to execute. However the difference here is that the code is converted into machine code as you are running the program, so the code is interpreted only when you need to run it, line by line. This can be much slower than compilation but allows for you to correct errors in the code (debugging) when you meet them. It is also used in common languages like Java and allows you to run the same code on multiple hardware set ups as long as you have an interpreter for that hardware. Think along the lines of running the same Java Application on Linux, Windows and your mobile phone.

Comparisons

You may be asked in the exam which translator you would recommend, here are a few things to think about

  • If the code is in Assembly Language (2nd Generation), then you will use an Assembler
  • If you want the fastest code you will use a compiler
  • If you want the code to be able to run on multiple machines you will need an Interpreter
  • If you want to debug code and not have to recompile with every change you make, you want to pick an Interpreter


Programming Concepts

In this module we will be looking at Imperative languages, Imperative languages are languages that execute one line after another, take this example for instance

int x = 1;
x=x+1;
x=x+1;
x=x+1;
cout << "x = " << x; // print x

output:

x = 4

Declaring a variable then assigning a value to that variable is called Assignment

int x;
x = 45;  // this is assigning the value 45 to the variable x

There are other sorts of languages that you will meet in module 4, but for the moment you only need to know about imperative languages. You will not need to know a particular language for the exam but knowing the concepts behind them and how to understand the pseudo code is essential - pseudo code is close to a third generation language, which is understandable what it should do to you and me, but probably wouldn't run on a computer. This section will now outline a few of the concepts you need to understand

Type Definitions

You may have met datatypes in GCSE when using databases or spreadsheets. The idea behind them is to use a type of data to store information that best matches the data you want to store. In computers this is incredibly important as you are trying to make the best use of the computing resources available, so picking the data type that matches the information you are declaring will be the most efficient way of storing the data. For example if you want to store a number you don't want to use a data type that can also store letters and other characters as this would be a waste of space. e.g.

3 stored in ASCII is 0010 0011
3 stored in pure binary is 0011, thus saving 4 bits

In any programming language you generally have 4 different data types.

  • Integers
    Integers are whole positive numbers, but unlike in maths the integer range will have limitations to the maximum number. The limitations can vary greatly.
    e.g. 104
int x = 45;  //declares an integer called x and sets the value to 45
  • Strings (Characters)
    Strings are a collection of single block characters. e.g.
    e.g. as54sfgjf/.dfg4
string x = "herman";
  • Booleans
    Booleans are variables that are like on/off switches and generally can only equal two values.
    e.g. TRUE/FALSE ON/OFF 1/0
bool x = TRUE;
  • Pointers
    Pointers are an advanced variable type. A Pointer is a 32bit or 16bit or 64bit number (depending on the environment), this number represents a single block in your computer memory. In essence it points to this memory block because you know that it is the nth memory block. Pointers are probably the most useful variable type however you may or may not come across them depending on what language you are using and how high/low level it is. You do not need to know about pointers for module 1.

There are also other datatypes that languages may possess, such as:

  • Character
    The Character data type is a single character.
    e.g. g
char x = 'g';
  • Real
    Real is a more advanced number system than integer, allowing for rational and irrational numbers, decimal places and negatives
    e.g. -12.3231
real x = -23.432;
  • Array
    Arrays are collections of other datatypes, generally able to store only one datatype, for example an array of type string may look like this:
    (red, green, orange, blue, brown)
array x = new array ("red", "green", "orange", "blue", "brown")// declare array with five elements, each of type string.  
  • Think of an array as a spreadsheet; you have the array (x), which is like a spreadsheet file, and you have the columns inside, e.g. x[2] (array x, 'column' 2). This is a one dimensional array. A two dimensional array would be where you have rows; e.g. x[2,3] (array x, column 2, row three). One can have, in theory, as many dimensions in an array as one likes.

Variables

In mathematics the idea of variables are introduced. Variables in a program are very similar EXCEPT that they may equal different values at different times of the program (this is why indeed they are called variables, they vary). They are used to store values that may be needed for the program, you can declare variables of any of the data type declared above. Think of them as a box with a name, you can change the contents of the box and look inside to see the value at any time:

int x = 5;
print x;
x = 104;
print x;
x = 32;
print x;

output:

5 104 32

Depending on the programming language you may or may not have to declare variables (in Basic you don't in C you have to). Declaring variables is simply telling the compiler/interpreter that you want to use a variable with that name.

Global and Local Variable

Dependent on where a variable is declared,the variable can be seen by other elements of the program code. There are two types of variable declarations, that is the Global and the Local variables.

Global variables are declared in the root area of the program code, that may be right at the beginning of the code (in C for example). The values stored in these variables will be accessible from any area of the code. This can be useful if you need to access a variable from anywhere in the program, but if you declare global variables when you don't need to access them all the time, they will sit and take up main memory, thus wasting precious computing resources. Use with care.

Local variables are declared in a sub area of the program code, this may be in a function or procedure. The values stored in these variables will only be accessible from the sub program they are declared in. This can be friendly on computing resources as they are declared when the sub program is called, used, and when the sub program is exited, destroyed. So you only use main memory when you need to store the variable and it does not waste main memory like global variables do. However if you think you will need to variable outside the sub program you should think of using a global variable.

An easy way to think about local and global variables is to think of them as local and global tv channels. Global channels such as Fox and MTV can be accessed from anywhere in the world, but local tv channels such as Channel 5 or Yorkshire TV, can only be accessed in certain areas.

Special cases could be where you declare a global variable called holder, if you then call a sub routine and in this routine there is a local variable declaration of another holder, you do not overwrite the global variable. What happens is for the duration of the sub routine the local variable holder is in charge, when the sub routine exits, the local variable is destroyed and the original value of the global variable now becomes dominant. Take this code for example

code:

main program code
 int holder = 4
 print holder
 ...
 subroutine caller
    int holder = 9
    print holder
 end subroutine
 ...
 run caller
 print holder
end main

output:

4 9 4

[[Rule of thumb:]] If you want to quickly tell the difference between a global and a local variable use these quick rules.

  • If a variable is declared inside a function or a procedure it is a local variable
  • If a variable is declared inside a iterative or selective statement it is local
  • If the declaration is indented from the left hand boundary it probably meets one of the above criteria and is local
  • If it meets none of the above statements and is declared in the main body of code it is a global variable

Constants

Constants are like variables in their declarations and the ability to look at the value stored inside them, however you can not change the values, the value of a constant remains, constant.

constant int x = 34;
print x

output:

34

Order of Operation

Program Design

There are several very useful programming statements that you can use. You probably use these in your everyday lives with out even thinking about it

Selection

An important part of programming is the use of selection, that is the ability to do something if a certain criteria is met. This may be as simple as increasing your health bar in a computer game if you eat a chicken drumstick or inserting the cooling rods into the nuclear reactor if the temperature exceeds a certain value.

File:If-Then-Else-diagram.svg The most common selection statement is the if statement, the idea is that you compare a value to some criteria, if the value and criteria match then you proceed in a certain way, otherwise you do something else. For example:

If Hungry Then
Eat
Else
'Go Out'
End

File:CASE-IF-THEN-END flowchart.png The other type is the Case statement, this can be summarised by several if statements where the value is compared to several criteria and the action of first criteria matched is performed, otherwise a default action may be performed.

Case Enter Restaurant and pick up menu
If Egg and Chips available Then
Order Egg and Chips
End If
If Pie and Chips available Then
Order Pie and Chips
End If
If Curry and Chips available Then
Order Curry and Chips
End If
If Pizza and Chips available Then
Order Pizza and Chips
End If
Default
Leave hungry
End

Iteration

An incredibly important part of computing is the idea of iteration, that is repeating the same thing again and again. You probably use iteration every day. Take writing lines in a detention for example; you write some lines, check to see if you have met the line limit, and if you haven't you write some more lines, check if you have met the line limit and so on, until you do meet the line limit and then you can stop. There are three main sorts of iteration.

File:While-loop-diagram.svg The while loop: For example:

While Hungry Do
Eat
End

File:Do-while-loop-diagram.svg Another type of while loop is a Do-While loop. This is slightly different from the While loop in that you perform the task before you check that you have to perform the task:

Do
Eat
While Hungry
End

File:For-loop-diagram.png The most complicated tool you may meet is the for loop. This is a glorified While loop and don't be put off by how complicated it looks.

For (start with Empty stomach, Is stomach empty?, Eat)
 Wipe mouth

Procedural Code

Procedures and Functions are very slightly different. Functions return a value whilst Procedures do not. An easy way to remember this is by saying that functions are fun, if you call them they will send you something back, procedures return nothing.

Functions - note the return value

code:
a = addtogether(2,3) + 4
print (a)
output: 
9

Procedure - note the lack of value returned by the procedure

code:
addtogether(2,3)
proc addtogether(a,b)
 {
  print(a+b)
 }
output: 
5


To communicate between different parts of the program and to pass variables to the the functions and procedures we use Parameters. For example

Function addtogether (number1, number2)
 {
  return: number1+number2
 }

where this code will add together the numbers 1 & 2.

Object Oriented Code

Structural Concepts

Methods

Objects

Call Stack

FIXME Expand this article. NB I cannot guarantee the accuracy of the information here, nor how relevant it is to the A Level Syllabus. It is worth knowing, anyhow. if it makes no sense to you then there are plenty of tutorials out there!

Object Orientated code is code which forms resides in blocks. An object, such as a class is declared, similar to how one would declare a variable.


   class myClass {
     
     public:
     
     void SayHello(string text) {
       cout << text;
       return void;
     }
   
   } anInstanceofmyClass myClass;


The above code declares an object (of type class) called myClass.

Within myClass a function is declared called SayHello(text), with protection level public (as opposed to protected, or private. Protection layers are effectively the same thing as local and global terms as used for variables. 'Public' means that the function is global, and can be accessed wherever an instance (creation) of the class exists. An object (by which I mean a variable, class, structure or any defined thing in memory) declared with modifier 'private' can only be used, called or accessed by something in that class, e.g. if I declared a private variable in myClass called text it could only be accessed by another Object within myClass, such as the SayHello() function. Protected is the protection modifier inbetween public and private. It can only be accessed by something within the same class, or by an object within a class derived from its class. Derivation, or Inheritance, as it is more often called, is a powerful feature or Object Orientated Programming (OOP), which basically means passing on the objects in one class to a 'child' class. The child class can have its own objects, as well as the objects it received from as many 'parent' classes as required. It can also redefine these objects within itself, e.g. SayHello() no longer takes a variable, it prints the text "hello" on the screen without input. I hope this is all making sense to you!

After the class is defined, as instance (Object) of the class is created, called anInstanceofmyClass. One can create as many instances of a class as one likes, e.g. anInstanceofmyClass1, anInstanceofmyClass2, anInstanceofmyClass3 myClass; To call the SayHello() function, one would type anInstanceofmyClass.SayHello("Hello Text"); anInstanceofmyClass1.SayHello("") and anInstanceofmyClass2.SayHello("") both do the same thing.

Personal tools
MediaWiki Appliance - Powered by TurnKey Linux