0% found this document useful (0 votes)
12 views107 pages

8.Programming a Level

The document covers various programming topics including programming language features, structured programming, and object-oriented programming. It explains the differences between low-level and high-level programming languages, their advantages and disadvantages, and introduces programming paradigms such as imperative, declarative, general-purpose, and special-purpose languages. Additionally, it details concepts related to object-oriented programming, including classes, objects, encapsulation, inheritance, and polymorphism.

Uploaded by

madzingiraff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views107 pages

8.Programming a Level

The document covers various programming topics including programming language features, structured programming, and object-oriented programming. It explains the differences between low-level and high-level programming languages, their advantages and disadvantages, and introduces programming paradigms such as imperative, declarative, general-purpose, and special-purpose languages. Additionally, it details concepts related to object-oriented programming, including classes, objects, encapsulation, inheritance, and polymorphism.

Uploaded by

madzingiraff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 107

PROGRAMMING

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:

(a) Machine Language and


(b) Assembly Language.
a. Machine code (language)
- Is the language used to write programs in
binary form (1s and 0s).
- Machine code executes without
translation.
- Machine language has the following
advantages:
 Programs run faster since they are
already in computer language. There is no
need for conversion as programs are in
Disadvantages of Machine language:
- They are very difficult to learn.
- They are difficult to understand.
- Very difficult to use and therefore very few
programmers use them these days.
- It takes too long to debug and therefore is
prone to some errors.
- It takes too long to develop working
programs.
- They are machine dependent (they can
only work on type of computer designed for
b. Assembly Language:
- These are programming languages that
use mnemonic codes in coding programs.
- Mnemonic codes are abbreviations used
to represent instructions when coding
assembly language programs, for
example, LDA for Load, ADD for Addition,
etc.
- One assembly language statement is
equivalent to one machine code
- Programs also run faster as they are
closer to machine language and
therefore are used in designing
programs that needs efficient timing,
e.g. games like chess, operating
systems, etc.
- Assembly language is used when
there is need to access registers and
memory addresses directly.
Mnemonic codes are very close to
machine code, hence are low level
language assembly language codes.
- They however run on specific
computer architecture since they are
hardware aligned.
- They also contain different forms of
instruction, e.g. jump, control,
arithmetic, etc.
Application: Assembly language is
used in:
- Coding operating systems
- Coding device drivers
- Coding programs for embedded
systems like DVD players, decoders,
etc.
- Coding encryption software
Advantages of Assembly language:
- One assembly language instruction corresponds to one
machine code instruction and therefore translation is
easier and faster to code.
- Programs run faster since they are close to machine
code.
- They occupy very small disk storage space hence are
economical to use.
- Easier for a programmer to use than machine language.
Disadvantages of Assembly Language
- They are very difficult to learn.
- They are very difficult to understand.
- Takes too long to develop working programs.
- They can be machine dependent (machine oriented)
2. High Level Languages (HLL):
- These are programming languages that use
English-like statements in coding programs, for
example COBOL, Pascal, BASIC, etc.
- High Level languages are mostly used for
developing user applications like stock control
systems, personnel records, etc.
- There are so many high level languages because
of competition from designers who want to
outpace each other.
- High Level languages are independent of the
architecture of the computer.
- Programs written in High Level Language are
first converted to machine code before running.
High level languages have the following
features:
- Problem oriented (Machine independent): they
are designed to solve an application problem
and therefore runs on any machine
- They are portable: they can be transferred
from one machine to another and run without
problem.
- Instructions are written in English statements
Facilities of High Level Languages
The following facilities of high level languages
are not found in low level languages
- Selection structures
- Iteration structures
- Built-in routines to simplify input and output,
e.g. INPUT, PRINT, PRINTLN, etc in BASIC.
- Built-in functions like sqr, sqrt, val, etc.
- Data structures like strings, arrays, records,
etc.
- User-defined functions.
Advantages of High Level
Languages:
- They are easier to understand since
they are written in English-like
statements which are more readable.
- They are easier to learn.
- It is easier to work with, that is to
correct errors (debug) and to test
programs.
Disadvantages of HLL
- Takes long to run since they need
to be first converted to machine
code.
- They occupy a lot of disk storage
space as compared to low level
languages.
Types of High Level
Languages/Programming Paradigms
- Programming paradigm refers to methods
used to categorise high level languages in terms
of organising principles used by the designers. It
is pattern that serves as a school of thought for
programming of computers.
- Such programming paradigms are as follows:
 Imperative/procedural languages
 Declarative languages
 General purpose languages
 Special purpose languages
1. Imperative /Procedural Programming
languages
- These are high level programs in which the
programmer specifies how the program is to be
executed, following a predefined execution
sequence.
- The programmer has to write instructions (in
source code) that are to be strictly followed when
performing a certain task.
- The programmer shows steps in the order of
execution, providing a set of data.
Imperative languages have the following
features:
 built-in data types like integers,
character, string, Boolean, etc
 user defined data types like records,
arrays, etc
 Declarations for variables, arrays,
constants, functions, procedures, etc.
 Programming statements like
assignments, control structures,
2. Declarative Programming
Languages
- These are programming languages that
states what is to be done rather than
how to do it.
- The programmer states the facts and
rules associated with the problem.
- The order of stating the rules and facts
is not important.
- Adding new rules, modifying existing
They do not use loops, selection structures or
procedures.
- Can also be called Very High Level
programming languages or 4GL languages.
- They have the following benefits:
- Makes it easier to program computer
applications due to the following reasons:
 Allow alternate methods of developing
software, e.g. prototyping.
 One 4GL code is equivalent to several lines of
3GL languages code.
Application: mostly used in
designing expert systems.
Examples of declarative languages
are Prolog (Programming in Logic)
3. General purpose
languages
These are languages that can
be used to perform a variety of
takes, they are not dedicated
to one particular task.
4. Special purpose languages
- These are languages that are dedicated to a
specific area, for instance, for running on
embedded computer systems like washing
machines, mobile phones, controlling robots in a
car assembly, etc.
- such languages must support real-time facilities,
and have the following facilities:
 Real-time control facilities: Programmer
specifies times/frequency at which certain actions
are to be performed, e.g. taking readings after
every 3 seconds for sensors. Programmers must
 Interaction with hardware
interfaces: language must
contain instructions to monitor
devices like sensors and control
actuators.
 Ability to support concurrent
programming: languages must
allow two or more actions to take
5. Object Oriented Programming
languages (OOP)
- These are programming languages that use
classes and objects, allowing fatures like
encapsulation, inheritance and
polymorphism.
- Objects are data items with all the
processing possible on the data item
attached to it. (have states and methods)
- OOP languages include Visual Basic, C++,
Java, Object Pascal, Common Lisp, Scheme,
- OOP languages support the
following features:
 Encapsulation
 Inheritance and
 Polymorphism
Identifying the state and behaviour
for real-world objects is a great
way to begin thinking in terms of
What Is an Object?
Objects
- are real world items that have
state (attributes) and behaviour
(operations) and belong to certain
class, e.g. desk, car, television set,
bicycle, etc.
- In general, an object is an instance
State:
- These are
attributes/characteristics/fields for
an object, e.g. a car has colour,
registration number, model, etc.
- Bicycles have state of current gear,
current speed, etc.
- Each object stores its own state.
- Dogs have state (name, colour,
Behaviour (operations):
- Refers to methods that can be
used on each object state, e.g.
changing gear of a car, applying
brakes, etc.
- Each class has its source code that
is associated with it and defines the
fields and methods.
- Dogs have behaviour (barking,
Class:
- A set of objects which share a
common data structure and a
common behaviour.
- In coding a program, a class is taken
as an abstract data type that
describes the fields and methods of
the class.
- Each class has different access
Encapsulation
- This is a technique of combining operations
(methods) and data (fields) into one unit as in
classes.
- Encapsulation can be in two forms:
(a) Data encapsulation: Hiding states
internally and requiring all interaction to be
performed through an object's methods. It
involves restricting access to the states
(fields).
(b) Procedural encapsulation: users do not
Inheritance
- In Object-oriented programming, inheritance is
whereby classes can re-use (assume) commonly
used state and behaviour from their parent (base)
class.
- Inheritance is the ability of a class to use the
variables and methods of a class from which the
new class is derived (parent class)
- Inheritance allows a new class to be derived from
an existing class.
- Inheritance therefore is a relationship among
classes, where a sub-class (derived class) shares all
- Base Class: This is the parent
class or the first class to be created
from which other class can inherit
states and methods.
- Derived class: These are new
classes that are created from the
base class, and therefore have
methods and states of the base
Polymorphism
- In general, polymorphism is the ability to have the
same operation performing differently in different
circumstances.

- Polymorphism allows an operation to perform


differently depending on the parameters that are
passed.
- This is the ability of classes to use the same name
in the class hierarchy for a method but each class
implementing the method differently.
- In polymorphism, derived classes are able to re-
define some of the base (super class) methods.
Containment/aggregation/composition
- These are links/associations between objects
that allow them to communicate.
- For instance: a form on a screen is an object.
On the object form, there are other objects, e.g.
delete button, display button, exit button, etc.
- These buttons (which are objects)
communicate with the form (another object).
- Thus the linkage between the form and the
buttons is called the containment.
Event driven programming
- This is whereby the sequence of code
execution is determined (triggered) by
external events or by user actions, e.g.
clicking a button on a form is an event that
can form a program to execute certain
sections of the code.
- Thus clicking a button or menu item is an
event.
- This is important because the programmer
WHAT IS A CLASS?
Example

42 Objects and Classes


Example explanation
 Theidea is that the ‘template’ of a CAR does
not change. Each Object was built from the
same set of template (Class) and therefore
contains the same components. All Objects
share the same copy of the member functions
(methods), but maintain a separate copy of the
member data (Properties)
 Forexample: a Ford car and a Toyota car are
both Cars, so they can be classified as
belonging to the Car class. All have same
43 Objects and Classes
What is an Object?
Properties
Properties describe objects. A color is a
property. Height, weight, size are all properties.
In , this simple formula is functionally the same.
You can have various properties set up in your ,
but provide different settings for each different
object.

45 Objects and Classes


Methods

Methods determine how an object


behaves. Some cars are lower on fuel
consumption than others, but still have
the same capabilities as other cars. You
will have to write the method for the
object; but how that method is ultimately
implemented may be different from
another object.
46 Objects and Classes
Object Variables

Object type variables are used for working with objects.


The scope of an object variable is given with Dim,
Private, Public, Static, etc. Thus we can have local,
form-level, or public (global) object variables.
 Public: The word "Public" means that other code can
see it outside the class.
 Private: The Private keyword in the declaration
statement specifies that the elements can be
accessed only from within the same module, class, or
structure.
47
i.e. cannot be accessed outside the class
Objects and Classes
Controlling Objects with their Properties

The individual properties of the objects can vary. Some


properties can be set at design time. We can use the
Properties window to set the value of these properties without
writing any code.
Some properties are not available at design time; these
properties are manipulated at run time using the code.
 Properties
that we can set and those that we manipulate at run
time are called read-write properties
e.g. text properties of a text box.
 Properties
that we can only read at run time are called read-
only properties
48 e.g. name property of a control. Objects and Classes
Setting properties values:
The value of a property should be set when it is required
to change the appearance or behaviour of the object value of a
property.
For Eg: The Text property of a text box can be changed
in order to change the contents of the text box.
The following syntax is used to set the value of a property.
object.property = expression
Getting properties values:
We get the value of a property when we want to find the
state of an object before our code performs additional
actions(such as assigning the value to another object)
For Eg: To get the value of a property, we use the
following
49 syntax, Objects and Classes
Performing actions with methods:
Methods can affect the value of properties. In
VB.Net, list boxes have a List property, which can be
changed using the Clear and AddItem methods.
Using methods in code:
When we use a method in code, the manner in which we write the
statement depends on the number of arguments the method requires and
whether the method returns a value. When a method does not take
arguments, we write the code using the following syntax :
object.method
In this example, the Refresh method repaints the picture box
Picture1.Refresh 'Forces a repaint of the control.
Some methods, such as Refresh method, do not have arguments and do
not return
50
values. Objects and Classes
Working with Objects

VB.Net supports properties, methods and events.



In VB.Net, the setting of attributes of an
object are referred to as properties
 The various procedures that can operate
on the object are called its methods.
 Events are triggered when some aspect
of the object are changed.

51 Objects and Classes


ACCESS LEVEL
Object Variables

The scope of an object variable is given with Dim,


Private, Public, Static, etc.
 Public: The word "Public" means that other code can
see it outside the class.

 Private: The Private keyword in the declaration


statement specifies that the elements can be accessed
only from within the same module, class, or structure.
i.e. cannot be accessed outside the class

53 Objects and Classes


Creating a Class

 Open new project


 Go to Project menu, click Add Class
 In the Name box, type the name of your new
class as person and then click Add.
 The Code Editor provides programming
statements that mark the beginning and
ending statements for your class, as follows:
Public Class Name is now person
End Class
Creating a Class ‘contd’

Add the following to the


Class ie declaration of
variables /attributes
 Public firstname As String
 Public race As String
 Public weight As String
 Public height As String
Your Class should look like this
Creating an Object from a Class
(New Operator)
1. Add a button to your form in design view
2. Double click the button on your Form
3. Add the following code to it:
 Dim charles As New person
 charles.firstname = "charles"
 charles.height = "150 cm"
 charles.race = "african"
 charles.weight = "70kg"

57
Your code should look like this
To display attributes of the object

Type this code below what has been typed


above
 MsgBox("FullName is :"&
charles.firstname)
 MsgBox("Height is :" & charles.height)
 MsgBox("Race is :" & charles.race)
 MsgBox("Weight is :" & charles.weight)
Your code window should look
like this
Running your code

 Check for errors


 Save your project
 Run your code and test it out.

Click Button and you should


see the object of a class.
Review Questions
1. What is a class?
2. Highlight the steps to create a class
3. What is an 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

These are the basis from which high level languages


are built. Programming constructs includes:
(a) Control Structures
(i) If – Then – Else Construct
(ii) Case statement
(b) Looping Structures
(i) For – Next construct
(ii) Repeat – Until construct
(iii) While – Endwhile
Operators
Operators are used to manipulate data and
they can be
(a) Arithmetic operators
e.g. b+c-d*e/f
(b) Logical operators
And, or, Not
(c) Assignment operators
=
(d) Comparison/relational operators
>, <, <=, >=, <>, Is
3. Identifiers
- An identifier is a unique label of a data
item of element of a program and
comprises of one or more characters.
- Identifiers includes variable names,
procedure names, constants, etc.
Identifiers must not be reserved words.
- Identifiers can be user defined as long
as they are not reserved words.
Constants
- A constant is a data item
(variable) whose value does not
change during program execution.
- Its value is fixed.
- Constants are used to represent
data items with fixed values, e.g.
the value of pi. In VB
5. Variables
- A variable is a name given to a memory
location that stores a certain value, which may
change (the value) during program execution.
- Variables can be field identifiers, e.g. surname
is a valid variable name.
- Variables must not be reserved words.
- Variables must be unique in the procedure or
program (if all are global).
- Variables are declared at the beginning or at
some point inside the program code. Every
variable must be declared before use, otherwise
- Variable names, as are all identifiers, start with
an alphabetic character.
- They can be one character or a string of characters.
- Variable names can be alphanumeric (combination
of alphabetic and numbers).
- They must be one word and must be related to the
data stored in them so that the programmer cannot
be confused, e.g. Surname should be variable that
stores a surname.
- If two words are used as a variable, they must be
joined by and underscore ( _ ), with no spaces
between the words, e.g. Student_Surname, NOT
- Alternatively, one may join the words as
follows, StudentSurname.
- Variable names should not be too long, 8
characters are ok although VB supports
longer variables names but must not be
more than 255 characters.
- Variables can store numeric, character
or string values and must be declared
appropriately.
- In Visual Basic 6.0, variables must be
The keyword Dim is used to declare
variables, and each variable should have a
data type, e.g.
Syntax: Dim variablename AS Datatype
Dim number As Integer
Dim Name1 As String
Dim x, Number_1, AmountOwed As Integer
- The scope of variables can be
global or local.
- Scope of a variable refers to the
extent (within the program) that the
variable is recognised (used),
especially in relation to other modules
and functions within the program.
- Constants can also be declared as
global or local.
In Microsoft Visual Basic variables have
the following features:
 Must start with an alphabetic letter as the first
character.
 Must be one word, no spaces allowed
 Must not contain a period (.), exclamation
mark (!), or the characters @, &, $, # in the
name.
 can't exceed 255 characters in length.
 Must not be reserved words
 Must be unique. You can't repeat the same
a. Global variables
- These are variables that are accessed and
can be used by any procedure or function
within the same program.
- They are public variables
- The value of the variable exists
throughout the program.
- Global variables are declared outside the
procedure.
- In VB, global variables are declared as
b. Local variables
- These are variables that are defined within a procedure and that
are accessible just within the procedure they are declared.
- They are defined within the procedure.
- They are private variables
- The value of the variable only exists within the
procedure it is defined.
- They are therefore local to that procedure in which
they are declared and therefore cannot be used
(not accessible) by other procedures.
- Local variables are declared as follows:
Private Sname As String
Form Variables
7. Expressions
- An expression is a construct made up of variables and operators that makes up a
complete unit of execution.
Example:
NumberA = a+b-c*d
- The above is a statement. However, a+b-c*d is an expression found in a statement.
- Expressions can be arithmetic (as given above), Boolean or string expressions. For
example,
(a>b) and (a>=c)
- Operator precedence is very important in evaluating expressions and therefore it is
important to enclose expressions is brackets where possible. Operator precedence is
as follows, starting from the highest to the lowest:
( ), Not, ^, {*,/,}\,Mod, {+, -,}{=,<,>,<=, >= },…
NB: Operators is set braces indicates that they are in the same level.
- Arithmetic expressions are evaluated first, followed by comparisons and lastly
logical expressions.
8. Statements
- A statement is a single instruction in a program which
can be converted into machine code and executed.
- A statement can just be one line of program code but
in some cases a statement may have more than one
line.
- For example: Name = “Marian” is a statement.
Example 1
NumberA = a + b
- this is an assignment statement, that is, variable
NumberA is assigned the sum of the values of variables
a and b. thus if a=2 and b = 3, NumberA is assigned
the value 5.
Example 2
If a>b Then MsgBox "a is bigger than b.",
vbExclamation
- The above is a one line statement composed of if
statement.

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

You might also like