Chapter 1 Introduction to Computer Programming Concepts
1.1 Introduction
Definitions
1) Computer program
A computer program is a sequence of instructions written using a Computer Programming
Language to perform a specified task by the computer.
The two important terms that have been used in the above definition are −
a) Sequence of instructions
b) Computer Programming Language
2) Computer Programming
Computer programming is the act of writing computer programs, which are a sequence of
instructions written using a Computer Programming Language to perform a specified task by the
computer.
Programming involves the following activities; writing a program, compiling the program, running
the program, debugging the programs. The whole process is repeated until the program is finished.
3) Computer
A computer is an electronic device capable of executing instructions, developed based on
algorithms stored in its memory, to process data fed to it and produce the required results faster
than human beings.
It is an electronic device which processes given data to derive the required and useful
information. During the processing the computer has to perform various functions like,
Accepting Instructions & data from the user, performing various arithmetic and Logical
operations as per Instructions given and presenting the Information or Output to the user.
4) Computer Programmer
Someone who can write computer programs or in other words, someone who can do computer
programming
Programmers translate the solutions or tasks into a language the computer can understand.
5) Programing Language
A programming language is a formal constructed language designed to communicate
instructions to a machine, particularly a computer.
A computer programming language is a language used to write computer programs, which
involves a computer performing some kind of computation
1.2. Program/ Programming Concepts
1) A computer program is also called a computer software, which can range from two lines to millions of
lines of instructions.
2) Computer program instructions are also called program source code and computer programming is
also called program coding.
3) A computer without a computer program is just a dump box; it is programs that make computers active.
4) Computer Programming is the process of developing and implementing various sets of instructions to
enable a computer to do a certain task.
5) Programs are written to solve problems or perform tasks on a computer.
6) The programs characterize what a computer actually does.
7) A program (independently of the language in which it is written) is constituted by two fundamental
parts:
Intro to Programming & Algorithms Notes Page 2
a) a representation of the information (data) relative to the domain of interest: objects
b) a description of how to manipulate the representation in such a way as to realize the desired
functionality: operations
To write a program both aspects have to be addressed.
1.3. Computer Basic Concepts
Computer is an electronic device that accepts data, processes it, and generates the relevant output.
A computer is an electronic device capable of executing instructions, developed based on algorithms
stored in its memory, to process data fed to it and produce the required results faster than human
beings.
It is an electronic device which processes given data to derive the required and useful information.
During the processing the computer has to perform various functions like, Accepting Instructions &
data from the user, performing various arithmetic and Logical operations as per Instructions given and
presenting the Information or Output to the user.
It can perform both simple and complex tasks with very high speed and accuracy. However, a computer
cannot perform any task—simple or complex, of its own. Computers need to be instructed about “how” the
task is to be performed. The set of instructions that instruct the computer about the way the task is to be
performed is called a program. A program is required for processing all kind of tasks—simple tasks like
addition of two numbers, and complex tasks like gaming etc.
1.3.1. Characteristics of Computers
Computers are so versatile that they have become indispensable to engineers, scientists, business executives,
managers, administrators, accountants, teachers and students. Modern computers posses certain
characteristics and abilities peculiar to them.
1) Perform complex and repetitive calculations rapidly and accurately.
2) Store large amounts of data and information for subsequent manipulation.
3) Compare items and make decisions.
4) Provide information to the users in many different forms
5) Automatically correct or modify the parameters of a system under control
6) Converse with the users interactively
7) Receive and display audio and video signals
8) Speed – a computer is a very fast machine. It can perform in a very few seconds the amount of work that
a human being can do in a year if he/she worked day and night doing nothing else.
9) Accuracy – the computer accuracy is consistently high.
10) Diligence – computers are free from monotony, tiredness and lack of concentration etc. It can therefore
work for hours without creating an error.
11) Versatility – a computer performs various tasks with ease. The computer perform three basic operations
a) It is capable to access and accept information through various input-output devices from the user.
b) It performs basic Arithmetic and Logic operations on data as desired.
c) It is capable to generate the desired output in the desired form.
12) Power of remembering – a computer can store and recall any information due to its secondary storage
capability.
13) No intelligence Quotient (IQ) – a computer cannot make its own decisions and has to be instructed on
what to do.
14) No feelings – computers are devoid of emotions. They have no feelings or instincts and none possesses
the equivalent of a human heart and soul.
Intro to Programming & Algorithms Notes Page 3
1.3.2. Limitation of Computer
1) Computer does not work on itself, it requires set of instructions to be provided, else computer
(Hardware) is waste.
2) Computer are not intelligent, they have to be instructed about each and every step which they have to
perform
3) Computers cannot take decisions on its own, one has to program the computer to take an action if some
conditional prevail.
4) Computers, unlike humans cannot learn by experience.
1.3.3. Advantages and Disadvantages of Computers
Advantages - Application Areas. Computers can be used in the following application areas
1) Data processing (Commercial use).
2) Numerical computing (Scientific use).
3) Text (word) processing (Office and Education)
4) Message communication (E-mails)
5) Image processing (Animation and industrial use).
6) Voice recognition (Multimedia)
Disadvantages
1) Unemployment
2) Computer Crime
3) Data Security
4) Violation of Privacy
5) Waste of Time and Energy
6) Health Risks
7) Impact on Environment
1.3.4. Components of a Computer
A computer has to main components;
a) Hardware
b) Software
Computer hardware refers to the physical components of a computer.
1) Input: devices used to capture and send data and instructions to the processing component / unit of the
computer
2) Storage: used to store the data and instructions until needed to be worked on. Also used to hold the
processed information until required for output. There are two types of storage; main memory(storage)
and secondary storage
3) Control: used to fetch instructions from the main storage, interprets them, and issues the necessary
signals to the components making the computer system. It directs all hardware operations necessary in
obeying instructions.
4) Processing: processing is done by the Arithmetic Logical Unit (ALU) which obeys the instructions and
performs the required arithmetic operations, and logical operations
5) Output: devices used to output the processed data (information). This includes, screen, printer etc.
Intro to Programming & Algorithms Notes Page 4
Main Memory
Computer software: A set of programs associated with the operation of a computer
1) System Software
2) Application Software
1.4. Program Development Life Cycle/Problem Solving Process
Problem solving is an innovative process for finding solutions to problems. Problem formulation is the
methodology of describing the problem and the results by stating the requirements and objectives that are
required to solve the problem using a computer program. The objectives are defined in terms of:
1) Input and its characteristics
2) Expected Output and its characteristics
3) Relationship between the input & the desired output
There are a number of concepts of relevance to problem solving using computers. Two particular concepts
includes
Intro to Programming & Algorithms Notes Page 5
1) Computability: A problem is said to be co+
2) mputable if it can in principle be performed by a machine. Some mathematical functions are not
computable.
3) Complexity: The complexity of a problem is measured in terms of resources required, time and
storage
As stated earlier, a program is needed to instruct the computer about the way a task is to be performed. The
instructions in a program have three essential parts:
1) Instructions to accept the input data that needs to be processed,
2) Instructions that will act upon the input data and process it, and
3) Instructions to provide the output to user
The instructions in a program are defined in a specific sequence. Writing a computer program is not a
straightforward task. A person who writes the program (computer programmer) has to follow the Program
Development Life Cycle.
1.4.1. Life cycle steps / phases
1) Problem Analysis - The programmer first understands the problem to be solved. Define the Problem -
State in the clearest possible terms the problem you wish to solve. It is impossible to write a computer
program to solve a problem that has been ambiguously or imprecisely stated.
The programmer determines the various ways in which the problem can be solved, and decides upon
a single solution which will be followed to solve the problem.
2) Program Design / Algorithm creation- The selected solution is represented in a form, so that it can
be coded. This requires three steps:
1) An algorithm is written, which is an English-like explanation of the solution.
2) A flowchart is drawn, which is a diagrammatic representation of the solution. The solution is
represented diagrammatically, for easy understanding and clarity.
3) A pseudo code is written for the selected solution. Pseudo code uses the structured
programming constructs. The pseudo code becomes an input to the next phase.
3) Program Development / Coding
The steps in an algorithm, translated into a series of instructions to the computer, comprise the computer
program. There are many languages in which computer programs can be coded, each with its own
syntax, vocabulary, and special features.
a) Choose the programming language
b) Write the program by converting the Pseudo-Code using the programming language
c) Compile the Program to remove Syntax errors: Syntax errors arise due to the incorrect use of
programming language or due to the grammatical errors with respect to the programming
language used
4) Debug the Program: Most programs of any length don't work properly the first time they are run
and must therefore be debugged. The coded pseudo code or program is compiled for any syntax
errors. Removal of the errors and any ambiguities in the code is called debugging
5) Run the Program (Execute): After the program has been fully debugged you run it, possibly using
many sets of input data. This step may take anywhere from a few seconds to many hours depending on
the complexity of the problem and the speed of the computer.
6) Analyze the Results: Often the output from a computer program requires considerable further analysis.
In some cases, even though the program worked perfectly, you may find that you solved the “wrong"
problem. There is an acronym well known to computer users: GIGO, which stands for “garbage in,
garbage out."
Intro to Programming & Algorithms Notes Page 6
a. If incorrect results are displayed, then the program has semantic error (logical error). The
semantic errors are removed from the program to get the correct results.
b. The successfully compiled program is now ready for execution.
c. The successfully tested program is ready for use and is installed on the user’s machine.
7) Program Documentation and Maintenance—The program is properly documented, so that later on,
anyone can use it and understand its working. Any changes made to the program, after installation,
forms part of the maintenance of program. The program may require updating, fixing of errors etc.
during the maintenance phase.
Intro to Programming & Algorithms Notes Page 7
Chapter 2 Program Algorithm
2.1 Introduction
An algorithm is any well-defined computational procedure that takes some value, or set of values, as
input and produces some value, or set of values, as output. An algorithm is thus a sequence of
computational steps that transform the input into the output.
An Algorithm is a logical sequence of discrete steps that describe a complete solution to a given problem
in a finite amount of time independently of the software or hardware of the computer. It is the set of
rules that define how a particular problem can be solved in finite number of steps
An algorithm can be viewed as a tool for solving a well-specified computational problem. The statement of
the problem specifies in general terms the desired input/output relationship. The algorithm describes a
specific computational procedure for achieving that input/output relationship. Algorithms are very essential
as they instructs the computer what specific steps it needs to perform to carry out a particular task or solve a
problem.
2.2. Characteristics of an Algorithm
Every algorithm should have the following five characteristics:
1) Input; takes in some value(s) as input
2) Output; produces some value(s) as output
3) Definiteness / non ambiguity: It must be precise and unambiguous. the statements must be interpretable
in a unique way by whom is executing them
4) Executability / Effectiveness: It must be possible to execute each statement and it must give the correct
solution in all cases
5) Termination (finiteness): It must eventually end. The execution of the algorithm must terminate in a
finite amount of time for each possible set of input data
2.3. Efficiency and Analysis of the Algorithm
1) Efficiency: The efficiency of an Algorithm means how fast it can produce the correct results for the
given problem. The Algorithm efficiency depends upon its time complexity and space complexity. The
complexity of an algorithm is a function that provides the running time and space for data, depending on
the size provided by us. Two important factors for judging the complexity of an Algorithm are;
a) Space complexity which refers to the amount of memory required by the algorithm for it execution
and generation of the final output.
b) Time Complexity which refers to the amount of computer time required by an algorithm for its
execution, which includes both the compile time and run time.
The compile time of an algorithm does not depend on the instance characteristics of the algorithm. The
run time of an algorithm is estimated by determining the number of various operation, such as addition,
subtraction, multiplication, division, load and store executed by it.
2) Algorithm Analysis: The analysis of an algorithm determines the amount of resources, such as time and
space required by it for its execution. Generally, the algorithms are formulated to work with the inputs or
arbitrary length. Algorithm analysis provides theoretical estimates required by an algorithm to solve a
problem.
2.4. Designing Algorithms
Algorithm design is a specific method to create a mathematical process in problem solving processes. There
are many ways to design algorithms.
1) Divide and conquer.
a) Divide the problem into a number of sub-problems.
Intro to Programming & Algorithms Notes Page 8
b) Conquer the sub-problems by solving them recursively. If the sub-problem sizes are small enough,
however, just solve the sub-problems in a straightforward manner.
c) Combine the solutions to the sub-problems into the solution for the original problem.
2) Dynamic programming: is a method for solving a complex problem by breaking it down into a
collection of simpler sub-problems, solving each of those sub-problems just once, and storing their
solutions. Dynamic programming algorithms are often used for optimization. A dynamic programming
algorithm will examine the previously solved sub-problems and will combine their solutions to give the
best solution for the given problem.
3) Greedy algorithm: is an algorithmic paradigm that follows the problem solving heuristic of making the
locally optimal choice at each stage with the hope of finding a global optimum.
In general, greedy algorithms have five components:
a) A candidate set, from which a solution is created
b) A selection function, which chooses the best candidate to be added to the solution
c) A feasibility function, that is used to determine if a candidate can be used to contribute to a solution
d) An objective function, which assigns a value to a solution, or a partial solution, and
e) A solution function, which will indicate when we have discovered a complete solution
Greedy algorithms produce good solutions on some mathematical problems, but not on others.
4) Back Tracking. is a general algorithm for finding all (or some) solutions to some computation al
problems, notably constraints satisfaction problems, that incrementally builds candidates to the
solutions, and abandons each partial candidate ("backtracks") as soon as it determines that the candidate
cannot possibly be completed to a valid solution.
Backtracking is an important tool for solving constraint satisfaction problems, such as crossword, verbal
arithmetic, Sudoku and many other puzzles.
5) Brute Force: A general problem-solving technique that consists of systematically enumerating all
possible candidates for the solution and checking whether each candidate satisfies the problem's
statement.
2.5. Steps in development of Algorithms
1) Problem definition
2) Development of a model
3) Specification of Algorithm
4) Designing an Algorithm
5) Checking the correctness of Algorithm
6) Analysis of Algorithm
7) Implementation of Algorithm
8) Program testing
9) Documentation Preparation
2.6. Program Control Structures
Programming procedures no matter how complex may be reduced to a combination of control structures.
Control structures specify the statements to be executed and the order of execution of statements. There are
three kinds of control structures:
1) Sequential—instructions are executed in linear order
2) Selection (branch or conditional) - The logic of the program may require execution of a statement based
on a decision. A true/false question determines the selection of the next instruction based on the answer
3) Iterative (loop) – a block of instructions may need to be repetitively executed until a certain condition is
met.
Intro to Programming & Algorithms Notes Page 9
2.7. Algorithm Representation
The steps of an Algorithm, they can be presented using Flow charts and pseudo-codes.
2.7.1 Flow Charts
A flowchart is a diagrammatic representation of the logic for solving a task. A flow chart is a traditional
means of showing in diagrammatic form, the sequence of steps to be undertaken in solving a problem.
Flowcharts or flow diagrams are important tools in writing a computer program. A flowchart allows you to
plan the sequence of steps in a program before writing it. The flowchart serves as a visual representation
which many programmers find indispensable in planning any program of at least moderate complexity.
A flowchart is drawn using boxes of different shapes with lines connecting them to show the flow of control.
The purpose of drawing a flowchart is to make the logic of the program clearer in a visual form.
2.7.1.1. Symbols / Elements of a Flowchart.
A flowchart consists of a set of boxes, the shapes of which indicate specific operations. The separate boxes
are connected with arrows to show the sequences in which the various operations are performed. The most
common symbols that are used to draw a flowchart are—Process, Decision, Data, Terminator, Connector
and Flow lines
A flowchart may be simple or complex. While drawing a flowchart, some rules need to be followed
1) A flowchart should have a start and end,
2) The direction of flow in a flowchart must be from top to bottom and left to right, and
3) The relevant symbols must be used while drawing a flowchart.
Shape Name Description
Rectangle Process symbol: Used to represent any kind of processing
activity. Details are written in the box
Calculate wages
Diamond: The decision Symbol: Used where a decision has to be
made in selecting the subsequent path to be followed.
Is the transaction
a credit?
Used to show the flow/ path of ma sequence of symbols.
Vertical line without arrow head are assumes t flow top
to bottom.
Horizontal lines without arrow heads are assumed to
flow left to right.
Every operation box must have at least one incoming or
outgoing arrow. Any arrow leaving a decision box must
be labeled with the decision result which will cause that
path to be followed.
Input/output symbol: Used where data input is to be
Intro to Programming & Algorithms Notes Page 10
Parallelogram performed
The Terminal symbol: Used as the first or last symbol in a
Oval program or separately drawn program module
Start
Stop
Connector symbol:
Small Circle
Exit to or entry from another part of the chart
Or
Used to add explanatory notes or description,
2.7.1.2. Representing the Program Control Structures
While preparing the flowchart, the sequence, selection or iterative structures may be used wherever required.
The Flow charts for the control structures are as follows;
Sequence: In a sequence, the steps are executed in linear order one after the other.
Selection / Branching: the step to be executed next is based on a decision taken. If the condition is true
(yes) a different path is followed than if the condition evaluates to false (no).
Interation / looping. In case of iterative operation, a condition is checked. Based upon the result of this
conditional check, true or false, different paths are followed. Either the next step in the sequence is
executed or the control goes back to one of the already executed steps to make a loop.
Intro to Programming & Algorithms Notes Page 11
Examples
1) The first flowchart computes the product of any two numbers and gives the result. The flowchart is a
simple sequence of steps to be performed in a sequential order.
2) The second flowchart compares three numbers and finds the maximum of the three numbers. This
flowchart uses selection. In this flowchart, decision is taken based upon a condition, which decides the
next path to be followed, i.e. If A is greater than B then the true (Yes) path is followed else the false
(No) path is followed. Another decision is again made while comparing MAX with C.
3) The third flowchart finds the sum of first 100 integers. Here, iteration (loop) is performed so that some
steps are executed repetitively until they fulfill some condition to exit from the repetition. In the decision
box, the value of I is compared with 100. If it is false (No), a loop is created which breaks when the
condition becomes true (Yes).
2.7.1.3. Limitations of program flowcharts
1) Not easily translated into programming language.
2) A complex and long flowchart may run into multiple pages, which becomes difficult to understand and
follow.
3) Updating a flowchart with the changing requirements is a challenging job.
2.7.2. Pseudo code
Pseudo code consists of short, readable and formally-styled English language used for explaining an
algorithm.
Intro to Programming & Algorithms Notes Page 12
Pseudo code is halfway between English and programming language and is based upon a few
simple grammatical construction which avoid the ambiguities of English but which can be easily
converted into computer programming language.
Pseudo code is a short-hand way of describing a computer program. It is an informal high-level description
of a computer programming algorithm, which omits details that are not essential for human understanding of
the algorithm. Using pseudo code, it is easier for a programmer or a non-programmer to understand the
general working of the program, since it is not based on any programming language. It is used to give a
sketch of the structure of the program, before the actual coding. It uses the structured constructs of the
programming language but is not machine-readable.
2.7.2.1 Representing the Control Structures
Examples
1. Finding the Product of two numbers
2. Finding Maximum of any Three numbers
Intro to Programming & Algorithms Notes Page 13
3. Finding of first 100 numbers
2.7.2.2. Limitations of Pseudo Code
1) Pseudo code cannot be compiled or executed.
2) No standard for the syntax of pseudo code exists.
For writing the pseudo code, the programmer is not required to know the programming language in which
the pseudo code will be implemented later.
2.8. Representing Program Control Structures using Flow Chart & Pseudo code
Control Structure Pseudo code Flow Chart
st
Sequence: 1 Instruction
In the absence of selection, or
repetition, program statements 2nd Instruction
are executed in the sequence in
which the appear in the program 3rd Instruction
Selection IF
Part of decision making and condition Yes
No
allows alternative actions to be THEN
?
taken according to the conditions actions
that exist at particular stages in ELSE
program execution actions
ENDIF
Or
Intro to Programming & Algorithms Notes Page 14
CASE
a). Actions ?
b). Actions
c). Actions
d). Actions
ENDCASE a b c
Repetition also called “looping”
There are many programming WHILE
problems in which the same condition
sequence of statements needs to DO
be performed again and again for Actions
a definite or indefinite number ENDWHILE
of times
REPEAT
actions
UNTIL
condition
Exercises
Develop the Algorithm and represent using both Flow Chart and Pseudo code that would solve the following
problem
1. A Construction company which hires casual works intends to computerize their wages calculation
process. The company wages payment policy is as follows;
a) The company has a 5 days working week.
b) Men are paid Ksh 250 per day while women are paid Ksh 200 per day
c) Extra day is paid at Ksh 50 more than the Normal working day.
2. Develop a program to calculate the area and perimeter of a Square.
3. A Bank intends to automate the calculation of bonuses for account holders at the end of every year. The
Bank’s policy is to give a 2% bonus on the ending balance of all accounts at the end of the year. A 5%
bonus is also given to all Female account holders if the balance is more than 50,000.
Intro to Programming & Algorithms Notes Page 15
Chapter 3: Programming Languages
3.1. Introduction
What is a Programming Language
There are many definitions of what constitutes a programming language, and none of these is the ‘correct’
answer. Programming languages are needed to allow human beings and computers to talk to each other.
Computers, as yet, are unable to understand our everyday language or, in fact, the way we talk about the
world. Computers understand logic expressed mathematically through what is known as machine code.
Computer language consists of 1s and 0s or the binary system, which the majority of human beings would
find very difficult to communicate in. Computer languages enable humans to write in a form that is more
compatible with a human system of communication. This is then translated into a form that the computer can
understand. Here are some different ideas on what constitutes a programming language.
1) A programming language has been defined as a tool to help the programmer.
2) A way of writing that can be read by both a human being and a machine.
3) A sequence of instructions for the machine to carry out.
4) A way for a human being to communicate with a machine that is unable to understand natural
language.
5) A computer language offers a means of writing algorithms that can be understood by both human
being and machine. Machines are unable to understand natural language, so a human being uses
algorithms that are translated into machine code by the programming language. Machine code is
difficult for humans to use, so a language ‘translates’ human readable language into machine
readable form.
6) A computer program offers humans a standard way of expressing algorithms to solve particular
problems. As languages offer a convention it allows other humans to read the program, and change
it if they need to.
3.2. Categories of Programming Languages
There are three categories / levels of programming languages;
1) Machine language (low level language)
2) Assembly (or symbolic) language
3) Procedure-oriented language (high level language)
3.2.1. Machine language
The lowest-level programming language (except for computers that utilize programmable microcode)
Machine languages are the only languages understood by computers. While easily understood by computers,
machine languages are almost impossible for humans to use because they consist entirely of numbers. It is a
programming language in which the instructions are in a form that allows the computer to perform them
immediately, without any further translation being required. Instructions are in the form of a Binary code
also called machine code and are called machine instructions.
3.2.2. Assembly Language
Introduced in 1950s, reduced programming complexity and provided some standardization to build and
applications. The 1 and 0 in machine language are replaced by with abbreviations or mnemonic code. It
consists of a series of instructions and mnemonics that correspond to a stream of executable instructions. It is
converted into machine code with the help of an assembler. Common features includes;
1) Mnemonic code; used in place of the operation code part of the instruction eg SUB for substract,
which are fairly easy to remember
Intro to Programming & Algorithms Notes Page 16
2) Symbolic Addresses which are used in place of actual machine addresses. A programmer can choose
a symbol and use it consistently to refer to one particular item of data. Example FNO to represent
First No.
3) The symbolically written program has to be translated into machine language before being used
operationally. A 1 to 1 translation to machine language, ie one symbolic instruction produces one
machine instruction/code.
Advantages of Assembly language over machine language
1) It is easy to locate and identify syntax errors, thus it is easy to debug it.
2) It is easier to develop a computer application using assembly language in comparison with machine
language
3) Assembly language operates very efficiently.
3.2.3. High level language
High level language is portable across different machine types (architectures); The machine independence of
the high level languages means that in principle it should be possible to make the same high-level language
run on different machines. It reflects the type of problem solved rather than the features of the machine.
High level languages are more abstract, easier to use and more portable across platforms as compared to
low-level programming languages. Source programs are written in statements akin to English. A high level
language code is executed by translating it into the corresponding machine language code with the help of a
compiler or interpreter. High level languages can be classified into the following categories;
1) Procedure-oriented languages (third generation)
2) Problem-oriented languages (fourth generation)
3) Natural languages (fifth generation).
Procedure languages.
High-level languages designed to solve general-purpose problems, example BASIC, COBOL, FORTRAN,
C, C++ and JAVA. They are designed to express the logic and procedure of a problem. Though the syntax of
the languages may be different, they use English-like commands that are easy to follow. They are portable.
Problem-Oriented Languages
Problem-oriented languages also known as Fourth Generation Languages (4GL) are used to solve specific
problems and includes query languages, report generators and Application generators which have simple
English like syntax rules. The 4GLs have reduced programming efforts and overall cost of software
development. They use either visual environment or a text environment for program development similar to
that of third-generation languages. A single statement of the 4GL can perform the same task as multiple line
of a third-generation language. It allows a program to just drag and drop from the toolbar, to create various
items like buttons, text boxes, label etc. A program can quickly create a prototype of the software
applications
Natural Languages
Natural languages widely known as fifth generation languages, are designed to make a computer to behave
like an expert and solve problems. The programmer just needs to specify the problem and the constraints for
problem solving. Natural languages such as LISP and PROLOG are mainly used to develop artificial
intelligence and expert systems.
Intro to Programming & Algorithms Notes Page 17
3.3. Features of high level language
Extensive vocabulary of words, symbols and sentences
Whole sentences are translated into many machine codes instructions
Portable across different machine types (architectures)
Libraries of macros and sub-routines can be incorporated
As they are problem oriented, the programmer is able to work at least to some extent independently of the
machine.
Have a set of rules that must be obeyed.
Syntax: the structure of the statements and the grammatical rules governing them. Grammatical rules
that govern the way in which words, symbols, expressions and statements may be formed and
combined.
Semantics: the meaning of the statements written in the language. The rules that governs its
meaning. – what happens when the program is executed/run most are standardized by ISO/ANSI to
provide an official description of the language
3.4. High Level language Translation
High level languages need to be translated into machine language which is the computer language. The
translation is referred to as the Compilation Processes and is done by a
1) Compiler or
2) Interpreter
3.4.1. Compiler
A compiler is a manufacturer specifically written computer program which translates (or compiles) a source
code computer program that translates the source code written in a high level language into the
corresponding object code of the low level language.
Characteristics of a Compiler:
1) Translates the source program code into machine code
2) Includes linkages for closed sub-routine
3) Allocates areas of main storage
4) Produces the object program.
5) Produces a printed copy (listing) of the source code and object code
6) Produces a list of errors found during compilation.
Characteristics of Compiler compilation process
1) The entire high level source code / program is converted into the executable machine code file prior
to the object program being loaded into main memory and executed.
2) Translation done only ones and the object program can be loaded into the main storage and
executed.
A program that translates a low-level language into a high level language is called a Decompiler.
Compiled languages includes C, C++, COBOL, FORTRAN etc.
Compilers are classified into single-Pass compilers and Multi-pass compilers. Single-pass compilers are
generally faster than multi-pas compilers, but multi-pass compilers are required to generate high quality
code
3.4.2. Interpreter:
The interpreter is a translation program that converts each high-level language statement into the
corresponding machine code. The translation process is carried out just before the program statement is
Intro to Programming & Algorithms Notes Page 18
executed. Instead of the entire program, one program statement at a time is translated and executed
immediately. When using an interpreter, the source code translated every time the program is executed
The commonly interpreted languages include BASIC and PERL. Though interpreters are easier to create as
compared to compilers, the compiled languages can be executed more efficiently and are faster. Interpreters
are appropriate in;
1) Handling user commands in an interactive system
2) Debugging programs as they run (removing program faults).
3) Handling software produced for or by a different computer.
3.5. Computer Program Compilation Process
A computer program compilation process involves the following five stages;
1. Lexical analysis: the source program is transformed into tokens. During the transformation all
whitespaces and comments are discarded. All character sequences that do not form valid tokens are
discarded and an error message is generated.
2. Syntactical analysis: analysis to ensure the program syntax is appropriate to the language. Failure results
in an error message been generated.
3. Type / Semantic checking: responsible for ensuring that the compile-time semantic rules of the language
are enforced. An error message is generated if the semantic rules are violated
4. Code optimization: improves the intermediate code based on the target machine architecture
5. Code generation: target machine code is generated.
The first three stages are concerned with finding and reporting errors to the programmer, while the last two
are concerned with generating efficient machine code to run on the targeted computer.
Lexical Syntaticical Type / Code Code
Source Analysis Analysis Semantic Optimization Generation
Code Analysis Machine
Code
3.6. Evaluating Programming Languages
Programming languages can be evaluated from a number of viewpoints/criteria, depending on either the
programmer, the environment in which the programmer works or the standards of the organization. When
developing software, a programmer should consider which language is most suitable to the task, rather than
relying on a language with which they are familiar. All the features of the language need to be considered
rather than just one particular feature. A wrong choice can mean that the software has to be re-written, which
can be very frustrating and time consuming.
There are a number of different ways that the programmer can think about the design of the system, from the
top-down of structured programming to object oriented design issues. Some languages are geared towards
one particular style of design, whilst others incorporate many types. Each of these language paradigms
enables the programmer to consider the problem from a different viewpoint. There are a few basic questions
that can be asked to help when making these decisions:
1. How readable is the language, to humans? If parts of the program are going to be read or altered
separately from the entire program is might be worth considering how legible they are going to be. It is
also useful to consider the length of names allowed in the language. Statements such as GO TO, FOR,
WHILE and LOOP have increased the readability of programs, and lead to neater programs. These
statements also affect the syntax or grammar.
Intro to Programming & Algorithms Notes Page 19
2. When it comes to writing the program, how easy is it to write the program in this particular language?
A programming language that is easy to write in can make the process easier and faster. It may help to
reduce mistakes. FOR loops and other types of statement allow the programmer to write much simpler
code. This will save time and money, and also make the program smaller.
3. How reliable is the language? Not all languages create robust programs, and some help the programmer
to avoid making errors. A program that is not robust can cause errors, and code can ‘decay’. Any
language that helps the programmer to avoid mistakes will make it easier to use.
4. How much would it cost to develop using a given language? Is the language expensive to use and to
maintain? Programs may need to be updated or redeveloped, and an expensive language may make this
prohibitive.
5. How complicated is the syntax going to be? Syntax is an important consideration. Clarity and ease of
understanding are important, as is a syntax that seems logical and sensible. Errors are very likely to
occur where one area of syntax too closely resembles another, and the program may prove difficult to
debug. Some theorists reason that if it is difficult to write a program to parse the language, then it
follows that it will be problematical for the programmer to get it right.
6. Does the language have standards? Languages that have standards for writing programs have greater
readability; for instance Java has standards for naming, commenting and capitalization.
3.7. The Programming Language Generations
The language generations span many decades, and begin with the development of machine code. Each
generation adds new features and capabilities for the programmer to use. Languages are designed to create
programs of a particular type, or to deal with particular problems. Modern languages have led to the
development of completely different styles of programming involving the use of more human-like or natural
language and re-usable pieces of code.
1) The first generation of languages was machine language. Instructions and addresses were numerical.
These programs were linked to the machine they were developed on.
2) The second generation allowed symbolic instructions and addresses. The program was translated by an
assembler. Languages of this generation include IBM, BAL, and VAX Macro. These languages were
still dependent on the machine they were developed on.
3) Third generation languages allowed the programmer to concentrate on the problem rather than the
machine they were writing for. Other innovations included structured programming and database
management systems. 3GL languages include FORTRAN, COBOL, Pascal, Ada, C, and BASIC. All
3GL languages are much easier for the human being to understand.
4) 4GL languages (fourth generation). These are known as non-procedural, they concentrate on what you
want to do rather than how you are going to do it. 4GL languages include SQL, Postscript, and relational
database orientated languages.
5) 5GL (fifth generation). These languages did not appear until the 1990s, and have primarily been
concerned with Artificial Intelligence and Fuzzy Logic. The programs that have been developed in these
languages have explored Natural Language (making the computer seem to communicate like a human
being).
3.8 Common Programming Language Descriptions
The development histories of some languages are interesting, as are their general capabilities. Here are
descriptions of the most common languages in use today.
1) Ada
Ada is a general-purpose, high-level programming language based on Pascal. It was developed under the
aegis of the Department of Defense and is especially well suited to real-time and embedded systems.
Ada emphasizes data abstraction and information hiding and forces you to differentiate between the
public and private parts of each class and package. “Ada” was chosen as the name of the language in
Intro to Programming & Algorithms Notes Page 20
honor of Ada Lovelace, a mathematician who is considered to have been the world’s first programmer.
Today, Ada is used primarily in military, space, and avionics systems.
2) Assembly Language
Assembly language, or “assembler,” is a kind of low-level language in which each statement
corresponds to a single machine instruction. Because the statements use specific machine instructions,
an assembly language is specific to a particular processor. Assembler is regarded as the second
generation language. Most programmers avoid it unless they’re pushing the limits in execution speed or
code size.
3) C
C is a general-purpose, mid-level language that was originally associated with the UNIX operating
system. C has some high-level language features, such as structured data, structured control flow,
machine independence, and a rich set of operators. It has also been called a “portable assembly
language” because it makes extensive use of pointers and addresses, has some low-level constructs such
as bit manipulation, and is weakly typed.
C was developed in the 1970s at Bell Labs. It was originally designed for and used on the DEC PDP-
11—whose operating system, C compiler, and UNIX application programs were all written in C. In
1988, an ANSI standard was issued to codify C, which was revised in 1999. C was the de facto standard
for microcomputer and workstation programming in the 1980s and 1990s.
4) C++
C++, an object-oriented language founded on C, was developed at Bell Laboratories in the 1980s. In
addition to being compatible with C, C++ provides classes, polymorphism, exception handling,
templates, and it provides more robust type checking than C does. It also provides an extensive and
powerful standard library.
5) C#
C# is a general-purpose, object-oriented language and programming environment developed by
Microsoft with syntax similar to C, C++, and Java, and it provides extensive tools that aid development
on Microsoft platforms.
6) Cobol
Cobol is an English-like programming language that was originally developed in 1959–1961 for use by
the Department of Defense. Cobol is used primarily for business applications and is still one of the most
widely used languages today, second only to Visual Basic in popularity (Feiman and Driver 2002).
Cobol has been updated over the years to include mathematical functions and object-oriented
capabilities. The acronym “Cobol” stands for COmmon Business-Oriented Language.
7) Fortran
Fortran was the first high-level computer language, introducing the ideas of variables and high-level
loops. “Fortran” stands for FORmula TRANslation. Fortran was originally developed in the 1950s and
has seen several significant revisions, including Fortran 77 in 1977, which added block-structured if-
then-else statements and character string manipulations. Fortran 90 added user-defined data types,
pointers, classes, and a rich set of operations on arrays. Fortran is used mainly in scientific and
engineering applications.
8) Java
Java is an object-oriented language with syntax similar to C and C++ that was developed by Sun
Microsystems, Inc. Java was designed to run on any platform by converting Java source code to byte
code, which is then run in each platform within an environment known as a virtual machine. Java is in
widespread use for programming Web applications.
9) JavaScript
Intro to Programming & Algorithms Notes Page 21
JavaScript is an interpreted scripting language that was originally loosely related to Java. It is used
primarily for client-side programming such as adding simple functions and online applications to Web
pages.
10) Perl
Perl is a string-handling language that is based on C and several UNIX utilities. Perl is often used for
system administration tasks, such as creating build scripts, as well as for report generation and
processing. It’s also used to create Web applications such as Slashdot. The acronym “Perl” stands for
Practical Extraction and Report Language.
11) PHP
PHP is an open-source scripting language with a simple syntax similar to Perl, Bourne Shell, JavaScript,
and C. PHP runs on all major operating systems to execute server side interactive functions. It can be
embedded in Web pages to access and present database information. The acronym “PHP” originally
stood for Personal Home Page but now stands for PHP: Hypertext Processor.
12) Python
Python is an interpreted, interactive, object-oriented language that runs in numerous environments. It is
used most commonly for writing scripts and small Web applications and also contains some support for
creating larger programs.
13) SQL
SQL is the de facto standard language for querying, updating, and managing relational databases. “SQL”
stands for Structured Query Language. Unlike other languages listed in this section, SQL is a
“declarative language,” meaning that it does not define a sequence of operations, but rather the result of
some operations.
14) Visual Basic
The original version of Basic was a high-level language developed at Dartmouth College in the 1960s.
The acronym BASIC stands for Beginner’s All-purpose Symbolic
Top Requested Programming Languages in Computer Occupations
Programming Language Type
1 SQL Database Language
2 JAVA Programming Language
3 ORACLE Database Language
4 C# Programming Language
5 JAVA-SCRIPT Programming Language
6 PYTHON Programming Language
7 JQUERY Programming Language
8 C++ Programming Language
9 Git Programming Language
10 PERL Programming Language
11 POWERSHELL Programming Language
Chapter Review Questions
1. Describe the General Program compilation process
2. What criteria should a programmer use to evaluate a programming language
3. What are advantages of High-level languages over the Assembly language.
4. Describe the categories of the high-level languages
Intro to Programming & Algorithms Notes Page 22
Chapter 4: - Programming Methodologies
4.1 Introduction
Programming Methodology can be defines as the approach to analyzing complex problems, planning for
software development and controlling the development process. The programming methodologies /
paradigms may differ in terms of the basic idea which relates to the program computation. There are many
types of programming methodologies prevalent among software developers.
4.2. Categories of Programming Methodologies
The Categories includes;
1) Structured Programming
2) Object Oriented Programing
3) Aspect-Oriented Programming (AOP)
Alternative
1) Imperative: the emphasis is on the operations intended as actions/commands/instructions that change
the state of the computation; the objects are functional to the computation;
2) Functional: the emphasis is on the operations intended as functions that compute results; the objects
are functional to the computation;
3) object oriented: the emphasis is on the objects, which as a whole represent the domain of interest;
the operations are functional to the representation.
4.2.1. Structured Programming
Structured programming involves building of programs using small modules. The modules are easy to read
and write. In structured programming, the problem to be solved is broken down into small tasks that can be
written independently. Once written, the small tasks are combined together to form the complete task.
In structured programming, the approach to develop the software is process-centric or procedural. The
software is divided into procedures or modules, based on the overall functionality of the software. As a
result, the procedures and modules become tightly interwoven and interdependent. Thus, they are not re-
usable. C, COBOL and Pascal are examples of structured programming languages.
a) Procedural Programming: Problem is broken down into procedures, or blocks of code that perform
one task each. All procedures taken together form the whole program. It is suitable only for small
programs that have low level of complexity. Example − For a calculator program that does addition,
subtraction, multiplication, division, square root and comparison, each of these operations can be
developed as separate procedures. In the main program each procedure would be invoked on the
basis of user’s choice.
b) Functional Programming: Here the problem, or the desired solution, is broken down into functional
units. Each unit performs its own task and is self-sufficient. These units are then stitched together to
form the complete solution. Example − A payroll processing can have functional units like
employee data maintenance, basic salary calculation, gross salary calculation, leave processing, loan
repayment processing, etc.
c) Logical Programming: Here the problem is broken down into logical units rather than functional
units. Example: In a school management system, users have very defined roles like class teacher,
subject teacher, lab assistant, coordinator, academic in-charge, etc. So the software can be divided
into units depending on user roles. Each user can have different interface, permissions, etc.
4.2.2. Object-Oriented Programming (OOP),
OOP focuses on developing the software based on their component objects. In OOP the software is broken
into components not based on their functionality, but based on the components or parts of the software. Each
Intro to Programming & Algorithms Notes Page 23
component consists of data and the methods that operate on the data. The components interact with each
other to provide the functionality of the software. The components are complete by themselves and are re-
usable. Object-oriented programming (OOP) is a programming language model organized around "objects"
rather than "actions" and data rather than logic. The terms that are commonly associated with object-oriented
programming are as follows:
1) Class is the basic building block in object-oriented programming. A class consists of data attributes
and methods that operate on the data defined in the class.
2) Object is a runtime instance of the class. An object has a state, defined behavior and a unique identity.
The state of the object is represented by the data defined in the class. The methods defined in the class
represent object behavior. A class is a template for a set of objects that share common data attributes
and common behavior.
3) Abstraction, Encapsulation, Inheritance and Polymorphism are the unique features of object-oriented
software.
4) Abstraction allows dealing with the complexity of the object. Abstraction allows picking out the
relevant details of the object, and ignoring the non-essential details. Encapsulation is a way of
implementing abstraction.
5) Encapsulation means information hiding. The encapsulation feature of object-oriented software hides
the data defined in the class. Encapsulation separates implementation of the class from its interface.
The interaction with the class is through the interface provided by the set of methods defined in the
class. This separation of interface from its implementation allows changes to be made in the class
without affecting its interface.
6) The Inheritance feature of object-oriented software allows a new class, called the derived class, to be
derived from an already existing class known as the base class. The derived class (subclass) inherits
all data and methods of the base class (super class). It may override some or all of the data and
methods of the base class or add its own new data and methods.
7) Polymorphism means, many forms. It refers to an entity changing its form depending on the
circumstances. It allows different objects to respond to the same message in different ways. This
feature increases the flexibility of the program by allowing the appropriate method to be invoked
depending on the object executing the method invocation call.
C++ and Java are object-oriented programming languages.
4.2.3. Aspect-Oriented Programming (AOP)
Aspect-oriented programming is a new programming paradigm that handles the crosscutting concerns of the
software. The crosscutting concerns are the global concerns like logging, authentication, security,
performance, etc., that do not fit into a single module or related modules. In OOP, the business logic or core
concern is encapsulated in well-defined classes. However, the code for implementing crosscutting concerns
is intertwined with a number of related classes or modules and gets scattered in the different classes of the
software.
AspectJ and AspectC are examples of aspect-oriented programming languages.
4.3 Approaches to Programming
Software developers may choose one or a combination of more than one of these methodologies to develop a
software. After having selected a suitable programming methodology / paradigm for the program to be
written, the coding of the logic of a program has to be done in a computer programming language. For the
purposes of coding, the programmer checks the requirements and suitability of the task, and selects from
among the programming languages available for the selected programming paradigm.
Intro to Programming & Algorithms Notes Page 24
For each of the methodologies discussed, problem has to be broken down into smaller units. To do this,
developers use any of the following two approaches
1) Top-Down Approach
2) Bottom-up Approach
4.3.1. Top-down approach
A top-down approach (is also known as step-wise design) is essentially the breaking down of a system to
gain insight into its compositional sub-systems. In a top-down approach an overview of the system is
formulated, specifying but not detailing any first-level subsystems. Each subsystem is then refined in yet
greater detail, sometimes in many additional subsystem levels, until the entire specification is reduced to
base elements. A top-down model is often specified with the assistance of "black boxes", these make it
easier to manipulate. However, black boxes may fail to elucidate elementary mechanisms or be detailed
enough to realistically validate the model.
Top-down-design starts with a description of the overall system and usually consists of a hierarchical
structure which contains more detailed descriptions of the system at each lower level. The lower level design
details continue until further subdivision is no longer possible, i.e., until the system is described in terms of
its "atomic" parts. This method involves a hierarchical or tree-like structure for a system as illustrated by the
following diagram:
At the top level, we have that part of the system which deals with the overall system; a kind of system
overview or main top-level module.
Top down programming method process
1) Define exactly what data the program will get and what it has to do with them.
2) If the task is simple enough, write the program code.
3) Otherwise, split the task into smaller parts and define exactly the duty of each part and interface to the
rest of the program.
4) Repeat the steps 1–4 separately for each subtask.
Advantages of the Top-Down Design Method
1) It is easier to comprehend the solution of a smaller and less complicated problem than to grasp the
solution of a large and complex problem. Separating the low level work from the higher level
abstractions leads to a modular design. Modular design means development can be self contained. Much
less time consuming (each programmer is only involved in a part of the big project).
2) It is easier to test segments of solutions, rather than the entire solution at once. This method allows one
to test the solution of each sub-problem separately until the entire solution has been tested.
Intro to Programming & Algorithms Notes Page 25
3) It is often possible to simplify the logical steps of each sub-problem, so that when taken as a whole, the
entire solution has less complex logic and hence easier to develop. A simplified solution takes less time
to develop and will be more readable.
4) The program will be easier to maintain. If errors occur in the output it is easy to identify the errors
generated from each of the modules / sub-programs of the entire program.
4.3.2. Bottom-up approach
This approach is the piecing together of systems to give rise to grander systems, thus making the original
systems sub-systems of the emergent system. In a bottom-up approach the individual base elements of the
system are first specified in great detail. These elements are then linked together to form larger subsystems,
which then in turn are linked, sometimes in many levels, until a complete top-level system is formed.
Bottom-up emphasizes coding and early testing, which can begin as soon as the first module has been
specified. This approach, however, runs the risk that modules may be coded without having a clear idea of
how they link to other parts of the system, and that such linking may not be as easy as first thought. Re-
usability of code is one of the main benefits of the bottom-up approach
4.4. Programming Aims and Program Characteristics
Good programming principles and practice aim at producing a program with the following characteristics;
1) Reliability: the program can be depended upon always to do what it is supposed to do
2) Maintainability: the program will be easy to change or modify when the need arises
3) Portability: the program will be transferable to a different computer with a minimum modification.
4) Readability: the program will be easy for a programmer to read and understand.
5) Performance: the program causes the tasks to be done quickly and efficiently.
6) Storage saving: the program is not allowed to be unnecessarily long
7) User friendly − Program interface, clickable links and icons, etc. should be user friendly.
8) Structural: To develop a program, the task must be broken down into a number of subtasks. These
subtasks are developed independently, and each subtask is able to perform the assigned job without the
help of any other subtask. If a program is developed structurally, it becomes more readable, and the
testing and documentation process also gets easier.
9) Efficiency: As the processing power and memory are the most precious resources of a computer, a
program should be laid out in such a manner that it utilizes the least amount of memory and processing
time.
10) Flexibility: A program should be flexible enough to handle most of the changes without having to
rewrite the entire program.
11) Machine Independence- Program should be machine independent. Program written on one system
should be able to execute on any other without any changes.
12) Cost Effectiveness- Cost Effectiveness is the key to measure the program quality. Cost must be
measured over the life of the program and must include both cost and human cost of producing these
programs.
13) Generality: Apart from flexibility, the program should also be general. Generality means that if a
program is developed for a particular task, then it should also be used for all similar tasks of the same
domain.
14) Documentation: Documentation is one of the most important components of an application
development. Even if a program is developed following the best programming practices, it will be
rendered useless if the end user is not able to fully utilize the functionality of the application. A well-
documented application is also useful for other programmers because even in the absence of the author,
they can understand it.
Intro to Programming & Algorithms Notes Page 26
15) The program should not have hard-coded input values. This implies that it should not be written to work
for a particular input value, but must be a general program (also called generic program) that accepts
input from the user.
A program comprising of the above features is generally characterized as a good program.
4.5. Program Instructions
A computer can do many things, but at a very basic level, programs consists of the following types of
instructions:
1) Input: get data from the keyboard or from some file or other device
2) Output: write
3) Math: perform some simple calculation, like addition and multiplication
4) Logical operations: compare values to make decisions
5) Conditional execution: check some condition and execute one sequence of statements or another
6) Repetition: repeat some sequence of operations, usually with some variation
4.6. Program Errors
There are three types of errors that occur in programs:
1) Syntax errors: these occur when the program is not written according to the rules of the language.
2) Runtime errors: these occur with programs that, even though they are syntactically correct, they might
execute instructions that violate some other rules of the language or some other set condition.
3) Semantic errors: these are the cases in which a program might run without generating error messages,
but it will not do the right thing.
Intro to Programming & Algorithms Notes Page 27