0% found this document useful (0 votes)
24 views24 pages

SCO102 PROGRAMMING Introduction Notes

IT

Uploaded by

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

SCO102 PROGRAMMING Introduction Notes

IT

Uploaded by

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

SCO102- INTRODUCTION TO COMPUTER PROGRAMMING

Lecture one

Introduction to Programming

Prepared by: Dr peter maina Mwangi


ELEMENTARY PROGRAMMING PRINCIPLES
Definition of terms
Computer Program:
 A computer program is a set of coded instructions given to the computer, and represents
a logical solution to a problem.
 It directs a computer in performing various operations/tasks on the data supplied to it.
 Computer programs may be written by the hardware manufacturers, Software houses, or
a programmer to solve user problems on the computer.
Cont:
Programming:
➢ Programming is the process of designing a set of instructions (computer programs)
which can be used to perform a particular task or solve a specific problem.
➢ It involves use of special characters, signs and symbols found in a particular
programming language to create computer instructions.
➢ The programming process is quite extensive. It includes analyzing of an application,
designing of a solution, coding for the processor, testing to produce an operating
program, and development of other procedures to make the system function.
➢ The program created must specify in detail the logical steps to be taken & the method
of processing the data input into the computer in order to carry out the specified task.
A computer program performs the following:
❑ Accepts data from outside the computer as its input.
❑ Carries out a set of processes on the data within the computer memory.
❑ Presents the results of this processing as its output, and
❑ Stores the data for future use.
Programming Languages:
 A programming language is a set of symbols (a language) which a computer programmer uses to solve a
given problem using a computer.
 The computer must be able to translate these instructions into machine-readable form when arranged in a
particular sequence or order.
TERMS USED IN COMPUTER PROGRAMMING
Source program (source code)
 The term Source program refers to program statements that the programmer enters in the program editor
window, and which have not yet been translated into machine-readable form.
 Source code is the code understood by the programmer, and is usually written in high-level language or
Assembly language.
Object code (object program).
 The term Object code refers to the program code that is in machine-readable (binary) form.
 This is the code/language the computer can understand, and is produced by a Compiler or Assembler after
translating the Source program into a form that can be readily loaded into the computer.
LANGUAGE TRANSLATORS
 A computer uses & stores information in binary form, and therefore, it cannot understand
programs written in either high-level or low-level languages.
 This means that, any program code written in Assembly language or high-level language must
be translated into Machine language, before the computer can recognize & run these programs.
 A Translator is special system software used to convert the Source codes (program statements
written in any of the computer programming languages) to their Object codes (computer
language equivalents).
 The Translators reside in the main memory of the computer, and use the program code of the
high-level or Assembly language as input data, changes the codes, and gives the output
program in machine-readable code.
 In addition, translators check for & identify some types of errors (e.g., Syntax/grammatical
errors) that may be present in the program being translated. They will produce error messages
if there is a mistake in the code.
Cont.:
 Each language needs its own translator. Generally, there are 3 types of language translators:
❖ Assembler.
❖ Interpreter.
❖ Compiler.

 Note. Interpreters & Compilers translate source programs written in high-level languages to their machine
language equivalents.
Assembler
➢ An assembler translates programs written in Assembly language into machine language that the
computer can understand and execute.
Functions of an Assembler
❖ It checks whether the instructions written are valid, and identifies any errors in the
program. The Assembler will display these errors as well as the complete source and object
programs. If the program has no errors, the job control will let it run immediately, or save the
object program so that it may run it later without translating it again.
❖ It assigns memory locations to the names the programmer uses. E.g., the Assembler keeps a
table of these names so that if an instruction refers to it, the Assembler can easily tell the
location to which it was assigned.
❖ It generates the machine code equivalent of the Assembly instructions. Usually, the Assembler
generates a machine code only when no errors are detected. Some of the errors include;

o Typing mistakes.
o Using the wrong format for an instruction.
o Specifying a memory location outside the range 0 – 2047.

 Note. The Assembler cannot detect Logic errors. The programmer knows of these errors only
when the program is run & the results produced are incorrect (not what the programmer
expected). The programmer must therefore, go through the program & try to discover why an
incorrect result was being produced
Interpreter
 An interpreter translates a source program word by word or line by line. This allows the
CPU to execute one line at a time.
 The Interpreter takes one line of the source program, translates it into a machine
instruction, and then it is immediately executed by the CPU.
 It then takes the next instruction, translates it into a machine instruction, and then the CPU
executes it, and so on.
 The translated line is not stored in the computer memory. Therefore, every time the
program is needed for execution, it has to be translated.
Compiler
 A compiler translates the entire/whole source program into object code at
once, and then executes it in machine language code. These machine code
instructions can then be run on the computer to perform the particular task
as specified in the high-level program.
 The process of translating a program written in a high-level source language
into machine language using a compiler is called Compilation.
 For a given machine, each language requires its own Compiler. E.g., for a
computer to be able translate a program written in FORTRAN into machine
language; the program must pass through the FORTRAN compiler (which must
‘know’ FORTRAN as well as the Machine language of the computer).
 The object code file can be made into a fully executable program by carrying
out a Linking process, which joins the object code to all the other files that
are needed for the execution of the program.
 After the linking process, an executable file with an .EXE extension is
generated. This file is stored on a storage media.

Cont.
Points to note.
 The job of a Compiler is much more difficult than that of an Assembler in
that, a single statement in a high-level language is equivalent to many
machine instructions.
 The format of an Assembly instruction is fairly fixed, while high-level
languages give a lot of freedom in the way the programmer writes
statements.
Cont.
Functions of a compiler
A Compiler performs the following tasks during the compilation process:
 It identifies the proper order of processing, so as to execute the process as
fast as possible & minimize the storage space required in memory.
 It allocates space in memory for the storage locations defined in the program
to be executed.
 It reads each line of the source program & converts it into machine language.
 It checks for Syntax errors in a program (i.e., statements which do not
conform to the grammatical rules of the language). If there are no syntax
errors, it generates machine code equivalent to the given program.
 It combines the program (machine) code generated with the appropriate
subroutines from the library.
 It produces a listing of the program, indicating errors, if any.
Differences between Compilers and Interpreters
Interpreter Compiler
1. Translates & executes each statement of the source 1. Translates all the source code statements at once as
code one at a time. a unit into their corresponding object codes, before
the computer can execute them.
The source code instruction is translated &
immediately obeyed by the computer hardware A Compiler translates the entire source program first
before the next instruction can be translated. to machine code, and then the code is executed by
(Translation & execution go together). the CPU.
(Translation & execution are separate phases)

1. Compiled programs (object codes) can be saved on


1. Translates the program each time it is needed for a storage media and run when required; hence
execution; hence, it is slower than compiling. executes faster than interpreted programs.
1. Compiled programs require more memory as their
1. Interpreted object codes take less memory object files are larger.
compared to compiled programs. 1. For a Compiler, the syntax errors are reported &
1. For an Interpreter, the syntax (grammatical) errors corrected after the source code has been translated
are reported & corrected before the execution can to its object code equivalent.
continue. 1. Once the source program has been translated, it is
no longer available to the Compiler, so the error
1. An Interpreter can relate error messages to the messages are usually less meaningful.
source program, which is always available to the
Interpreter. This makes debugging of a program
easier when using an Interpreter than a Compiler.
cont.
Linkers & Loaders
 Computer programs are usually developed in Modules or Subroutines (i.e., program segments
meant to carry out the specific relevant tasks). During program translation, these modules are
translated separately into their object (machine) code equivalents.

 The Linker is a utility software that accepts the separately translated program modules as its
input, and logically combines them into one logical module, known as the Load Module that has
got all the required bits and pieces for the translated program to be obeyed by the computer
hardware.
 The Loader is a utility program that transfers the load module (i.e. the linker output) into the
computer memory, ready for it to be executed by the computer hardware.
Cont.

Syntax
 Each programming language has a special sequence or order of writing
characters.
 The term Syntax refers to the grammatical rules, which govern how words,
symbols, expressions and statements may be formed & combined.
Semantics
 These are rules, which govern the meaning of syntax. They dictate what
happens (takes place) when a program is run or executed.
LEVELS OF PROGRAMMING LANGUAGES
➢ There are many programming languages. The languages are classified into 2
major categories:
❖ Low-level programming languages.
❖ High-level programming languages.
 Each programming language has its own grammatical (syntax) rules, which
must be obeyed in order to write valid programs, just as a natural language
has its own rules for forming sentences.
LOW-LEVEL LANGUAGES
 These are the basic programming languages, which can easily be understood
by the computer directly, or which require little effort to be translated into
computer understandable form.
 They include:
❖ Machine languages.
❖ Assembly languages.
 Features of low-level languages
❖ They are machine hardware-oriented.
❖ They are not portable, i.e., a program written for one computer cannot be
installed and used on another computer of a different family.
❖ They use Mnemonic codes.
❖ They frequently use symbolic addresses.
Machine languages (1st Generation languages)

 Machine language is written using machine codes (binary digits) that consist of 0’s & 1’s.
 The computer can readily understand Machine code (language) instructions without any
translation.
 A programmer is required to write his program in strings of 0’s & 1’s, calculate &
allocate the core memory locations for his data and/or instructions.
 Different CPU’s have different machine codes, e.g., codes written for the Intel Pentium
processors may differ from those written for Motorola or Cyrix processors.
 Therefore, before interpreting the meaning of a particular code, a programmer must
know for which CPU the program was written.
 A machine code instruction is made up of 2 main parts;
 An Address (operand):
 It specifies the location (address) of the computer memory where the data to be worked upon
can be found.
 A Function (operation) code:
 It states to the Control Unit of the CPU what operation should be performed on the data/item
held in the address, e.g., Addition, Subtraction, Division, Multiplication, etc.
Assembly language (2nd Generation Languages)
 Assembly languages were developed in order to speed up programming (i.e., to overcome
the difficulties of understanding and using machine languages).
 The vocabulary of Assembly languages is close to that of machine language, and their
instructions are symbolic representations of the machine language instructions.
❖ Assembly language programs are easier to understand, use & modify compared to Machine
language programs.
❖ Assembly language programs have less error chances.
 To write program statements in Assembly language, the programmer uses a set of
symbolic operation codes called Mnemonic codes.
 The code could be a 2 or 3 shortened letter word that will cause the computer to perform
specific operation. E.g., MOV – move, ADD - addition, SUB – subtraction, RD - read.
Cont.
 To enable the CPU understand Assembly language instructions,
 An Assembler (which is stored in a ROM) is used to convert them into Machine language.
❖ The Assembler accepts the source codes written in an Assembly language as its input, and translates
them into their corresponding computer language (machine code/ object code) equivalent.
 Comments are incorporated into the program statements to make them easier to be
understood by the human programmers.
Cont.
Advantages of Low-level languages
 The CPU can easily understand machine language without translation.
 The program instructions can be executed by the hardware (processor) much
faster. This is because; complex instructions are already broken down into
smaller simpler ones.
 Low-level languages have a closer control over the hardware, are highly
efficient & allow direct control of each operation.
 They are therefore suitable for writing Operating system software & Game
programs, which require fast & efficient use of the CPU time.
 They require less memory space.
 Low-level languages are stable, i.e., they do not crash once written
Cont.
Disadvantages of Low-level languages
 Very few computer programs are actually written in machine or Assembly
language because of the following reasons;
 Low-level languages are difficult to learn, understand, and write programs in
them.
 Low-level language programs are difficult to debug (remove errors from).
 Low-level languages have a collection of very detailed & complex instructions that
control the internal circuiting of the computer. Therefore, it requires one to
understand how the computer codes internally.
 Relating the program & the problem structures is difficult, and therefore
cumbersome to work with.
 The programs are very long; hence, writing a program in a low-level language is
usually tedious & time consuming.
 The programs are difficult to develop, maintain, and are also prone to errors (i.e.,
it requires highly trained experts to develop and maintain the programs).
 Low level languages are machine-dependent (specific), hence non-portable.
HIGH-LEVEL PROGRAMMING LANGUAGES
 High-level languages were developed to solve (overcome) the problems encountered in
low-level programming languages.
 The grammar of High-level languages is very close to the vocabulary of the natural
languages used by human beings. Hence; they can be read and understood easily even
by people who are not experts in programming.
 Most high-level languages are general-purpose & problem-oriented. They allow the
programmer to concentrate on the functional details of a program rather than the
details of the hardware on which the program will run.
 Programs written in a high-level language cannot be obeyed by the computer hardware
directly. Therefore, the source codes must be translated into their corresponding
machine language equivalent. The translation process is carried out by a high-level
language software translator such as a Compiler or an Interpreter.
Features of high-level programming languages
 They contain statements that have an extensive vocabulary of words, symbols, sentences & mathematical
expressions, which are very similar to the normal English language.
Example;
Read (TaxablePay);
IF TaxablePay<1000 THEN
Tax: =0;
ELSE
Tax: =TaxRate * TaxablePay;
Write (Tax: 6:2);
 Allow modularization (sub-routines).
 They are ‘user-friendly’ and problem-oriented rather than machine-based. This implies that, during a
programming session, the programmer concentrates on problem-solving rather than how a machine
operates.
 They require one to obey a set of rules when writing the program.
 Programs written in high-level languages are shorter than their low-level language equivalents, since one
statement translates into several machine code instructions.
 The programs are portable between different computers.
Assignment
 Research on advantages and Disadvantages of high level programming
languages

You might also like