OCR 2 Programming Techniques Revision Notes
OCR 2 Programming Techniques Revision Notes
Aim is to be user friendly, i.e. effective, efficient and satisfying for the end user.
Very often, the user interface requires data to be input using a keyboard and mouse.
Usually takes the form of a GUI (Graphical User Interface) or a form-based interface.
A number of factors should be considered when designing an input screen...
o The user age, computer literacy, disabilities etc.
o Layout using headings, not cluttering but using all space etc.
o Order heading at top, action buttons (in forms etc.) at bottom
o Validation interface should reject invalid data
o GUI objects objects should make data input efficient / avoid errors
o Online help interface should keep user informed (progress bars etc.)
Data capture forms are used to collect data to be input into a program.
o Layout data fields should match / correspond to paper version
o Instructions no validation checks on paper; clear instructions essential
o Readability if data cannot be read, it cannot be input (encourage CAPITALS)
Page 1 of 21
When designing other types of interface, such as touch screen interfaces, other
factors must be considered. For example, the contact areas must be large enough on
the touch screen to be used effectively.
People with disabilities should also be catered for, for example, by adding sound
output in addition to the visual output.
Programs need to be presented so that they can be easily checked for completeness.
This can be done using a data dictionary.
This is a file containing descriptions of the data in a program these details include:
o Identifier (i.e. the name) for variables / fields
o Data type / data structure
o Size of the data
o Any validation that needs to be carried out on the data
As computer programs are so complex nowadays, it has become difficult for one
person to devise a solution or to implement it on their own.
A modular, top-down design can be used to tackle such problems.
Tasks are continually split down using stepwise refinement.
Subtasks are implemented independently as modules.
These modules are then put together to create a solution for the main problem...
Mobile
phone (for
example)
Text
messages
Voice calls
Receiving
voice
Sending
voice
Capture
voice
Convert
voice to
digital
Transmit on
network
Phonebook
Receiving
text
messages
Sending text
messages
Multitap
entry
Multitap
text entry
Predictive
text entry
Transmit on
network
Search
functions
Page 2 of 21
File access
Start / Stop
Process
Input / Output
Decision
Subroutine
Connector
Page 3 of 21
Page 4 of 21
These are short definitions of key terms for structures of procedural programs.
The definitions highlighted in red above at the three basic programming constructs.
These allow us to control how the program will execute the statements in it.
ITERATION is useful because otherwise we would have to write the instructions out
the correct number of times.
There are two types of control loop, condition-controlled and count-controlled.
o A condition-controlled loop keeps going until a certain condition is met.
o A counter-controlled loop repeats until it has run a certain number of times.
There are different kinds within each control loop.
Within the condition-controlled loop there is a WHILE and a REPEAT UNTIL loop...
Page 5 of 21
WHILE
Condition is tested before each cycle.
Instructions may never be executed.
REPEAT UNTIL
Condition is tested after each cycle.
Instructions will always be executed once.
In both cases, it repeats if the condition is TRUE and exits if the condition is FALSE.
By changing the conditions and/or some of the data in the program, it is possible to
convert a WHILE loop into a REPEAT UNTIL loop that performs the same function.
The parameters of a procedure are a special kind of variable, used to refer to one of
the pieces of data provided as input to the subroutine.
These pieces of data are called arguments.
An ordered list of parameters is usually included in the definition of a subroutine.
Each time the subroutine is called, its arguments for that call can be assigned to the
corresponding parameters.
Page 6 of 21
Instructions are then executed using the actual values substituting parameters.
This is referred to as passing parameters to the procedure.
Tracing the execution of a recursive call means following the execution step by step.
As you follow it, you note...
o Which lines are executed
o What happens when each line is executed
o Where recursive calls are made
This is best illustrated as an algorithmic diagram...
Page 7 of 21
Iteration
Uses a loop to repeat instructions.
Recursion
Repeats by calling itself with
simplified arguments.
A data type is a formal description of the kind of data used in a computer program.
NUMERIC
Used to store numbers.
Stored by converting to binary.
Integer used for whole numbers.
o
2 / 4 bytes
4 / 8 bytes
BOOLEAN
Data that can only have two values.
Generally TRUE / FALSE.
Used to store whether a condition
has been met.
Typically stored in 1 byte for ease.
CHARACTER (ALPHANUMERIC)
Stores just one character / symbol.
Typically 1 byte.
If we want to store longer text than
one character, we use a string.
STRING (ALPHANUMERIC)
Each character usually takes 1 byte.
The length of a string is the number
of characters stored in it.
A telephone number would be
stored as a string because each digit
(character) is important, not the
actual value of the overall number.
Page 8 of 21
Data structures allow us to store more than one item of data together.
o Under one identifier (name).
Can be used to access individual items of data.
Two data structures arrays and records.
VotesForCandidate(1) = 2300
Element
Array
Index
The highest and lowest values of the index are the bounds.
The advantages of using an array as opposed to separate variables...
o All elements are declared in one statement.
o If number of elements changes, change bound of array in declaration.
o Array contains similar data; loops can be used to perform similar operations.
Records can be used to store several data items under one identifier.
Individual items of data inside the record are called fields.
It helps to know the number of records in a file and the size of the records.
This allows the size of the file to be calculated.
The code below defines a student and assigns values to its records...
DIM TheStudent as Student
TheStudent.Name = Jack Bennett
TheStudent.Gender = M
TheStudent.Age = 17
Page 9 of 21
Page 10 of 21
Field
Name
Gender
Age
Data Type
String
Character
Integer
Size
20
1
2
These are the main file operations the way they are implemented varies.
PREPARING THE FILE: most languages require the program to make the operating
system aware that a file is about to be used. This is done using an OPEN command.
When the file is opened it is often necessary to specify how the file is going to be
used this is called access mode. It protects the data in the file from being
corrupted. This is why it is good practice to open a file for as little time as necessary.
Page 11 of 21
When the operation is over, the file should be closed. This releases the file so that it
can be used by another part of the program or indeed a different program.
READING DATA FROM A FILE: languages provide different methods for reading data. The
simplest way is to read the data from the file one line at a time. This is useful for
searching through a serial file. In this case it is necessary to test, before reading each
line, that the end of the file has not been reached.
WRITING DATA TO A FILE: you also need to be able to write the data into a file that has
been correctly opened this can include adding data to the end of a serial file
(appending). Inserting data into a sequential file will require all subsequent records
to be moved to make space for the data to be inserted. An alternative method is to
create a new file and copy all the records from the present file into it, inserting the
new data in the file at the correct location. The new file then replaces the old file.
This is called merging the data into the sequential file.
FILE MANAGEMENT: programming languages also provide facilities for copying, moving,
updating, deleting files and finding out if a file exists.
Not to be confused with equality operator which also often uses = sign.
o Hence it is often read as becomes rather than equals.
o Some languages use a different sign (e.g. :=) most dont!
You cannot swap the left- and right-hand sides of the assignment operator.
o Using x = 7, swapping would give 7 = x (7 becomes x nonsense!).
o Left-hand side therefore must be a single variable.
o Right-hand side must be an expression that equates to a single value.
Arithmetic operations are used to manipulate numeric data and produce a result.
Numbers which are manipulated are called operands.
Arithmetic operations only tell us how to manipulate the data.
Page 12 of 21
Unary operators only have one operand operator written before operand.
Binary operators have two operands operator written between two operands.
Operation
Ordinary (Real)
division
Symbol
/
Meaning
Normal division operation;
result is real number, even
when operands are integers.
Examples
13 / 5 = 2.6
15 / 3 = 5.0
2 / 9 = 0.222...
Quotient (Integer
division)
Remainder (Modulo
arithmetic)
DIV
sometimes
% or \
MOD
13 DIV 5 = 2
15 DIV 3 = 5
2 DIV 9 = 0
13 MOD 5 = 3
15 MOD 3 = 0
2 MOD 9 = 2
Relational Operator
= (or ==)
<> (or !=)
<
>
<=
>=
Mathematical Equivalent
=
<
>
Meaning
Equal to
Not equal to
Less than
Greater than
Less than or equal to
Greater than or equal to
Binary operators have two operands operator written between two operands.
Therefore all of these operations are binary.
Unary operators
Multiplication and Division
Addition and Subtraction
Relational (comparison) operators
Boolean operators
Assignment
(negation), NOT
*, /, DIV, MOD
+,
<, >, <=, =>, = (equality), <>
AND, OR
= (assignment)
Again as with maths, parentheses (brackets) can be use to change the order.
o Items in brackets should be evaluated first.
If theres more than one set of brackets, then they are evaluated in
turn from the innermost to the outermost.
Page 14 of 21
Concatenating two strings means joining them together to make one string.
Many languages use the operator + for this operation.
Where FullName, FirstName and Surname are string variables...
FORMAT
What it does
Example
LEFT(<string>,<number of characters>)
Returns the given number of characters from the left of the string.
LEFT(robson,3) returns the string rob
FORMAT
What it does
Example
RIGHT(<string>,<number of characters>)
Returns the given number of characters from the right of the string.
RIGHT(robson,3) returns the string son
FORMAT
What it does
Example
Page 15 of 21
For this reason, programmers must be aware of how strings are compared.
For example, they must replace accented characters with non-accented equivalents.
o The name Zo would have to be replaced with Zoe.
Otherwise Zo would come after other words such as Zoo.
These are short definitions of key terms for writing maintainable programs.
Variable facility for storing data which can be referenced by identifiers. The current
value of the variable is the data actually stored in the variable.
Constant a special kind of variable whose fixed value cannot be altered by the
program during execution.
Identifier the name for a variable or constant.
Reserved Word words already used in the language for a specific purpose, generally
functions and operations that are inbuilt.
Some programming languages will initialise variables (when they are declared).
o This basically means that the variables are given a starting value.
Generally integers are set to 0, Booleans are set to FALSE and strings are empty.
Page 17 of 21
Many programs contain values that do not change while the program is running.
These may be universal constants that are always the same.
New programmers tend to use literals in their code for such values.
o This means that the code contains the actual values spelt out in full.
As a general rule literals should be avoided in code.
They should be substituted with constants unless there is good reason not to.
Indentation
Every time a code structured is used which has a beginning
and an end statement on separate lines, the code within the
structure should be given a new level of indentation.
Formatting
As well as the vertical white spaces created by indentation,
the code should be grouped into logical blocks that perform
parts of the main task by entering blank lines between them.
Page 18 of 21
Occurs due to an
unexpected situation with
the data being processed
or another external factor.
Program would otherwise
work under normal
operating conditions.
For example, (stack)
overflow errors, or library
errors.
Test strategies specify what type of testing should be carried out at each stage.
Page 19 of 21
Alpha and beta testing are different to black and white box testing.
Carried out when software is nearly complete and is being tested as a whole.
Applies especially to commercial software: developed for external end user.
ALPHA TESTING
Takes place within the company producing the software.
Employees test the program as though they were potential users.
At this stage, software is not complete and there will be several bugs.
Restricting number of people allows swift replacement with updated versions.
BETA TESTING
Takes place after alpha testing.
Software is now very similar to the final product.
Released to potential users outside the company.
Testing takes place in actual conditions software will be used in.
Useful as end users may find bugs the company had not anticipated.
Beta testers also report constructive comments about features of program.
Debugging tools provide facilities for finding and removing bugs (faults) in software.
Most obvious is translator diagnostics.
o Messages generated by translator while it translates source into object code.
Useful for finding out where there are syntax errors.
Often include warnings where there might be a logic error.
Page 20 of 21
Its necessary to ensure that the finished program will run smoothly for the end user.
This is called installing the program on the end users computer.
Executable files (.exe) are often the best way of installing a program.
If they are not available, the source code and translator program must be copied.
o From the programmers computer to the end users computer.
Sometimes having just the executable code is not enough to allow.
The end user may not be able to run the program.
In this case, we need an installation routine.
Page 21 of 21