Introduction-To-Programming 10

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Ministry of Secondary Education Republic of Cameroon

Progressive Comprehensive High School Peace – Work – Fatherland


PCHS Mankon – Bamenda School Year: 2014/2015
Department of Computer Studies

TOPIC: INTRODUCTION TO PROGRAMMING


Class: Comp. Sc. A/L By: DZEUGANG PLACIDE

Although computers appear to be amazingly intelligent machines, they cannot yet think on their
own. They still rely on human being to give them directives. Computers are very literal, and
they must be told what to do right down to the last detail. It is only as effective as the
instructions we give it, and those instructions are contained in what we actually called program.
This chapter explain the concept of programming, as well as different programming paradigms.

Learning objectives
After studying this lesson, student should be able to:
 Discuss what a computer program is.
 State and explain the different steps to write a program
 Discuss some examples of programming paradigm with some examples in each case.
 Discuss the concepts of testing, debugging, documentation
 Define some concepts related to Object Oriented Programming

Contents
I. DEFINITIONS .......................................................................................................................... 2
II. CHARACTERISTICS OF A GOOD PROGRAM ...................................................................... 2
III. DEVELOPING A PROGRAM .............................................................................................. 2
IV. PROGRAMMING LANGUAGES ......................................................................................... 4
V. PROGRAMMING PARADIGMS ............................................................................................. 8
VI. PROGRAM TESTING AND DEBUGGING ......................................................................... 9
VII. PROGRAM DOCUMENTATION ....................................................................................... 11
VIII. OBJECT ORIENTED PROGRAMMING (OOP) ................................................................. 12

visit www.dzplacide.overblog.com to discover many other learning tools for computer science & ICT
Topic: Introduction to programming 2 By DZEUGANG Placide

I. DEFINITIONS

A Computer program (also called software or just a program) is a sequence of instructions


written to perform a specified task with a computer. The act of writing computer program is
referred to as programming. The statements that make up a computer program are collectively
referred to as program code.

II. CHARACTERISTICS OF A GOOD PROGRAM

Some of the most essential attributes of good computer program include

(a) Efficiency: This is the degree with which the program fulfils its purposes without waste
of resources
(b) Reusability: This is the ease with which a program or a module of a program can be
reused to design other program.
(c) Maintainability: this is the ease with which modifications can be made to satisfy new
requirements or to correct deficiencies. Well-designed program should be flexible
enough to accommodate future changes that will be needed as new requirements come
to light.
(d) Reliability: This is the frequency and extends to which a program fails to perform its
functions under normal operating circumstances.
(e) Correctness: This is the degree with which a program meets its specified requirements.
(f) Robustness: This is the degree with which software resist to users manipulations errors
(g) Documentation: enough comment should be inserted in a program to facilitate its
understanding by any computer user
(h) Portability: Portability refers to the ability of an application to run on different
platforms (operating systems) with or without minimal changes.
(i) Flexibility: A program should be flexible enough to handle most of the changes without
having to rewrite the entire program.
(j) Readability: The program should be written in such a way that it makes other
programmers or users to follow the logic of the program without much effort.

III. DEVELOPING A PROGRAM

A program consists of a series of instructions that a computer processes to perform the required
operation. In addition, it also includes some fixed data, required to perform the instructions,
and the process of defining those instructions and data. Thus, in order to design a program, a
programmer must determine three basic rudiments:

• The instructions to be performed.


• The order in which those instructions are to be performed
• The data required to perform those instructions.

visit www.dzplacide.overblog.com to discover many other learning tools for computer science & ICT
Topic: Introduction to programming 3 By DZEUGANG Placide

I.1. Program Development Cycle


There are different approaches to problem solving. One common approach is to use the
program development cycle, with the number of steps that may vary according to the person
who has formalized the development. The development cycle of a program includes the
following phases

Problem
analysis

Task analysis
Documentation

Algorithm
Testing and development
debugging
Coding

Figure 1. Program Development Cycle

 Problem Analysis: In this phase, the problem is analysed precisely and completely.
Based on one's understanding, the developer knows about the scope within which
the problem needs to be developed.
 Task Analysis: After analysing the problem, the developer needs to develop
various solutions to solve the given problem. From these solutions, the optimum
solution is chosen, which can solve the problem comfortably and economically.
 Algorithm Development: An algorithm depicts the solution in logical steps
(sequence of instructions).
 Coding: After meeting all the design considerations, the actual coding of the
program takes place in the chosen programming language. languages.
 Testing and Debugging: A program compiler and programmer-designed test data
machine test the code for syntax errors. The results obtained are compared with
results calculated manually from these test data.
 Documentation: Documenting a program enables the user to operate the program
correctly. It also enables other persons to understand the program clearly so that it
may, if necessary, be modified, or corrected by someone other than the original
programmer.

visit www.dzplacide.overblog.com to discover many other learning tools for computer science & ICT
Topic: Introduction to programming 4 By DZEUGANG Placide

IV. PROGRAMMING LANGUAGES

IV.1. Definition and characteristics of programming languages

A programming language is then defined as a language that is used for creating computer
programs. A programming language is determined by syntax and semantic.

- The syntax is the rules guiding the formulations of the program statement. It specifies
how words and symbols are put together to form statements and expressions. It is the
grammar of the language.
- Semantic describes the meaning of a program statement. It is the vocabulary of the
language.

Several characteristics believed to be important with respect to making a programming


language good are briefly discussed below.

 Simplicity A good programming language must be simple and easy to learn and use.
 Abstraction Abstraction means the ability to define and use complicated structures
or operations in ways that allow many of the details to be ignored.
 Efficiency The program written in good programming language are efficiently
translated into machine code, are efficiently executed, and acquires as little space in
the memory as possible.
 Structuredness Structuredness means that the language should have necessary
features to allow its users to write their programs based on the concepts of structured
programming.
 Compactness In a good programming language, programmers should be able to
express intended operation concisely.
 Extensibility A good programming language should allow extension through simple,
natural, and elegant mechanisms.
 Suitability to its Environment the language must also be made suitable to its
environment.

IV.2. History and Evolution of programming languages

The evolution of computer programming is typically discussed in term of five important


generations. The diagram below illustrates how the different generations are related to one
another

a) First generation: Machine language

Machine language is a set of instructions and data that a computer's central processing unit can
execute directly. Machine language statements are written in binary code, and each statement
corresponds to one machine action.

Characteristics:

visit www.dzplacide.overblog.com to discover many other learning tools for computer science & ICT
Topic: Introduction to programming 5 By DZEUGANG Placide

a) Each computer can only understand programs that are written in its own ML
b) ML is provided by the computer manufacturer
c) Coded as a series of 0’s and 1’s. E.g. 1001110001

Disadvantages

a) Very difficult to write because:


 Binary system not ‘user-friendly’ to human
 It requires excellent memorising power
b) Programmer has to keep track of storage locations of data and instruction.
c) Difficult to understand and debug

b) Second generation: assembly language

Assembly language is the human-readable notation for the machine language used to control
specific computer operations. Programs are written using symbolic instruction codes that are
meaningful abbreviations or mnemonics. Program written in assembly language are translated
into machine language using assembler

Characteristics Weakness
a) Also provided by the manufacturer a) Machine dependant
b) One instruction for each computer b) The program is usually long
operation c) Hard to learn and slow to write
c) Computer codes are representing by
mnemonics (a set of letters)
d) The code must be assembled into
machine language before execution.

c) Third generation: High level language

The third generation of programming language, 3GL, or procedural language uses a


series of English-like words, that are closer to human language, to write instructions. High-
level programming languages make complex programming simpler and easier to read, write
and maintain. Programs written in a high-level programming language must be translated into
machine language by a compiler or interpreter.

Some examples of third generation languages are: PASCAL, FORTRAN, BASIC, COBOL,
C and C++, HTML,

d) Fourth generation: Very High-Level Language

The fourth generation programming language or non-procedural language, often abbreviated


as 4GL, enables users to access data in a database. A very high-level programming language
is often referred to as goal-oriented programming language because it is usually limited to
a very specific application and it might use syntax that is never used in other programming
languages.

SQL, NOMAD and FOCUS are examples of fourth generation programming languages.

visit www.dzplacide.overblog.com to discover many other learning tools for computer science & ICT
Topic: Introduction to programming 6 By DZEUGANG Placide

e) Fifth generation: Natural language

The fifth generation programming language or visual programming language is also known as
natural language. Provides a visual or graphical interface, called a visual programming
environment, for creating source codes.

Prolog and Mercury, are the best known fifth-generation languages.

IV-3. Types of programming languages

Programming languages can be classified into two main types: Low-level programming
language and High-level programming language.

a) A low-level programming language is a programming language that provides


little or no abstraction from computer’s microprocessor. They are machine-oriented
languages
b) A high-level programming language is a programming language that is more abstract,
easier to use, and more portable across platforms.

Comparison

Low Level programming language High Level Programming Language


- Take up less storage space - Make programming much more convenient
- Run faster - Writtent using common names and words (more
- Useful for writing system like human language)
programs for they require fast and - Problem oriented language (designed to solve a
efficient use of CPU (e.g. specific problem)
operating systems) - Easy to write, to read, to understand and to
- Sometime an operation can be modify (written in english-like format)
only be performed in a low-level - Programs are shorter and faster to code (one
language. statement for several computer operations)
- More portable (can be executed by differend
computers i.e. machine independant)
- Language more closer to the problem
description

visit www.dzplacide.overblog.com to discover many other learning tools for computer science & ICT
Topic: Introduction to programming 7 By DZEUGANG Placide

IV.4- Language translation

The computer interprets the instructions as 1's and 0's. A source program written in assembly
language or in a high-level language must be converted to the machine language (object
program) before being executed by the machine. This process is called translation and requires
a tool called translator

Fig: a simplified translation process

There exist 3 types of translators:

a) Assembler: An assembler translates the symbolic codes of programs of an assembly


language into machine language instructions
b) Compiler: Compilation or compiling is a process that translates a program in one
language (the source language) into an equivalent program in another language (the
object or target language).
c) Interpreter: Interpreter reads the program line by line, whereas in compiler the entire
program is read by the compiler, which then generates the object codes. Interpreter
directly executes the program from its source code. Due to this, every time the source
code should be inputted to the interpreter.

Fig: The functions of the 3 types of translators

Similarities between compilers and interpreters

- Both translate high-level languages to machine codes


- Both direct errors in the programs and print error messages
- Both work out where to store the object program and its data in the memory

visit www.dzplacide.overblog.com to discover many other learning tools for computer science & ICT
Topic: Introduction to programming 8 By DZEUGANG Placide

Differences between compilers and interpreters

IV.4- Features of a good programming language

V. PROGRAMMING PARADIGMS

A programming paradigm is a fundamental style of computer programming, a way of


building the structure and elements of computer programs. Paradigms differ in the concepts
used to represents the elements of a program (object, functions, variables, ...) and the steps that
compose a computation (assignment, evaluation, continuation, ...). some programming
languages are designed to follow only one paradigm, while others support multiple paradigms.

The three main categories of programming paradigms are: imperative, object-oriented and
declarative.

1) Imperative languages

Imperative paradigm, also known as Procedural programming specifies a list of operations that
the program must complete to reach the desired state. Each program has a starting state, a list
of operations to complete, and an ending point.

The advantages of this paradigm are its efficiency, closeness to the machine and popularity.
The disadvantages include: the difficulties in debugging as well as the difficulties in
understanding the semantic of a program. Typical examples of typical imperative programming
languages are: Fortran, Algol, Pascal, C and Basic

2) Object-oriented programming

Object-oriented programming is one the newest and most powerful paradigms. In object
oriented programs, the designer specifies both the data structures (called attributes) and the
types of operations that can be applied to those data structures (called methods). This pairing
of a piece of data with the operations that can be performed on it is known as an object. A
program thus becomes a collection of cooperating objects, rather than a list of instructions.
Objects can store state information and interact with other objects, but generally each object
has a distinct, limited role.

visit www.dzplacide.overblog.com to discover many other learning tools for computer science & ICT
Topic: Introduction to programming 9 By DZEUGANG Placide

Popular examples of object-oriented programming languages include: C++, Java, Python, C#


and Perl

3) Declarative languages

Declarative programming is an approach to computer programming that involves the creation


of a set of conditions that describe a solution space, but leave the interpretation of the specific
step needed to arrive at that solution up to an unspecified interpreter. Logic and functional
programming are two major types of declarative programming.

o Functional programming: Functional programming languages define subroutines and


programs as mathematical functions. These languages are most frequently used in
various types of mathematical problem solving. Popular functional languages include
LISP and Mathematica.
o Logic programming: Within the logic paradigm, program is thought of as a set of
logic formulae: axioms (facts and rules) describing properties of certain objects, and a
theorem to be proved. Program execution is a process of logic proving (inference) of
the theorem through constructing the objects with the described properties. E.g. Prolog

Paradigm Key Program Program execution Result


concept
Imperative Command Sequence of command Execution of command Final state of
computer memory
Functional Function Collection of functions Evaluation of functions Value of the main
function
Logic Predicate Logic formulas: Logic theorem of the Failure or success
axioms and a theorem theorem of proving
Object- Object Collection of classes Exchange of messages Final state of the
oriented of objects between objects objects’ states
Table: Features of the main programming paradigms

Two final paradigms to discuss are compiled languages and interpreted languages.

d) Compiled languages use a program called a compiler to translate source code into
machine instructions, usually saved to a separate executable file. E.g. Pascal, C
e) Interpreted languages, in contrast, are programs that can be executed from directly
from source code by a special program called an interpreter. This distinction refers to
the implementation, rather than the language design itself. LISP and HTML are well
known interpreted languages.

VI. PROGRAM TESTING AND DEBUGGING

The fact that a program is capable of producing results gives no guarantee that these answers
are correct. Before implementing the program on the user's system, it should be tested
thoroughly to ensure that it is running properly and giving desired results. This process of
validating a program is known as testing. If the program fails the test procedure, the reasons

visit www.dzplacide.overblog.com to discover many other learning tools for computer science & ICT
Topic: Introduction to programming 10 By DZEUGANG Placide

for this failure must be detected and removed. The process of detecting, isolating and correcting
bugs in a program is known as debugging.

VI.1 Types of errors

The errors in a program can be classified into two categories:

• Syntax Errors: errors due to the fact that the syntax of the language is not respected.
Syntax errors are the most common errors and, typically, involve incorrect punctuation,
incorrect word sequence, undefined terms or misuse of terms. These errors are easier to
correct because in case of such errors, programming languages provide a useful error
message, which gives an idea about what is wrong with the program.
• Logic errors are those errors that remain after all the semantic and syntax errors have
been removed. Usually, logic errors manifest themselves when the result the program
produces doesn't match the result your test data suggest it should produce. Logic errors
occur when you implement the algorithm for solving the problem incorrectly. The key
to fixing logic errors is to be able to reproduce the error consistently.
• A semantic error occurs when you obey the syntax rules of the language but are using
the statement out of context.

Those errors can be regrouped in two main categories

- Compile errors: errors which occurs during the compiling process


- Runtime errors: Error detecting during the execution of the program. They are
dynamic semantic errors, and logical errors, that cannot be detected by the compiler
(debugging).

VI.2 Testing Approaches


There exist two main testing approaches, black box and white box testing

a) Black Box Testing:

Black box testing, also known as functional testing is a testing technique where the internal
architecture of the item being tested is not necessarily known by the tester (the person
responsible for testing the program). The tester should only know about the inputs and the
expected outcomes. The ultimate goal of black box testing is to test every possible combination
and value of input. Thus, the tester and the programmer can be independent of one another,
avoiding programmer bias towards one's own work.

b) White Box Testing

White box testing, also known as glass box, structural, clear box and open box testing, is one
of the most popular testing approaches. Unlike black box testing, white box testing uses
specific knowledge of programming code to examine outputs. He must apply test cases and
various inputs in order to test branches, conditions, loops and the logical sequence of statements
being executed.

visit www.dzplacide.overblog.com to discover many other learning tools for computer science & ICT
Topic: Introduction to programming 11 By DZEUGANG Placide

VI.3 Debugging Approaches and concepts


The module responsible of debugging is called debugger. In general, three categories for
debugging approaches may be proposed. Brute force, Back tracking and Cause elimination

1) The brute force category of debugging is probably the most common and efficient
method for isolating the cause of a software error. Brute force debugging methods are
applied when all methods of debugging fail.
2) Backtracking is a common debugging approach that can be used successfully in small
programs. Beginning at the site where a symptom has been uncovered, the source code
is traced backward (manually) until the site of the cause is found.
3) Cause Elimination is manifested by induction or deduction and introduces the concept
of binary partitioning. Data related to the error occurrence are organized to isolate
potential causes.

Some techniques used to facilitate program debugging are:

- a breakpoint is an intentional stopping or pausing place in a program, put in place


for debugging purposes. It is also sometimes simply referred to as a pause.
A breakpoint makes your program stop whenever a certain point in the program is
reached.
- A watchpoint is a special breakpoint that stops your program when the value of an
expression changes.
- A catchpoint is another special breakpoint that stops your program when a certain kind
of event occurs, such as the throwing of a C++ exception or the loading of a library.

VII. PROGRAM DOCUMENTATION

The programming process is incomplete if it is not properly documented. Documentation


refers to all written materials that help in explaining the use and maintenance of a system or
program. Proper documentation enables the user to understand the program and acts as a
reference tool for programmers, in case modifications are required. It is easier to understand
the logic of a program, from the documented records rather than the code.

VI.1 Types of Documentation


The documentation is classified into two categories: For the user and for the developers

a) Documentation for the User

Usually, the end user has nothing to do with how the program was created, but he/she is
interested in how it can be used to his/her advantage. Some useful documenting techniques
from the user's point of view are as follows:

• System Manuals: A system manual contains the detailed technical information for
each application system. It describes each of the major functions performed by the

visit www.dzplacide.overblog.com to discover many other learning tools for computer science & ICT
Topic: Introduction to programming 12 By DZEUGANG Placide

application, the data affected by or used within the application and the logical flow of
data within the application, as well as details concerning all application input and
output.
• User Manuals: User manuals assist the person to understand the proper use of the
system. It provides detailed instructions concerning the procedure(s) to be followed to
enter data into the application for processing, as well as to request nonrecurring reports
and other information.

b) Documentation for Developers

Modifying an application, if it is a complex one, is not very easy especially if the modification
is to be done by a programmer other than the original one. In such cases, proper documentation
can reduce the burden of the programmers a great deal. Some useful documenting techniques
from the programmer's point of view are as follows:

• Comments: Comments are used within a program to assist the reader in understanding
the function or purpose of a single or multiple instructions. In C programming we use
// and /*…*/ to insert comment in the code.
• Programming Tools: Algorithms, flowcharts and pseudocodes are useful means of
program documentation. They help in making the program logic easier to understand.
In case, the program needs certain modification, the programmer can fall back on these
tools.
• Various Reports: Programming involves many steps. In all these steps, certain reports
are generated such as error reports, maintenance reports and testing reports.

VIII. OBJECT ORIENTED PROGRAMMING (OOP)

Object-oriented programming (OOP) is a type of computer programming, which promotes


building of independent pieces of code that interact with each other. Object-Oriented
Programming (OOP) is different from procedural programming languages (C, Pascal, etc.) in
several ways. Everything in OOP is grouped as "objects. OOP, defined in the purest sense, is
implemented by sending messages to objects. To understand this concept, we first need to know
what is an object?

VIII.1 Object
An object can be considered a "thing" that can perform a set of related activities. The set of
activities that the object performs defines the object's behavior. For example, the hand can grip
something or a Student (object) can give the name or address. In pure OOP terms an object is
an instance of a class.

VIII.2 Class

visit www.dzplacide.overblog.com to discover many other learning tools for computer science & ICT
Topic: Introduction to programming 13 By DZEUGANG Placide

A class is simply a representation of a type of object. A class is the blueprint from which the
individual objects are created. Class is composed of three things: a name, attributes, and
operations.

VIII.3 Abstraction and generalization


Abstraction is an emphasis on the idea, qualities and properties rather than the particulars (a
suppression of detail). The importance of abstraction is derived from its ability to hide
irrelevant details and from the use of names to reference objects. It places the emphasis on what
an object is or does rather than how it is represented or how it works.

While abstraction reduces complexity by hiding irrelevant detail, generalization reduces


complexity by replacing multiple entities which perform similar functions with a single
construct. Generalization is the broadening of application to encompass a larger domain of
objects of the same or different type.

VIII.4 Interface
the Interface separates the implementation and defines the structure. Interface can be used to
define a generic template and then one or more abstract classes to define partial
implementations of the interface.

VIII.5 Encapsulation
Encapsulation is a way of organizing data and methods into a structure by concealing the way
the object is implemented, i.e. preventing access to data by any means other than those
specified. Encapsulation therefore guarantees the integrity of the data contained in the object.

VIII.5 Polymorphism
Polymorphisms is a generic term that means 'many shapes'. More precisely
Polymorphisms means the ability to request that the same operations be performed by a wide
range of different types of things. In OOP the polymorphisms is achieved by using many
different techniques named method overloading, operator overloading, and method overriding,

visit www.dzplacide.overblog.com to discover many other learning tools for computer science & ICT
Topic: Introduction to programming 14 By DZEUGANG Placide

VIII.6 Inheritance
Inheritance is the process by which objects can acquire the properties of objects of other class.
In OOP, inheritance provides reusability, like, adding additional features to an existing class
without modifying it. This is achieved by deriving a new class from the existing one. The new
class will have combined features of both the classes.

visit www.dzplacide.overblog.com to discover many other learning tools for computer science & ICT

You might also like