8.Programming a Level
8.Programming a Level
A LEVEL
TOPICS TO BE COVERED
Programming language features
Structured programming
Functions and procedures
Arrays
File handling
Interface design
Object Oriented Programming
Advanced Programming
- A programming language is a set of
symbols in computer language that are used
in coding computer programs.
- A programming language is a specially
written code used for writing application
programs e.g. C, Pascal, COBOL, BASIC, Visual
Basic, C++ and Java (Originally for intelligent
consumer-electronic devices (cell phones),
then used for creating Web pages with
dynamic content, now also used for
developing large-scale enterprise applications)
- Program: a set of detailed and
unambiguous instructions that instructs a
computer to perform a specific task, for
example, to add a set of numbers.
- Programming: A process of designing,
coding and testing computer programs
- Programmer: A person who specialises
in designing, coding and testing computer
programs
- Problem: any question or matter
TYPES OF PROGRAMMING LANGUAGES
1. Low Level Languages (LLL):
- These are programming languages used to
write programs in machine code (i.e in 1s and
0s) or in mnemonic codes.
- Low level languages are machine oriented
(machine specific).
- Low level language is in two forms:
57
Your code should look like this
To display attributes of the object
Practical Activity
4. Design a program which shows how an object is an instance of a class.
Event handlers: small program
codes which are invoked (called) in
response to external events.
Dispatcher: small program codes
that call event handlers so that
events can be processed.
Advantages of OOP
Grouping code into individual software objects
provides a number of benefits, including:
- Modularity: The source code for an object
can be written and maintained independently
of the source code for other objects. Once
created, an object can be easily passed
around inside the system.
- Information-hiding: By interacting only
with an object's methods, the details of its
internal implementation remain hidden from
- Code re-use: If an object already
exists, you can use that object in your
program.
- Easy debugging: If a particular
object turns out to be problematic,
you can simply remove it from your
application.
- Reliability: if codes are designed
by specialists, they are more likely to
- Time saving: re-use of existing
methods and states means less time
needed to code programs.
- Decreased maintenance costs:
programmers have les code to maintain.
- Smaller program codes: since the
states and methods of classes are re-
used, the code of the program is smaller,
taking less disk storage space.
- Storage structures of an object may be
Features of High Level Programming
Languages
1. Programming Constructs
Example 3
If b < 0 Then
MsgBox "b is less than zero. Command cannot be
executed", vbExclamation
Exit Sub
End If
9. Block structure
- A block is a group of zero or more
statements between balanced braces
and can be used anywhere a single
statement is allowed. For instance
if (condition) Then ( begin block 1)
………………. (end block 1)
else (begin block 2)
……………….
End If (end block 2)
10. Functions
- A function is a self-contained module that
returns a value to the part of the program which
calls it every time it is called/executed.
- A function can be just an expression that
returns a value when it is called.
- A function performs a single and special task,
e.g. generate a student number.
- Because they return a value, functions are data
types, e.g. integer, real, etc.
- Functions can be in-built functions or user-
- In-built functions are pre-defined
procedures of a programming language
that returns a value, e.g. Val (returns a
numeric value of a string), MsgBox
(creates a textbox on the screen), Abs
(returns an absolute value of a number),
etc.
- Visual Basic has in-built date functions,
string functions, conversion functions, etc.
- A user-defined function is a procedure
Public Function count_rec(ByVal rs As
Recordset) As Boolean
If rs.RecordCount <= 0 Then
MsgBox "There is no record in the table.",
vbExclamation
count_rec = True
Else
count_rec = False
End If
End Function
- Note that a Function starts with the
word Function and ends with the
statement End Function. This function
returns a Boolean value(either true or
false). The function name as just after
the word Function, i.e count-rec in the
case above.
11. Procedures
- A self-contained module that does not return
a value.
- Procedures usually starts with the key word
Procedure and then procedure name, e.g
Procedure FindTotal. Procedures are user
defined.
- The name of the procedure should be related
to its task
- Each procedure name must be unique within
the same program.
- A procedure can be called from the main
- Parameters/arguments are values that are
passed from one procedure to another and can
be the actual values or variable names. They
are therefore values given to a function by
statements from other modules.
- Parameters can be formal or actual
parameters
Actual parameters: these are arguments
found in the calling module/statement, could
be variables or actual data like 30, 40.
Formal parameters are those variables that
- When processing of the called
procedure is finished, processing goes to
the next stated after the calling
statement.
- Parameters can be passed by value or
by reference
Passing Parameters by Value
- Passing by value is whereby the actual
value (or value of a variable) is searched (e.g
20), copied and passed to another module.
- In this case, variables whose values are
passed will NOT be altered even if the values
of variables in the calling procedure or
function change.
- Thus only the values of the variables are
passed, not the variable itself.
- A copy of the value of the variable is
Passing Parameters by reference
- Passing by reference is whereby the
address of the value is passed, allowing it
to be altered if necessary.
- This is when calling procedure passes
the variable to another function, whose
processes may alter the value of the
variable in the calling procedure.
- In this case, the original variable’s
memory is addressed and can therefore
- If the programmers does not specify ByVal or
ByRef function, Visual Basic assumes that it if
12. Semantics
The meaning attached to statements and the way
they are used in a certain language.
13. Syntax
- These are grammatical rules and regulations
governing sentence construction and layout of
different programming languages.
- For example, Pascal uses a semi-colon(;) at the
end of each instruction, it also uses the reserved
(key) word writeln to display items on the screen,
etc.
- Each programming language has its own syntax.
14. Literals
- A literal is a variable which is given a fixed
value within the code of the program.
- A literal is the source code representation
of a fixed value.
- Literals are represented directly in your
code without requiring computation, as
shown:
Dim result As Integer
Dim Name As String
Result =20
15. Input operations
This Refers to statements that
prompts the user to enter data into
the computer. E.g.
ID = InputBox("Enter Member Data
to Search")
The above displays an input box
with the message in bracket that
16. Output operations
Refers to statements that allows display
or printing of results on the screen or to
the printer, e.g.
frmPayments.Show
allows, the payment form to be
displayed on the screen.
MsgBox ("Record not Found")
Displays a message box with the
17. File handling operations
Allows user to manipulate files. E.g.
in Visual basic:
rs.Close
While Not .EOF
DATA TYPES
- Data types describe the nature of data
handled by programs.
- These are important as they enhance
program readability and maintenance.
- Data types can be simple (integers, string,
etc.) or complex (arrays, lists, records, etc.).
- Data types are declared at the beginning or
at some point inside the program code.
- Data types for variables must be properly
declared.
a. Numeric data types
1. Integer
- Used to represent whole numbers, positive or negative, and
occupy 2 bytes of memory.
- Decimal values will be rounded to the nearest whole number.
- Values accepted range from -32 768 to + 32 767. Default value is
0.
2. Byte
- Stored in a single 8-bit unsigned data value ranging from 0-255.
- Used to store binary data.
- Default value is a 0.
3. Real
- A data type that is used to represent values (numbers) with
decimal point values, e.g 23.56 is a real number.
- Can be used to represent averages.
- Some languages take this to be float data type.
4. Currency
- Occupy 8 bytes with 0 as default value.
- Used for handling monetary values and support up to
4 digits to the left and 15 digits to the left of the
decimal point.
5. Date
Represent date and time values and occupy 8 bytes.
6. Double
- Stores a double precision floating point number with
decimal places but longer than single.
- Default is 0 and occupies 8 bytes
7. Single
- Stores a single precision floating point number, with
decimal places.
8. Long
- Short for long integer and default value is 0.
- It occupies 4 bytes from -2 147 483 648 to -2
147 483 647.
b. String data types
1. String
- A string is data type that stores one or more
characters, which can be alphabetic or
alphanumeric.
- If a variable is declared as a string, it must not
be used for daily life mathematical calculations
Fixed length: has specified maximum
number of characters. E.g.
Dim Sname as string * 20
- The field will be allocated 20 spaces in
the computer memory.
- If a name shorter than 20 characters is
entered, blank spaces will remain
unoccupied since they would have been
assigned to Sname already.
- If too long than allocated space, the
Variable length:
Does not specify the number of
characters, each data items
occupies the number of spaces it
requires.
This is declared as follows
Dim Sname as string
2. Boolean
- Represents instructions that evaluate to
either True or False only. The default value is
False and occupy 2 bytes of storage.
- Mostly used together with If….. Then…….
Else Construct.
3. Character
- A data type that holds just one alphabetic or
special character like %, $, a, etc.
- can be written as char in most programming
languages.
User-defined data.
These are defined by the user depending
on the method of solution, e.g classes
when the used define own classes.
Enumerated data:
These are data types with a list of items
that are pre-defined, e.g days of the
week, months of the year, etc.
Word: a computer word is group of bits
that can be handled or transferred by the