0% found this document useful (0 votes)
76 views

Chapter 4-Introduction To Programming

Computer

Uploaded by

Anthony
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Chapter 4-Introduction To Programming

Computer

Uploaded by

Anthony
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

MAT 224:- Introduction to Data Processing and Computers

Chapter 4: Introduction to programming programming language. The process of using such


languages to instruct the computer is known as
4.1 Chapter objectives
programming or coding.
By the end of this, learners should be able to:
We can therefore define a programming language as
 Understand programming levels
computer language used by programmers to develop
 Learn fundamental concepts underlying applications, scripts, or other set of instructions for a
programming languages computer to execute. It is a series of instructions that
 Understand the factors to consider while selecting a instruct the computer on how to perform tasks
programming language
4.3 Programming levels
 Identify the different parts of a C program
The figure below is intended to illustrate several key points
 Able to write, debug and execute a simple C program about programming languages. First, programming
4.2 Introduction to programming languages are built upon and relate directly to the
Users communicate with the computer in a language underlying computer (hardware). In fact, they are designed
understood by it. It needs to be instructed to perform all the to control the operation of the hardware. Second, these
tasks. The instructions are provided in the form of computer programming languages can be divided into TWO major
programs, which are developed by a programmer using a groups; low and high level programming languages
programming language. The language, which the user
employs to interact with the computer is known as computer

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

 It is very difficult to program in machine language.


The programmer has to know details of hardware to
write program.
 The programmer has to remember a lot of codes to
write a program which might easily result in program
errors.
 It is difficult to debug the program.

4.3.1.2 Assembly Language


Fig 1: programming levels
Instead of using a string of binary bits in a machine
4.3.1 Low-level Languages
language, assembly language use English-like words as
These are category of programming languages which are
commands that can be easily interpreted by programmers.
machine-dependent i.e. must run on specific machines. They
These commands are referred to as mnemonics. Examples
require extensive knowledge of computer hardware and its
of mnemonics in assembly language include ADD, SUB,
configuration. They are further divided in to Machine language
MUL, DIV.
and Assembly language.
Assembly language source must be translated into machine
4.3.1.1 Machine Language
code by a separate program known as assembler. The
Disadvantages of Machine Language
machine code can run on its own at any time therefore the
assembler and the source code are no longer needed. If

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

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

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

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.

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

4.4.2 Object Oriented Programming Paradigm Advantages of High-level Languages


Structured programming doesn’t t explain how to keep data High-level languages permit faster development of large
and programs together. However with OOP approach, data programs. The final program as executed by the computer
and functions (methods) are packaged into a single unit is not as efficient, but the savings in programmer time
called an object. generally far outweigh the inefficiencies of the finished
Emphasis is kept on data rather procedure. Large and product. This is because the cost of writing a program is
complex programs are divided into what are known as nearly constant for each line of code, regardless of the
objects. Data structures are designed in such a way that language. Thus, a high-level language where each line of
they characterize the objects. Objects that operate the data code translates to 10 machine instructions costs only one
of objects are tied together in the data structures. The data tenth as much in program development as a low-level
is always hidden and cannot be accessed by external language where each line of code represents only a single
functions. If objects need to communicate with one another, machine instruction.
they will do so through message passing. New data and
4.5 Factors to consider while selecting a programming
functions can be added wherever necessary. OOP follows
language
the bottom up approach of program design. Example of
The features of one programming language may differ from
OOP languages include C ++, java, C#, java applets e.t.c
the other. One can be easy and simple, while others can be
difficult and complex. A program written for a specific task
may have few lines in one language, while many lines in the

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

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.

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

4.5.4 Language Acquaintance application developed by an easy language is not efficient

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

Interpreter codes for the same computer then it is known as a self-


compiler. On the other hand, if a compiler runs on a
4.6.1 Assembler
computer and produces the machine codes for other
They are programs that convert assembly code to
computer then it is known as a cross compiler.
equivalent machine code. It will translate the symbolic
codes of programs of an assembly language into machine
Compilers will show errors and warnings for the statements
language instructions. The symbolic language is translated
violating the syntax rules of the language. They also have
the ability of linking subroutines of the program.

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

4.6.3 Interpreter 4.7 Program development Lifecycle (PDLC)


It translates high level languages into machine code. It PDLC refers to the steps programmers follow when building
translates only one statement of the program at a time. It computer programs. There are six steps which include:
reads only one statement of program, translates it and
1) Understand the problem
executes it. Then it reads the next statement of the program
2) Plan the logic
again translates it and executes it. In this way it proceeds
3) Code the program
further till all the statements are translated and executed.
4) Use software to translate the program to machine
On the other hand, a compiler goes through the entire
language
program and then translates the entire program into
machine codes. A compiler is 5 to 25 times faster than an 5) Test the program
interpreter. 6) Deploy the program into production

4.7.1 Understand the problem/analyze the problem


By the compiler, the machine codes are saved permanently
for future reference. On the other hand, the machine codes It’s the most difficult phase in program design since the
produced by interpreter are not saved. An interpreter is a users may not able to articulate their needs pretty well.
small program as compared to compiler. It occupies less Users need keep on changing. The process may force the
memory space, so it can be used in a smaller system which programmers to learn the user’s functional tasks.
has limited memory space. The programmer is supposed to meet with the system
analysts and the users with an aim of understanding what
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

the problem is. The programmer should also identify the


programs input, output and processing components. Programmers write instructions (source code) in English-
Failure to understand the problem is the major cause of like high level languages. This source codes have to be
most project failures. translated by translator program low-level machine
4.7.2 Planning the logic language that can be executed. During the translation,

During this stage, the programmer is supposed to plan the syntax errors are identified by the translator program

steps the program will take. 4.7.4 Testing the program

During this stage, the programmer executes the program


At this stage, the program activities are grouped into with sample data and checks the results then compare them
modules. A solution algorithm for each phase is provided with the user’s requirements. During this phase logic errors
and tested. Programmers using some design tools which are identified and corrected. When choosing the sample
can be graphical or written description for each module. data make sure you are careful to exercise all branches of
Tools such as algorithms, flowcharts and pseudocode are the logic.
used to design the program logic. 4.7.5 Putting the program into production
4.7.3 Coding, compiling & debugging the program
This is done after testing is complete and all errors in the
At this stage of program, the programmer selects the programs have been corrected. It may require coordination
programming language based on the task at hand then with other related activities or software.
writes instructions which implement the algorithms in step.

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

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

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

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

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

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.

4.11 Chapter summary


4.12 Chapter exercise
4.13 Further reading suggestion

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

You might also like