A-level Computing/WJEC (Eduqas)/Component 1/Principles of programming

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Paradigms[edit]

A paradigm is the fundamental approach of a programming language, based on the features of said language.

Procedural[edit]

Procedural programs work in a sequential manner, executing lines of code as they are encountered and then advancing to the next line of code. C and Python are examples of languages which adopt this approach. Procedures work based on variable assignment (a specific memory location will store the value), selection (IF statements), iteration/loops (repetitively executing code) and procedures. The procedural paradigm gets its name from its modularity as it divides codes up into procedures (blocks of code). The paradigm is part of the larger imperative programming paradigm which makes use of statements to change the state (the values of any variables at any given moment in time) of a program. This state can be modified by changing a variable, for example through variable assignment.

Visual[edit]

The visual paradigm incorporated in Scratch allows a user to display "Hello, World!"

Rather than typing code out, the visual paradigm is where a series of GUI elements such as buttons and diagrams can be interacted with to create code. Scratch and GameMaker are examples of programs that use this approach. Scratch uses a drag-and-drop application where commands are dragged in from a set group of commands and this generates code. This paradigm is useful for introducing new developers as it allows them to focus on the logic rather than learning the language, but is not suited for seasoned developers, as they are used to typing code or even complicated programs due to their sheer complicity.

Event-driven[edit]

The event-driven paradigm is based on a GUI (Graphical User Interface) and makes use of any events that occur when the program is running. VB.NET Windows Forms use this approach. Programs sit in a constant loop, waiting for any input from the user. When an interaction is triggered, for example a user clicking a button, a listener is called. The listener then processes the event and executes the relevant code in the program.

Object Orientated Programming[edit]

Object Orientated Programming (OOP) is a paradigm in which code is developed around interactions with objects, classes and methods. Objects are a combination of data and the actions that can be carried out on that data. A class, on the other hand, is the definition of an object - it describes the information needed to create and manipulate objects of that type. This includes information about the object itself (its attributes) and the code for the actions it can perform (called its methods).

There are 3 key principles of an object oriented program: encapsulation, inheritance and polymorphism.

  1. Encapsulation hides the implementation of a class, and controls access to methods and data of an object. This means data is kept inside the class, and is inaccessible to the rest of the program. Objects control how other objects interact with themselves, preventing errors e.g. a programmer cannot set the width of a window to -500. An secondary advantage of encapsulation is that once an object is created, knowledge of its implementation is not necessary to be able to use it. Objects created this way can easily be reused in other programs, and by different programmers.
  2. Inheritance comes about where classes are grouped by similar attributes and methods. Attributes and methods that are in common across their classes can be given to a parent class, and the other classes become subclasses of this parent class. The affect of this is that the subclasses inherit the methods and attributes of their parent class - that is to say they can use them as their own. This is a benefit, as it means a method that is held in common only has to be defined once, and then all other classes that use it can look to their parent class instead. Inheritance is relevant when new objects are created (instantiation).
  3. Polymorphism is simply the idea that a method can work differently depending on the class that runs it or the data it is given to work with. For example, an Area method would calculate area different for the classes 'circle' and 'square', but both would inherit the class from their parent class - 'shape'.

Languages[edit]

Mark-up[edit]

The mark-up language works by adding annotations to a normal text-based document, known as mark-up, distinguishing the annotation from the plaintext. This is accomplished using chevron characters to contain the annotation, for example:

<bold> whoa this is some HTML </bold>
RecipeBook, an XML representation of recipes.

Examples of Mark-up languages include HTML (Hypertext Mark-up Language) and XML (Extensible Mark-up Language). HTML is used for the delivery of webpages and XML is sometimes used to represent data structures on the web. XML has no set conventions and the developer is free to add their own meaning to each piece of data, this is demonstrated in the image. Whereas HTML has presentation semantics meaning that there is a set standard that must be followed in order for a webpage to be rendered on a browser.

Special Purpose Languages[edit]

Special purpose languages are ones that were designed with a specific purpose in mind. They might include essential/helpful features relevant to the application. Special purpose languages are available in fields such as simulation and control. They are very specialised with built-in functions/abilities that lend themselves directly to solving the problem that the language was designed to work on. Commonly used in applications such as: Computer-aided design (CAD), Artificial Intelligence (AI), expert systems, scientific applications and games programming.

High-level Programming[edit]

In high-level programming languages the process of addressing the memory registers and accessing the hardware is abstracted away from the developer. Rather, the instructions are written in a semantic manner that is understood by humans, which translates into many lines of machine code (0s and 1s).

Low-level Programming[edit]

In low-level programming languages the developer has direct access and can manipulate the memory registers and the CPU, as well as being able to directly communicate with the hardware devices attached to the computer. Different CPU architectures execute a different instruction set, and the developer can access these instructions via low-level programming. The developer could use a common language such as Assembly which utilises mnemonics to execute machine code, where one line of Assembly translates to a single line of machine code. To convert Assembly back to high-level code, an assembler must be utilised.

Standardisation[edit]

The USB (Universal Serial Bus) connection medium is an example of a standard. Standards are used to ensure that various products can integrate with one another, for example an external HDD via USB to read/write files on an operating system, which makes use of the USB standard on all computers. Groups will be formed with various people involved in the process in order to develop a standard.

Unambiguity in Syntax[edit]

Ambiguity in reference to syntax means that the syntax can be interpreted in more than one way. This is not possible for a computer to understand which way to interpret code If, for instance, a computer was given a statement with two Else statements, it will not be able to determine which one to enter. Backus-Naur Form (BNF) is used to write down the grammar of a programming language e.g. semicolons, defining rules that must be followed in order for code to be considered syntactically valid. The rules must not be ambiguous.

Syntax Diagrams[edit]

Represent BNF in a more accessible form. This demonstrates the rules to a person reading the diagram, but is not in any way meant to be used with a computer.

Backus-Naur Form[edit]




Backus Naur Form (BNF) is a formal way of writing the grammar of a programming language. The main idea is to introduce rules to ensure that the source code is syntactically correct (the arrangement of each part is correct) and unambiguous (it can only be interpreted in one way, rather than multiple ways). To introduce these concepts to code, we use grammar rules like normal English, these being contained within chevrons (<>). There are three parts that can be included in one of these 'grammar rules': terminal symbols, non-terminal symbols and production rules. A terminal cannot be evaluated any further (this simply means it cannot be broken down any more), whereas a non-terminal requires further evaluation (it must be broken down before it can be interpreted). To create a rule, we must first be familiar with the symbols used within rules, and what they represent. Here is an example:

<Digit> ::= 0|1|2|3|4|5|6|7|8|9

<Digit> is used to start the rule and give it the name of 'Digit', note the chevrons used. "::=" is used to represent assignment. "|" is used to represent the logical operator OR (this is a pipe character). This rule can be used to construct any number we need since all numbers must contain 0-9. It is very important that you start from 0 otherwise you could not include large numbers such as 100 or 900. The numbers in this example (0-9) would be considered terminals since you cannot evaluate them any further.

<TwoDigitNumber> ::= <Digit><Digit>

Here we have declared the rule 'TwoDigitNumber' which consists of two 'Digit' rules. As you can see, we need to consult the 'Digit' rule to see what this rule means - we need to evaluate it further - so this is a non-terminal.


You may be wondering how we can construct words or letters with BNF and quite rightly so! Let's imagine we're creating an email using BNF to represent everything. We would need the following rules to do so:

<Uppercase> ::= A|B|C|D|E ... |Z

(note here how we've used ... to represent the continuation, you can do this too in the exam!)

<Lowercase> ::= a|b|c|d|e ... |z

This allows for use of any letter in the alphabet, but as a lowercase character.

<Digit> ::= 0|1|2|3|4|5|6|7|8|9

(usually needed in all BNF questions, e.g. some emails may contain an EmployeeID in this case.) These are all the rules we need so far for creating our email, but there is a problem with the current rules we've used. People can only have an email consisting of single letters and digits. This is obviously problematic when we want to have a full name in the email. We can use a concept covered before, recursion, to combat this issue:

<Name> ::= <Lowercase>|<Lowercase><Name>

In the <Name> rule there are two possible paths, the first and the second one. In the first path it accepts a singular lowercase character, which will be called if there is a single letter only. Otherwise the second path will be called which will accept a single character but will also call the rule again - so there can be as many lowercase characters as you desire, creating a full name. This way of thinking is very helpful as it allows you to remember the recursion rule properly, as it must be in this exact format otherwise it is not correct. There must be two paths, either just the singular character or the singular character and the rule being called again.

Finally, after we've addressed all the possible constraints on the data, we create a final rule which contains all the logic. The question used for this example is below:

Email Exam Question

The email addresses of staff at National Bank are made up of a first name, followed by a full stop, followed by a surname, followed by a single digit, followed by the @ sign, followed by 'nb.co.uk'. All first names consist of lower case letters only, and can be any length.

Answer:

<Lowercase> ::= a|b|c|d|e ... |z

<Digit> ::= 0|1|2|3|4|5|6|7|8|9

<Name> ::= <Lowercase>|<Lowercase><Name>

<Email> ::= <Name>.<Name><Digit>@nb.co.uk

Note that there is exact requirements in the rule as stated by the question - we need the full stop, the @ sign and the banks domain at the end of the email address. This can simply be added verbatim in the rule as shown above, the full stop '.' and the '@' symbol with the 'nb.co.uk' domain.

We can also represent BNF in a diagrammatic form, but this is only for people to easily see what it represents rather than having a bigger purpose. Three shapes are used to do so: a rectangle with two lines to the left and the right within which represents a non-terminal, an oval to represent a terminal and an arrow which represents the flow of information (the path taken). Within both symbols there is text within them to show what they represent. An oval (terminal) may contain "1" and a rectangle (non-terminal) may contain "Digit". Recursion is represented just drawing a line with arrows going back before the decision to go through the non-terminal, so this shows that you can go back until the entire string has been constructed.