CSC 102 Lecture Slides (1) Computer Science
CSC 102 Lecture Slides (1) Computer Science
A computer is basically defined as a tool or machine used for processing data to give
required information.
It is capable of:
a. taking input data through the keyboard (input unit)
b. storing the input data in a diskette, hard disk or other medium
c. processing it at the central processing unit (CPU) and
d. giving out the result (output) on the screen or the Visual Display Unit (VDU).
Data: The term data is referred to -----------facts about a person, object or place e.g. name, age,
complexion, school, class, height etc.
Information: Is referred to as ---------------------------------processed data or a meaningful statement e.g. Net pay of
workers, examination results of students, list of successful candidates in an examination
or interview etc.
Computer is fast becoming the universal machine of the 21st century. Early
computers were large in size and too expensive to be owned by individuals.
Thus they were confined to the laboratories and few research institutes. They
could only be programmed by computer engineers. The basic applications were
confined to undertaking complex calculations in science and engineering.
Today, computer is no longer confined the laboratory.
Computers and indeed, computing have found
become
embedded in almost every
every where
goes beyond
item we use. Computing is fast becoming (ubiquitous.) Its application transcends
science, engineering, communication, space science, aviation, financial
institutions, social sciences, humanities, the military, transportation,
manufacturing, extractive industries to mention but a few. This unit presents
the background information about computers.
Characteristics of Computer
Speed: The computer can manipulate large data at incredible speed and response time
can be very fast.
Accuracy: Its accuracy is very high and its consistency can be relied upon. Errors
committed in computing are mostly due to human rather than technological weakness.
There are in-built error detecting schemes in the computer.
Storage: It has both internal and external storage facilities for holding data and
instructions. This capacity varies from one machine to the other. Memories are built up
in K(Kilo) modules where K = 1024 memory locations.
Automatic: Once a program is in the computers memory, it can run automatically each
time it is opened. The individual has little or no instruction to give again.
Reliability: Being a machine, a computer does not suffer human traits of tiredness and
lack of concentration. It will perform the last job with the same speed and accuracy as
the first job every time even if ten million jobs are involved.
Flexibility: It can perform any type of task once it can be reduced to logical steps.
Modern computers can be used to perform a variety of functions like on-line processing,
multi-programming, real time processing etc.
The second generation saw several important developments at all levels of computer
system design, from the technology used to build the basic circuits to the
programming languages used to write scientific applications.
transistorized airborne digital computer
Electronic switches in this era were based on discrete diode and transistor
technology with a switching time of approximately 0.3 microseconds. The first
machines to be built with this technology include TRADIC at Bell Laboratories in 1954
and TX-0 at MITs Lincoln Laboratory. Memory technology was based on magnetic
cores which could be accessed in random order, as opposed to mercury delay lines, in
which data was stored as an acoustic wave that passed sequentially through the
medium and could be accessed only when the data moved by the I/O interface.
During this second generation many high level programming languages were
introduced, including FORTRAN (1956), ALGOL (1958), and COBOL (1959). Important
commercial machines of this era include the IBM 704 and 7094. Theylatter
introduced I/O processors for better throughput between I/O devices and main
memory.
The Intel iPSC -1, nicknamed the hypercube, took a different approach.
Instead of using one memory module, Intel connected each processor to its
own memory and used a network interface to connect processors. This
distributed memory architecture meant memory was no longer a bottleneck
and large systems (using more processors) could be built. The largest iPSC-1
had 128 processors. Toward the end of this period, a third type of parallel
processor was introduced to the market. In this style of machine, known as a
data-parallel or SIMD, there are several thousand very simple processors. All
processors work under the direction of a single control unit; i.e. if the control
unit says add a to b then all processors find their local copy of a and add it
to their local copy of b. Machines in this class include the Connection Machine
from Thinking Machines, Inc., and the MP-1 from MasPar, Inc.
Scientific computing in this period was still dominated by vector processing. Most
manufacturers of vector processors introduced parallel models, but there were
very few (two to eight) processors in these parallel machines. In the area of
computer networking, both wide area network (WAN) and local area network
(LAN) technology developed at a rapid pace, stimulating a transition from the
traditional mainframe computing environment towards a distributed computing
environment in which each user has their own workstation for relatively simple
tasks (editing and compiling programs, reading mail) but sharing large, expensive
resources such as file servers and supercomputers. RISC technology (a style of
internal organization of the CPU) and plummeting costs for RAM brought
tremendous gains in computational power of relatively low cost workstations and
servers. This period also saw a marked increase in both the quality and quantity of
scientific visualization.
One of the most dramatic changes in the sixth generation is the explosive
growth of -------------------------------------wide area networking. Network bandwidth has expanded
tremendously in the last few years and will continue to improve for the next
several years. T1 transmission rates are now standard for regional networks,
and the national backbone that interconnects regional networks uses T3.
networking technology is becoming more widespread than its original strong
base in universities and government laboratories as it is rapidly finding
application in K-12 education, community networks and private industry. A
little over a decade after the warning voiced in the Lax report, the future of a
strong computational science infrastructure is bright.
1. Signal Types
2. Purpose
3. Capacity
Signal Types
Digital Computer
Represent its variable in the form of digits. It counts the data it deals with,
whether representing numbers, letters or other symbols, are converted into
binary form on input to the computer. The data undergoes a processing after
which the binary digits are converted back to alpha numeric form for output for
human use. Because of the fact that business applications like inventory control,
invoicing and payroll deal with discrete values, they are beset processed with
digital computers. As a result of this, digital computers are mostly used in
commercial and business places today.
Analog Computer
It measures rather than counts. This type of computer sets up a model of a
system. Common type represents it variables in terms of electrical voltage and
sets up circuit analog to the equation connecting the variables. The answer can
be either by using a voltmeter to read the value of the variable required, or by
feeding the voltage into a plotting device. They hold data in the form of physical
variables rather than numerical quantities.
Signal Types
what is a digitizer?-- it is a conversion element that accepts analog inputs and output digital value
Hybrid Computer
In some cases, the user may wish to obtain the output from an analog computer
as processed by a digital computer or vice versa. To achieve this, he set up a
hybrid machine where the two are connected and the analog computer may be
regarded as a peripheral of the digital computer. In such a situation, a hybrid
system attempts to gain the advantage of both the digital and the analog
elements in the same machine. This kind of machine is usually a special-purpose
device which is built for a specific task. It needs a conversion element which
accepts analog inputs, and output digital value. Such converters are called
digitizers. There is need for a converter from analog to digital also. It has the
advantage of giving real-time response on a continuous basis. Complex
calculations can be dealt with by the digital elements, thereby requiring a large
memory, and giving accurate results after programming. They are mainly used in
aerospace and process control applications.
Classification By Purpose
Special Purpose Computers
A special purpose computer is one that is designed to solve a restricted class of
problems. Such computers may even be designed and built to handle only one
job. In such machines, the steps or operations that the computer follows may be
built into the hardware. Most of the computers used for military purposes fall
into this class. Other example of special purpose computers include:
Computers designed specifically to solve navigational problems.
Computers designed for tracking airplane or missiles.
Computers used for process control applications in industries such as oil refinery,
chemical manufacture, steel processing and power generation.
General Attributes of Special Purpose Computers
Special purpose computer are usually very efficient for the tasks for which they
are specially designed.
They are very much less complex than the General-Purpose Computers. The
simplicity of the circuiting stems from the fact that provision is made only for
limited facilities.
They are very much cheaper than the General-Purpose type since they involve less
components and are less complex.
Classification By Purpose
General-Purpose Computers
General-Purpose computers are computers designed to handle wide range of
problems. Theoretically, a general-purpose computer can be adequate by means
of some easily alterable instructions to handle any problems that can be solved by
computation. In practice however, there are limitations imposed by memory size,
speed and the type of input/output devices. Examples of areas where the general
purpose are employed include the following:
Payroll, Banking, Billing, Sales analysis, Cost accounting ,Manufacturing scheduling
Inventory control
General Attributes of General-Purpose Computers
General-Purpose computers are more flexible than special purpose computers.
They can handle a wide spectrum of problems.
They are less efficient than the special-purpose computers due to such problems
as;
Inadequate storage;
Low operating speed;
Coordination of the various tasks and subsection may take time.
General Purpose Computers are more complex than the special purpose ones.
Classification by Capacity
The volume of work that a given computer handles is closely tied to the cost and
to the memory size of computer. Therefore, most authorities today accept the
price of rental price as the standard for ranking computers.
Here, both memory size and cost shall be used to rank (classify) computer into
three main categories as follows:
(i)Microcomputers
(ii)Medium/Mini/Small Computers
(iii)Large Computer/Main Frames.
(i)Micro Computers
Microcomputers, also known as single board computers, are the cheapest class of
computers. In the microcomputer, we do not have a Central Processing Unit (CPU)
as we have in the larger computers rather we have a microprocessor chip as the
main data processing unit. They are the cheapest smallest and can operate under
normal office condition. Examples are IBM, APPLE, COMPAQ, Hewlett Packard
(HP), Dell Toshiba.
(iii) Mainframe
The Main Frame Computers often called number crunches have memory capacity
of the order of 4 Kbytes and they are very expensive. They can execute up to
100MIPS (Meanwhile Instructions Per Second). They have large systems and are
used by many people for a variety of purpose.
The computer hardware comprises the --------------------------input unit, the --------------------------------------processing unit and the
output unit.
-------------------------------The input unit comprises those media through which data is fed into the computer.
Examples include the ----------------------keyboard, mouse,
joystick, trackball,
scanner etc.
---------------- --------------------------------------- --------------------The processing unit is made up of the --------------------------------------------------------------Arithmetic and Logic Unit (ALU), the
control
----------------------------unit and the main
memory. The main memory also known as the primary memory
---------------------------------is made up of the -------------------------------------------------Read Only Memory (ROM) and the Random
Access Memory
-------------------------------------------------------------(RAM).
The output unit is made up of those media through which data, instructions for
processing the data (program), and the result of the processing operation are
displayed for the user to see. Examples of output unit are the monitor (Visual
Display Unit) and the printer.
The speed of a CPU is measured in megahertz (MHz). A computer has central clock
that keeps all the components in time with each other; one hertz is similar to a clock
tick and megahertz is equal to one million ticks per second. If your PC runs at 333 or
400MHz, the central clock ticks 333 or 400 million times every second. As you might
imagine, the faster the clock ticks, the faster the computer runs. Without the CPU,
there would be no PC. Like all other hardware components, the CPUs are continually
undergoing further development. You can see the explosive technological
development in data processing most clearly in the development of newer and faster
CPUs. The CPUs have for years doubled their performance about every 18 months
and there are no indications that this trend will stop.
The physical components of the computer are called the hardware while all the other
resources or parts of the computer that are not hardware, are referred to as the
Software. Software are the set of programs that makes the computer system active.
In essence, the software are the programs that run on the computer.
what is a program? A Program is a series of coded instructions showing the logical
steps the computer follows to solve a given problem.
Classification of Computer Software
The computer software could be divided into two major groups namely System
Software (Programs) and Application Software (Programs).
System Software
This is refers to the suits of programs that facilitates the optimal use of the hardware
systems and/or provide a suitable environment for the writing, editing, debugging,
testing and running of User Programs. Usually, every computer system comes with
collection of these suits of programs, which are provided by the Hardware
Manufacturer.
Computer System Software can be divided into three groups which are;
Operating System
Language Translator
Utility Software
OPERATING SYSTEMS
An operating system is a program that acts as an interface between a user of a
computer and the computer hardware. The purpose of an operating system is to
provide an environment in which a user may execute programs.
Systems programs are programs written for direct execution on computer hardware
in order to make the power of the computer fully and efficiently accessible to
applications programmers and other computer users. Systems programming is
different from application programming because the requires an intimate
knowledge of the computer hardware as well as the end users needs. Moreover,
systems programs are often large and more complex than application programs,
although that is not always the case. Since systems programs provide the
foundation upon which application programs are built, it is most important that
systems programs are reliable, efficient and correct.
OPERATING SYSTEMS
In a computer system the hardware provides the basic computing resources. The
applications programs define the way in which these resources are used to solve the
computing problems of the users. The operating system controls and coordinates
the use of the hardware among the various systems programs and application
programs for the various users
The basic resources of a computer system are provided by its hardware, software
and data. The operating system provides the means for the proper use of these
resources in the operation of the computer system. It simply provides an
environment within which other programs can do useful work.
We can view an operating system as a resource allocator. A computer system has
many resources ( hardware and software) that may be required to solve a problem:
CPU time, memory space, file storage space, input/output devices etc.
The operating system acts as the manager of these resources and allocates them to
specific programs and users as necessary for their tasks. Since there may be many,
possibly conflicting, requests for resources, the operating system must decide which
requests are allocated resources to operate the computer system fairly and
efficiently. An operating system is a control program.
OPERATING SYSTEMS
This program controls the execution of user programs to prevent errors and
improper use of the computer.
Operating systems exist because they are a reasonable way to solve the problem of
creating a usable computing system. The fundamental goal of a computer system is
to execute user programs and solve user problems.
The primary goal of an operating system is a convenience for the user. Operating
systems exit because they are supposed to make it easier to compute with an
operating system than without an operating system. This is particularly clear when
you look at operating system for small personal computers.
A secondary goal is the efficient operation of an computer system. This goal is
particularly important for large, shared multi-user systems. Operating systems can
solve this goal. It is known that sometimes these two goals, convenience and
efficiency, are contradictory.
OPERATING SYSTEMS
A computers operating system ( OS ) is a group of programs designed to serve two basic
purposes:
To control the allocation and use of the computing systems resources among the
various users and tasks, and.
To provide an interface between the computer hardware and the programmer that
simplifies and makes feasible the creation, coding, debugging, and maintenance of
application programs.
Specifically, we can imagine that an effective operating system should accomplish all of the
following:
Facilitate creation and modification of program and data files through an editor
program,
Provide access to compilers to translate programs from high-level languages to
machine language,
Provide a loader program to move the complied program code to the computers
memory for execution,
Provide routines that handle the intricate details of I/O programming,
Assure that when there are several active processes in the computer, each will get fair
and non interfering access to the central processing unit for execution,
Take care of storage and device allocation,
Protection System
Networking
Command Interpreter System
Language Translator what is a language Translator?----it is the suite of language that translates other languages to machine language
A programming language is a set of notations in which were express our instructions to the
computer. At the initial stage of computer development, programs were written in
machine language conducting the binary system i.e. 0 and 1. Such programs were hard to
write, read, debug and maintain. In an attempt to solve these problems, other computer
languages were developed. However, computers can run programs written only in
machine language. There is therefore the need to translate programs written in these
other languages to machine language. The suites of languages that translate other
languages to machine language are called Language Translator. The initial program
written in a language different from machine language is called the source program and
its equivalent in machine language is called object program.
Source program
Language
Translator
Machine
Language
(object program)
Language Translator
Three examples of classes of language translators are Assemblers, Interpreters and
Compilers.
Assemblers: An Assembler is a computer program that accepts a source program in
assembly language program reads and translates the entire program into an equivalent
program in machine language called the object program or object code. Each machine has
its own assembly language, meaning that the assembly language of one machine cannot
run on another machine.
Interpreter: An Interpreter is a program that accepts program fin a source language,
reads, translates and executes it, line by lone into machine language.
Compilers: A Compiler is a computer program that accepts a source program in one highlevel language, reads and translates the entire users program into an equivalent program
in machine language, called the object program or object code.
The stages in compilation include:
Lexical analysis
Syntax analysis
Semantic analysis
Code generation
Utility Software
This is a set of commonly used programs in data processing departments also called
service or general-purpose programs.
They perform the following operations.
File Conversion: This covers data transfer from any medium to another, making an exact
copy or simultaneously editing and validating. For example, copying from a hard disk to a
diskette.
File Copy: It makes an exact copy of a file from one medium to another or from an area of
a medium to another area of the same medium.
Housekeeping Operations: These include programs to clear areas of storage, writing file
labels and updating common data.
They are not involved in solving the problem at hand. They are operations that must be
performed before and after actual processing.
Application software
Application software is a set of programs designed to solve problems of a specific nature.
It could either be supplied by the computer manufacturer or in some cases, the users
produce their own application program called USER PROGRAMS. Hence, an application
software could be subdivided into two classes, namely; Generalized and User-defined
Software.
Under the Generalized software, we have as examples: Word Processing Programs e.g.
Word Perfect, Word Star, Microsoft word.
PageMaker, CorelDraw likewise the Spreadsheet program e.g. LOTUS 1,2,3, Excel, Super-Q
while under the User-defined, we could have some User-defined packages for a particular
company or organization, for accounting, payroll or some other specialized purposes.
Example A = B+ 2
The operation codes are = and +
The operands are A, B and 2
A and B are storage devices while 2 is the
actual data.
input-output instructions;
arithmetic instructions;
branching instructions;
logic instructions.
An input instruction directs the computer to accept data from a specific input device and
store it in a specific location in the store.
An output instruction tells the computer to move a piece of data from a computer
storage location and record it on the output medium.
All of the basic arithmetic operations can be performed by the computer. Since
arithmetic operations involve at least two numbers, an arithmetic operation must
include at least two operands.
Machine Language
The earliest forms of computer programming were carried out by using languages that
were structured according to the computer stored data, that is, in a binary number
system. Programmers had to construct programs that used instructions written in binary
notation 1 and 0. Writing programs in this fashion is tedious, time-consuming and
susceptible to errors.
A machine language program allows the programmer to take advantage of all the
features and capabilities of the computer system for which it was designed. It is also
capable of producing the most efficient program as far as storage requirements and
operating speeds are concerned. Few programmers today write applications programs in
machine language. A machine language is computer dependent. Thus, an IBM machine
language will not run on NCR machine, DEC machine or ICL machine. A machine
language is the First Generation (computer) Language (IGL).
Stages of Programming
The preparation of a computer program involves a set of procedure.
These steps can be classified into eight major stages, viz
Problem Definition
Devising the method of solution
Developing the method using suitable aids, e.g. pseudo code or flowchart.
Writing the instructions in a programming language
Transcribing the instructions into machine sensible form
Debugging the program
Testing the program
Documenting all the work involved in producing the program.
Problem definition
The first stage requires a good understand of the problem. The programmer (i.e.
the person writing the program) needs to thoroughly understand what is
required of a problem. A complete and precise unambiguous statement of the
problem to be solved must be stated. This will entail the detailed specification
which lays down the input, processes and output-required.
Devising the method of solution
The second stage involved is spelling out the detailed algorithm. The use of a
computer to solve problems (be it scientific or business data processing
problems) requires that a procedure or an algorithm be developed for the
computer to follow in solving the problem.
Developing the method of solution
There are several methods for representing or developing methods used in
solving a problem. Examples of such methods are: algorithms, flowcharts,
pseudo code, and decision tables.
Program testing
The purpose of testing is to determine whether a program consistently produces
correct or expected results. A program is normally tested by executing it with a
given set of input data (called test data), for which correct results are known.
Program documentation: Documentation of the program should be developed
at every stage of the programming cycle
The following are documentations that should be done for each program:
Problem Definition Step
Planning the Solution Step
Program source coding sheet
Users manual to aid persons who are not familiar with the program.
Operators manual to assist the computer operator.
Highlight and discuss into details the content of a good program documentation
and its advantages.
Flowcharts
A Flowchart is a graphical representation of the major steps of work in process. It
displays in separate boxes the essential steps of the program and shows by
means of arrows the directions of information flow. The boxes most often
Flowcharts Symbols
Flowcharts are drawn with the help of symbols. The following are the most
commonly used flowchart symbols and their functions:
Used to show the START or STOP point.
Used for arithmetic calculations of process.
Used for Input and Output instructions
Used for decision making
Flowcharts Symbols
Used for entry to or exit from another part of flowchart. A small
circle identifies a junction point of the program
Algorithms
Definition: An algorithm, named after the ninth century scholar Abu Jafar Muhammad
Ibn Musu Al-Khowarizmi , is defined as follows: Roughly speaking:
An algorithm is a set of rules for carrying out calculation either by hand or a machine.
An algorithm is a finite step-by-step procedure to achieve a required result.
An algorithm is a sequence of computational steps that transform the input into the
output.
An algorithm is a sequence of operations performed on data that have to be organized
in data structures.
An algorithm is an abstraction of a program to be executed on a physical machine
(model of computation)
An algorithm therefore can be characterized by the following:
A finite set or sequence of actions
This sequence of actions has a unique initial action
Each action in the sequence has unique successor
The sequence terminates with either a solution or a statement that the problem is
unresolved.
An algorithm can therefore be seen as a step-by-step method of solving a problem.
Algorithms
Examples
Write an algorithm to read values for three variables. U, V, and W and find a value for
RESULT from the formula: RESULT = U + V2/W. Draw the flowchart.
Solution:
Algorithm
Input values for U, V, and W
Computer value for result
Print value of result
Stop
Pseudocode
Pseudocode is a program design aid that serves the function of a flowchart in expressing
the detailed logic of a program. Sometimes a program flowchart might be inadequate for
expressing the control flow and logic of a program. By using Pseudocode, program
algorithm can be expressed as English-language statements. These statements can be
used both as a guide when coding the program in specific language and as
documentation for review by others. Because there is no rigid rule for constructing
pseudocodes, the logic of the program can be expressed in a manner without confronting
to any particular programming language. A series of structured words is used to express
the major program functions. These structured words are the basis for writing programs
using a technical term called structure programming.
Example: Construct Pseudocode for the problem in the example above.
BEGIN
STORE 0 TO SUM
STORE 1 TO COUNT
DO WHILE COUNT not greater than 10
ADD COUNT to SUM
INCREMENT COUNT by 1
ENDWILE
END
Decision Tables
Decision tables are used to analyze a problem. The conditions applying in the problem
are set out and the actions to be taken, as a result of any combination of the conditions
arising are shown. They are prepared in conjunction with or in place of flowcharts.
Decision tables are a simple yet powerful and unambiguous way of showing the actions to
be taken when a given set of conditions occur. Moreover, they can be used to verify that
all conditions have been properly catered for. In this way they can reduce the possibility
that rare or unforeseen combinations of conditions will result in confusion about the
actions to be taken.
Decision tables have standardized format and comprise of four sections.
Conditions Stub: This section contains a list of all possible conditions which could apply
in a particular problem.
Condition Entry: This section contains the different combination of the conditions each
combination being given a number termed a Rule:
Action Stub: This section contains a list of the possible actions which could apply for ay
given combinations of conditions.
Action Entry: This section shows the actions to be taken for each combination of
conditions.
VARIABLES
A name given to an object. It can be subject to change during program
execution. Basically, variable can be grouped into two. These are
Numeric Variable
String variable.
NUMBERS: In Basic, Numbers can either be
Integer - all positive and negative whole numbers),e.g. 567, 34, -45, -988
etc.
Real -all positive and negative numbers with fractional part, e.g. 56.980,
3.0,-6.789,-324.566 etc.
STRINGS: Alphabets, digits, and other special characters enclosed in
quotation mark. Blank spaces inclusive. Strings are used to represent
non-numeric information such as names, address, telephone number,
and other textual messages.
CONSTANTS
This is defined as a quantity that remains fixed (unchanged) throughout the
execution of the program (i.e. it is not subject to change.
Constant can be classified into two
--numeric constant (known as numbers)
--string constant.
Constant is assigned to variable.
-- a numeric constant must be assigned to a numeric variable, and
string constant must be assigned to a string variable.
--a string constant must be in quote.
Examples of valid numeric constants (numbers): 10 , 15 , 23.45 ,-6 , -17 ,-34.11 etc
NOTE:A number can be preceded with a plus (+) or minus (-) sign. The number is
understood to be positive if no sign precedes it.
Number can be up to eight or nine significant figure.
The magnitude of a number can be as large as 1038 and as small as 10-38 (this
depends on the version of BASIC you are using).
Comma cannot appear anywhere in a number.
No other special character is allowed to be used with number, except dot (.)
Examples below are invalid numeric constants: 12,526, N1000, 035-1234566 etc
OPERATORS
An operator defines the action to be performed on the
operand(s) to obtain the result. An operand simply
means item in an operation from which the result is
obtained by means of defined action(s). Operand consists
constants and variables.
There are basically three types of operators in BASIC
programming. These are:
Arithmetic operators
Relational operators
Logical operators
ARITHMETIC OPERATORS
As the name suggests, arithmetic operators are used for arithmetic
operations. The various types of operators are their corresponding
functions are given below
OPERATOR
^
- or +
FUNCTION
Exponentiation
Unary operation
EXAMPLE
2^3
-(2)
*
/
\
MOD
+
-
Multiplication
Division
Integer division
Integer remainder
Addition
Subtraction
2*2
12/4
14\3
14 MOD 3
5+2
10-4
RESULT
8
-2
4
3
4
2
7
6
RELATIONAL OPERATORS
Relational operators are used for comparison operations.
Mostly, they are used to compare two strings or two numeric
expressions so as to obtained specific result. The result
returns either a true or false.
OPERATOR
=
<>
<
>
<=
>=
FUNCTION
Equality
Not equal to
Less than
Greater than
Less than or Equal to
Greater than or Equal to
EXAMPLE
A=B
A<>b
A<B
A>B
A<=B
A>=B
LOGICAL OPERATORS
They are used to perform tests on multiple relations. Besides, they are used to build compound and complex statements.
Each of these operators returns either true or false. There are many logical operators, but we are going to discuss only
three (3) out of them.
OPERATOR
i.
ii.
iii.
FUNCTION
NOT
AND
OR
EXAMPLE
Logical Complement
Conjunction
Disjunction
NOT P
(A=10) AND (B=15)
(A=10) OR (B=15)
Note: When arithmetic, relational, and logical operators are combined in one expression, the
arithmetic operators are always done first, followed by relational operators, and then logical operators.
PRECEDENCE/HIERACHY OF OPERATORS (ORDER OF EXECUTION)
OPERATOR
DEFINITION
PRECEDENCE LEVEL
^
Exponentiation
1
- or +
Unary operator
2
* and /
Multiplication and Division
3
+ and Addition and Subtraction
4
= and < >
Equal to and Not equal to
5
< and >
Less than and greater than
5
>=
Greater than or equal to
5
<=
Less than or equal to
5
NOT
Logical complement
6
AND
Conjunction
7
OR
Disjunction
7
BASIC performs operators with lower precedence first and performs operators with equal precedence sequentially from
left to right.
THE USE OF PARENTHESIS: Parenthesis is used to alter the normal hierarchy of operation. The expressions in
parenthesis are performed first before putting the normal precedence level into operation.
SUMMARY
Developed by Professor John G. Kenemy and Thomas Kurtz of Dartmouth University in the early
sixties.
To uniquely identify a line containing a BASIC statement, line number/label may be used at the
beginning of the line containing the statement.
Two or more BASIC statements cannot have the same line number.
Correct syntax must be used, otherwise the program will not be executed, let alone generate result.
BASIC CHARACTER SET
Alphabets A to Z (both in lower case and upper case),
Digits 0 to 9 (both signed and unsigned), and
Certain special characters such as: + ,-, *, /, (, ), ^, #, <, >, =, $ etc.
BASIC RESERVED/KEY WORDS:
CLS, REM, LET, INPUT, PRINT, READ, DATA, OPEN, CLOSE, END, FOR, NEXT, DO, LOOP, WHILE,
WEND, DIM
VARIABLES
Numeric Variable
String variable.
CONSTANTS
OPERATORS
Arithmetic operators
Relational operators
Logical operators
SUMMARY
EXPRESSIONs
An expression simply means collection or combination of
operands (constants, variable etc) with operators to form an
algebraic term.
In forming an expression, two operators must not follow
each other. However, parenthesis must be used to separate
two successive operators.
Example:
SUM = ( R + 10 ) / ( J 3 )
Basic Keywords
DESCRIPTION OF SOME BASIC
KEYWORDS
REM STATEMENT: This statement allows explanatory remarks
(comments) to be inserted into a program. Apostrophe ( ) performs the
same function as REM. E.g.
REM Program to find the area of a triangle
OR
Program to find the area of a triangle
The only slight different between the two is that apostrophe ( ) can be
placed at the front of a BASIC statement so as to introduce remark which
explains the function of the expression
CLS: This is simply used to clear the screen during program execution
RESULT
56.79
56.78
=N=56.79
$56.79
56,789
K
KAYODE
Example
PRINT TAB(50); GOOD
LPRINT & LPRINT USING STATEMENTS
These statements function in the same way as the PRINT and LPRINT
statements except that the output goes to the line printer.
These statements should be used independently, not in combined form.
That is use LPRINT separately AND LPRINT USING separately.
Control Statements
TRANSFER OF CONTROL
Normally, a BASIC program is executed in a
sequential order as the statements follow one
another. However, sometimes, it is necessary to
jump to some lines of the program. Thus, altering
the normal sequence of execution.
Two types of transfer of control. These are:
--Unconditional transfer
--Conditional transfer
INPUT A , B
IF A > B THEN PRINT The value of A is greater than B : END
IF B > A THEN PRINT The value of B is greater than A : END
IF A = B THEN PRINT The value of A is equal to B : END
NOTE: ELSEIF statement could be included within IFTHEN block.
Example 2c
INPUT A , B
IF A > B THEN
PRINT The value of A is greater than B
ELSEIF B > A THEN
PRINT The value of B is greater than A
ELSE
PRINT The value of A is equal to B
END IF
Loop Statements
LOOP STATEMENTS
This is another operation that is often required in programming. This
involves repeating certain portion of the program for specific number of
times or until particular condition(s) is/are satisfied.
Loop is basically divided into two
--Unconditional loop
--Conditional loop
UNCONDITIONAL LOOP: this is the act of repeating certain portion of
the program for a specified number of times without testing for any
condition. FORNEXT statement is used to carryout this kind of loop.
CONDITIONAL LOOP: This is the act of repeating certain portion of
the program as long as the condition(s) tested come(s) true or as long as
the condition(s) tested is/are
Satisfied
Example of BASIC keywords for implementing this loop are
DO...LOOP and WHILEWEND statements
SUMMARY
EXPRESSIONs
Example : Sum= (r*3)/ (h*5)
DESCRIPTION OF SOME BASIC KEYWORDS
REM STATEMENT
CLS
LET STATEMENT
READ STATEMENT
DATA STATEMENT
INPUT STATEMENT
LINE INPUT STATEMENT
PRINT STATEMENT
PRINT USING STATEMENT
LOCATE STATEMENT
TAB STATEMENT
LPRINT & LPRINT USING STATEMENTS
RESTORE STATEMENT
STOP STATEMENT
END STATEMENT
TRANSFER OF CONTROL
Unconditional transfer
Conditional transfer
SUMMARY
GOTO
IF..THEN..ELSE
LOOP STATEMENTS
Unconditional loop
Conditional loop
FOR NEXT
DO LOOP
WHILE ..WEND
DO
[statement]
LOOP WHILE condition
The only difference between the two syntax above is that, in the
first loop the condition is tested and if the condition is true, then
the loop is executed. The second one will execute the statement
within the loop for the first time before testing for the condition.
K=5
DO WHILE K< = 4
PRINT The condition is satisfied
satisfied
PRINT K
K=K+1
LOOP
Syntax2:
DO UNTIL condition
[statement]
LOOP
OR
K=5
DO
PRINT The condition is
PRINT K
K=K+1
LOOP WHILE K< = 4
DO
[statement]
LOOP UNTIL condition
The only difference between the two examples above is that, in the first loop the
condition is tested and if the condition is true, then the loop is executed. The
second one will execute the statement within the loop for the first time before
testing for the condition. The example below explains the two better.
K=5
DO UNTIL K > 4
PRINT The condition is satisfied
PRINT K
K=K+1
LOOP
K=5
DO
PRINT The condition is satisfied
PRINT K
K=K+1
LOOP UNTIL K > 4
Library Functions
LIBRARY FUNCTIONS
These functions have predefined meaning to BASIC programming language.
They perform special set of operations that might be difficult by mere writing a
set of program code.
Some of these functions are described below.
ABS(x) This is a math function that returns the absolute value of x.
Note x must be a numeric expression.
Example PRINT ABS(-10) displays 10
SIN(x)
COS(x) This is a math function the returns the cosine of angle x in radians.
Example PRINT COS(40) displays -.6669381
TAN(x) This is a math function the returns the tangent of angle x in radians.
Example PRINT TAN(40) displays -1.117215
MID$
This is a string processing function that returns a substring of a string. The
syntax is MID$(string,start,length) where start is a numeric expression that defines
the starting character position of the substring and length is length of the substring.
Although, the length can be omitted if you want all the characters to the right.
Example PRINT MID$(KAYODE,2,3) displays AYO
Sample Programs
SAMPLE PROGRAMS
Write a program to find the area of a triangle using the Heros formula. That is:
AREA =
s (s a)(s b)(s c) where s = ( a + b + c ) / 2
Solution:
1
2
3
4
5
6
7
CLS
INPUT Enter the value of the side a: , a
INPUT Enter the value of the side b: , b
INPUT Enter the value of the side c: , c
s = (a + b + c) / 2
AREA = ABS(SQR(s*(s a)*(s b)*(s c)))
PRINT The Area of the Triangle = ; AREA
CLS
SUM = 0
COUNT = 0
FOR K=1 TO 30
IF (K MOD 2) = 0 THEN to detect even number
PRINT K
SUM = SUM + K
COUNT = COUNT + 1
END IF
NEXT K
PRINT SUM = ; SUM
PRINT AVERAGE = ; SUM / COUNT
CLS
PRINT ANGLE SINE_OF_ANGLE
COSINE_OF_ANGLE TANGENT_OF_ANGLE
PRINT
FOR ANGLE = 10 TO 30
S = SIN ( (3.142 / 180) * ANGLE)
C = COS ( (3.142 / 180) * ANGLE)
T = TAN ( (3.142 / 180) * ANGLE)
PRINT ANGLE; TAB(10);
PRINT USING #.####;S; TAB(27);
PRINT USING #.####;C; TAB(46);
PRINT USING #.####;T
NEXT ANGLE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
CLS
DIM NUM(8)
FOR K = 1 TO 8
READ NUM(K)
NEXT K
DATA 4,1,2,3,6,8,-4,9
TEST=NUM(8)
FOR K = 1 TO 8
IF TEST > = NUM(K) THEN
TEST = NUM(K)
END IF
NEXT K
PRINT The smallest number = ; TEST
FOR K = 1 TO 8
IF TEST < = NUM(K) THEN
TEST = NUM(K)
END IF
NEXT K
PRINT The largest number = ; TEST
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
CLS
FOR J = 1 TO 30
SUM = 0
FOR K = 1 TO (J 1)
IF (J MOD K) = 0 THEN
SUM = SUM + K
ENDIF
NEXT K
IF J = SUM THEN
PRINT J, is a perfect number
ELSEIF J < SUM THEN
PRINT J, is a deficient number
ELSE
PRINT J, is an abundant number
END IF
NEXT J
CLS
SUM =0
COUNT = 0
FOR N=1 TO 50 STEP 2
PRINT N
SUM = SUM + N
COUNT = COUNT + 1
IF COUNT = 15 THEN
PRINT SUM = ;SUM
END
END IF
NEXT N
1
2
3
Given that A=10, B=15, and C=5. Write a program that assigns the value of A to B,
the value of B to C and the value of C to A (i.e the result becomes A=5, B=10 and
C=15)
Solution:
1
2
3
4
5
6
CLS
A = 10 : B = 15 : C =5
Assigning the values
TEMP = B
B = A : A = C : C = TEMP
PRINT A = ; A, B =;B, C =;C
2
3
4
5
6
7
8
9
10
11
12
13
14
Write a program to input and output the name, sex, and age of 10 people.
Solution:
1
2
3
4
5
6
7
8
9
10
VISUAL BASIC
Visual Basic is an event driven language which
has some features of Object Oriented
Programming (OOP).
Actions are tied to the occurrence of events e.g.
an action may be triggered by clicking the mouse
NOTE: Simply drag and drop a tool from the toolbar to the form panel,and dont forget to
save your project,your lecturer will teach you that as we might not remember to mention
this gain as we move on)
The design panel is the output of the code ran in the coding panel, even though if
the design is first created always be sure that the code has been automatically
generated. You can run your code by simply pressing F5 or go to the MenuBar
THE MENUBAR
Dont forget you can change the features of each tool in its property window which is at the
right hand side of Microsoft Visual Studios 6
NOTE: the NAME of each tool is VERY IMPORTANT!, because each tool will be referenced by
its name, so be careful and always give your tools good and distinct names
Suggestion of good names are : LblAddress will be good for a label carrying the address of a
client, BtnAddComputefor button that computes Addition etc
Knowing this, I guess we can proceed to the programming part, I hope you will catch up as
we move further.
Guess you didn't forget we have to declare our variable we use DIM for that
NOTE: DIM is different in BASIC and VISUAL BASIC