Chapter 4-Introduction To Programming
Chapter 4-Introduction To Programming
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
MAT 224:- Introduction to Data Processing and Computers
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
MAT 224:- Introduction to Data Processing and Computers
you make any changes to the source code, the assembler ii). It is necessary to remember the registers of CPU and
shall be needed to recompile the program again and mnemonic instructions by the programmer.
generate a new object program. iii). Several mnemonic instructions are needed to write in
Advantages of Assembly Language assembly language than a single line in high-level
i). Mnemonics are easier to understand hence saving language. Thus, assembly language programs are
the programmer lot of time and effort. longer than the high language programs.
ii). It is easier to correct errors and modify program Advantages of Low-level Languages
instructions.
Low-level languages have the advantage that they can be
iii). Assembly Language has the same efficiency of
written to take advantage of any peculiarities in the
execution as the machine level language. Because
architecture of the central CPU. Thus, a program written in
this is one-to-one translator between assembly
a low-level language can be extremely efficient, making
language program and its corresponding machine
optimum use of both computer memory and processing
language program.
time.
Disadvantages of Assembly Language
Disadvantages of Low-level Languages
i). Its machine dependent hence a program written for To write a low-level program takes a substantial amount of
one computer might not run in other computers with time, as well as a clear understanding of the inner workings
different hardware configuration i.e. There is no of the processor itself. Therefore, low-level programming is
program portability. typically used only for very small programs, or for segments
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
MAT 224:- Introduction to Data Processing and Computers
of code that are highly critical and must run as efficiently as The functions will share global data and these data will
possible. normally move freely around the system from one functions
4.3.2 High-Level Languages to another with the functions transforming the data from one
High-level programming languages allow the specification form to another. Procedure oriented programming employs
of a problem solution in terms closer to those used by top down approach for program design.
human beings. These languages were designed to make
programming far easier, less error-prone and to remove the Though structured programming has yielded excellent
programmer from having to know the details of the internal results when applied to moderately complex programs, it
structure of a particular computer. Some examples of fails when a program complexity increases. Some common
programming languages in these category include Java, procedural programming languages include BASIC,
Python, Prolog e.t.c. COBOL, C among others e.t.c
They are very convenient but very far removed from the
computer they are running on. They are independent
hence can run on different types of machines
4.4 Programming paradigms
4.4.1 Procedure oriented programming
The main emphasis is on doing things i.e. algorithms.
Ideally large programs are divided into smaller programs
known as functions/procedures/subroutines.
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
MAT 224:- Introduction to Data Processing and Computers
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
MAT 224:- Introduction to Data Processing and Computers
other. We judge the success and strength of a programming 4.5.2 Good Programming Language Features
language with respect to standard features. There a Its definition should be independent of any particular
number of factors while selecting a programming just to hardware or operating system. It should support software
mention a few engineering technology, discourage or prohibit poor
practices, and promote or support maintenance activities. It
4.5.1 Ease of Use
should support the required level of system reliability and
It’s the most important factor in choosing a language. The
safety. If the language does not support the basic
language should be easy in writing codes for the programs
requirements, then it is advisable to learn a new language
and executing them. The ease and clarity of a language
and then start the application development.
depends upon its syntax. It should be capable enough to
provide clear, simple and unified set of concepts. The 4.5.3 Nature of Application
vocabulary of the language should resemble English (or Each language provides certain distinctive features, which
some other natural language). Symbols, abbreviations and separates it from other languages. For example, Java
jargons should be avoided unless they are already known should be used to write an application that runs over the
to most people. Any concept that cannot be easily explained Internet, whereas to develop a system application, C and
to amateurs should not be included in the language. C++ are used.
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
MAT 224:- Introduction to Data Processing and Computers
In cases where a number of languages may suit the enough, then it is better to slug it out with a more complex
application to be developed, the most familiar language to language. For example, Visual Basic provides an easier
the programmers should be chosen. way to develop an application, but it does not have the same
4.5.5 Ease of Learning control over the resources as other languages such as
Visual C++ or Delphi.
Sometimes, it may necessity the programmer has to learn a
4.5.7 Speed Requirement
new language in order develop an application. In such a Some applications e.g. railway or air reservation need to be
case, the first step is to find out which language, along with quick as compared to batch processing applications such
its alternatives, would be suitable for the proposed as payroll and inventory control. In such cases, the
application. Then, the programmer has to decide which execution efficiency is important and the programmer
language is easier to learn and use. should use assembly language instead of high-level
4.5.6 Control over Resources languages. The main reason behind this is that assembly
Apart from the ease of use, the control factor should also be
language usually has a shorter production time as
considered while selecting a language. Easy to learn and
compared to high-level languages.
use feature of the language does not mean that the
4.6 Translators
application created with it will be the most powerful. Certain
It’s a program that converts source statement into machine
applications require more control over the hardware and
language. The source statements are just like English
hence the control factor should not be ignored. If the
words. The computer interprets the instructions as 1’s and
Email: [email protected] Mobile:-+254-725-695-782
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
MAT 224:- Introduction to Data Processing and Computers
0’s. A program can be written in assembly language as well to the machine code in the ratio of one is to one symbolic
as in high-level language. This written program is called the instructions to one machine code instructions.
source program. The source program is to be converted to 4.6.2 Compiler
the machine language, which is called an object program. It is a program which translates a high level language
Translators can be classified as depicted below follows program source code into a machine language/object
program code. A compiler is more intelligent than an
assembler. It checks all kinds of limits, ranges, errors etc.
But its program run time is more and occupies a larger part
of the memory.
The translator programs include, Assembler, compiler and If a compiler runs on a computer and produces the machine
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
MAT 224:- Introduction to Data Processing and Computers
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
MAT 224:- Introduction to Data Processing and Computers
During this stage, the programmer is supposed to plan the syntax errors are identified by the translator program
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
MAT 224:- Introduction to Data Processing and Computers
4.7.6 Maintain the program This involves typing in the program with a text editor and
making corrections if necessary.
The last activity of the PDLC is maintaining the program.
The program is stored as a text file on the disk
Maintaining the program involves a number of activities
4.8.2 Compile
such as
The translator converts the source into machine language
i). Identifying the errors
code (object code), which it stores on the disk .A linker then
ii). Identify any enhancements links the object code with standard library routines that the
iii). Modifying existing programs to improve their program may use and creates an executable image, which
functionality is also saved on disk, usually as a file with the file name
without any extension (e.g. hello).
4.8 The development environment and the
4.8.3 Execute
development cycle
The executable is loaded from the disk to memory and CPU
A program goes through 3 main phases during
executes the program one instruction at a time.
development: editing (writing the program), compiling (i.e.,
translating the program to executable code and detecting 4.9 Overview of C language
syntax errors) and running the program and checking for It was developed in 1972 by Dennis Ritchie at the Bell
logical errors (called debugging). telephone laboratories for use with UNIX operating system.
4.8.1 Editing It was designed for system programming because of its low
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
MAT 224:- Introduction to Data Processing and Computers
level memory management and its construct are close to 1. //sample program
the machine instructions. 2. include "stdio.h"
4.10 C program structure and syntax 3. void main()
Every C program consists of a header and a main body and 4. {
has the following structure. 5. printf ("This is a line of text to output.\n");
// Comment statements, which are ignored by computer 6. }
#include <header file name >
Void main() Line 1:
{ At the top of the program are comments and
declaration of variables ; instructions, which will not be executed. Lines
statements ; beginning with // indicate that the rest of the line is a
} comment. Comments are inserted by programmers to
help people read and understand the program. Here
We will consider each line of the above program. For the comment states the purpose of the program.
convenience the program is reproduced on the next page Comments are not executed by the computer. They
with line numbers to allow us to comment on details of the can in general be placed anywhere in a program.
program. However, you must not put line numbers in an Line 2: Lines beginning with # are instructions to the
actual program. compilers pre-processor. They include instruction says
what follows is a file name, find that file and insert its
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
MAT 224:- Introduction to Data Processing and Computers
contents right here. It is used to include the contents of The opening (left) brace marks the beginning of the
a file of definitions that may be used in the program. main body of the program. The main body consists of
Line 3: instructions, which can be declarations defining the
The word main is very important, and must appear data or statements on how the data should be
once (and only once) in every C program. This is the processed. All C declarations and statements must
point where execution begins when the program is run. end with a semicolon.
It does not have to be the first statement in the program Line 5:
but it must exist as the entry point. The use of the term printf( ) is a predefined function. To send output on the
main is actually a function definition. Functions must monitor, the text is put within the function parentheses
return a value and the term void is used to denote that and bounded by quotation marks. The definition of this
the main program does not return a value. Following function is defined in the STDIO library (the Standard
the main program name is a pair of parentheses ( ), Input/output library). To compile and link this program
which are an indication to the compiler that this is a the #include statement is required. The end result is
function. The two braces ( { }) are used to define the that whatever is included between the quotation marks
limits of the program itself: in this case, the main in the printf ( ) function will be displayed on the monitor
function. The actual program statements go between when the program is run. Notice the semi-colon at the
the two braces. end of the line. C uses a semicolon as a statement
Line 4: terminator, so the semi-colon is required as a signal to
the compiler that this line is complete.
Email: [email protected] Mobile:-+254-725-695-782
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author
MAT 224:- Introduction to Data Processing and Computers
Line 6
The closing (right) brace marks the end of the main
body of the program.
© By Edward Kioko. All rights reserved for this module. No part of this module may be reproduced or transmitted in any form or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the author