Unit 1 Introduction to Computer Programming
1.1 Definition of a computer program and programming language
Program:
Basic commands that instruct the computer system to do something are called instructions. An
organized list of instructions that, when executed, causes the computer to behave in a unction
predetermined manner is a program. A program is like a recipe. It contains a list of ingredients
(called variables) and a list of directions (called statements) that tell the computer what to do
vide a with the variables. The variables can represent numeric data, text or graphical images.
Without programs, computers are useless.
Programming Language: When we want communicate each other, we use our languages like
Nepali, English and so on. Likewise, if we want make computers work some tasks for us, we
need to give instructions of that the computer machine understands. Such type of language is
called a programming language. Therefore, programming language is a standardized
communication technique for describing instructions for a computer. Each programming
language has a set of syntactic rules used to define computer programs. A language enables a
programmer to precisely specify what data a computer is to act upon, how these data are to be
stored/transmitted and what actions are to be taken under various circumstances. Programming
languages are other kinds of software which enables us to develop different kinds of software.
Programming languages are classified mainly in two categories on the basis of creating
instructions: Low level languages and High-level languages
1.2 Types and Generation of Programming Languages
1. First Generation Programming Language: Machine-level Language
Machine language is a language that a computer actually understands. The least possible
level at which we can program a computer is in its own native machine code, consisting of
strings of 1's and 0's, and are stored as binary numbers. Thus, machine language is a
sequence of instructions written in the form of binary numbers consisting of 1's and 0's to
which the computer responds directly. The main advantage of machine language is that
they execute faster than a high-level language because computer understands it directly and
the code written in machine-level language is not needed to translate into other forms.
However, machine languages are more difficult to write.
Unit 1 Introduction to Computer Programming
Advantages of machine language
1. Computer uses specific processor's features like registers and hence execution of
code written in machine level language is faster and efficient which leads to less
execution time.
2. The CPU directly understands machine instructions, and hence translation into
other form is not required unlike in high level language.
2. Second Generation: Assembly Language
Second-generation languages, known as assembly languages, introduced human-readable
notations, such as mnemonics, that are easier to understand than binary code. These languages still
require conversion to machine code through an assembler before execution. Assembly language is
used primarily for writing operating systems and device drivers due to its close interaction with
hardware.
Advantages of Assembly Language
1. Writing program using assembly language is more convenient than machine language.
2. It improves understandability to users.
3. Embedded system and device programming would be better using assembly language
because it provides fewer codes than high level language.
3. Third Generation Language (3GL): High Level Language
Third-generation languages, also known as procedural languages, are high-level programming
languages designed to be more user-friendly by utilizing syntax similar to human language. This
makes it simpler for programmers to create and understand code. Some well-known examples are
C, C++, Java, FORTRAN, and PASCAL. Before execution, these languages must be converted
into machine code using a compiler or interpreter.
4. Fourth Generation (4GL): Very High-Level Language
Fourth-generation languages (4GL) are created to simplify programming by using syntax that is
more similar to everyday human language. These languages allow users to focus on specifying
what tasks need to be done, without needing to worry about the specific implementation details.
Unit 1 Introduction to Computer Programming
They are widely used for tasks such as database handling, generating reports, and building
graphical user interfaces (GUIs).
Some well-known examples are SQL, Python, Perl, Ruby, and MATLAB.
5. Fifth Generation (5GL):
Fifth-generation languages (5GL) represent the latest stage in programming evolution and focus
on visual programming and artificial intelligence. These languages use visual tools and constraint-
based logic to create programs. Instead of writing step-by-step instructions, the programmer
defines goals, and the system generates the code to achieve them.
Examples of fifth-generation languages are Prolog, OPS5, and Mercury.
1.3 Problem-solving using a computer
In our daily life, we may have a number of problems to be solved using computer. We use
computer to solve these problems efficiently.
1.3.1 Problem Analysis
Problem analysis is the process of becoming familiar with the problem that will be solved
with a computer program. Before solving a problem, it should be analyzed and understood
well.
The step of problem-solving methods are:
1. Problem Analysis
2. Algorithm Development
3. Flowcharting
4. Coding
5. Compilation and Execution
6. Debugging and Testing
7. Documentation
1.3.2 Algorithm and Flowchart
An algorithm is step by step description of the method to solve a problem. It is an effective
procedure for solving a problem in a finite number of steps. Algorithm maintains sequences
of computer instructions required to solve a problem in such a way that if the instructions
are executed in the specified sequence, the desired result is obtained. Developing an
algorithm is a step of program design.
Unit 1 Introduction to Computer Programming
An Algorithm is the step-by-step descriptions of the procedure written in human
understandable language for solving given problem.
Example:
[Link]
2. Read/Input length, breadth
3. Calculate area of rectangle A=l x b
4. Display result
5. Stop
Features of Algorithm
• Finiteness: An algorithm terminates after a fixed number of steps.
• Definiteness: Each step of an algorithm is precisely defined.
• Effectiveness: All operation use in algorithm are basic (division, multiplication,
compression, etc.) and can be performed exactly infixed duration.
• Input: Algorithm has certain precise input.
• Output: Algorithm has one or more output.
Flowcharting:
Flowchart is a graphical representation of an algorithm using standard symbols. In other words,
flowchart is a pictorial representation of an algorithm that uses boxes of different shapes to
denote different type of instructions. This is a problem-solving technique that is widely used.
Symbols that are used in flow chart.
1. START/STOP:
The oval shape represents start/stop of the program. Generally, contains words like START,
END or STOP.
2. Input/output:
The parallelogram represents the I/O function. This step is used to obtaining a number from an
input device and showing the results.
Unit 1 Introduction to Computer Programming
3. Connection:
Used to join different flowlines and to connect remote parts of the flowchart on the same
page.
4. Process: A rectangular represents processing operation. A process changes one or more
data.
5. Line or arrow: Line or arrows, represent processing operation direction. The flow of
control normally be up to down, right to left.
6. Decision making: The diamond represent a decision of switching type of option that
determine which one of alternative paths to be followed.
Advantages of Flowchart
• Communication
• Effective Analysis
• Proper Documentation
• Efficient Coding
• Easy in Debugging and program maintenance
Write a flowchart to find even and odd numbers.
Unit 1 Introduction to Computer Programming
1.3.3 Programming
Coding is the process of transferring paper works like algorithm and flowchart into
computer program following the well-defined syntaxes using the programming language
like C, C++, Java, etc.
1.3.4 Compilation, Linking and Execution
The process of changing high level language into machine level language is known as
compilation. It is done by special-software, known as compiler. The compilation process
tests the program whether it contains syntax errors or not. If syntax errors are present,
compiler can-not compile the code. Once the compilation (conversion into object program)
is completed then the program is linked with other object programs needed for execution,
thereby resulting in a binary program and then the program is loaded in the memory for the
purpose of execution and finally it is executed. During the execution, the program may ask
user for inputs and generates outputs after processing the inputs.
Figure: Compilation process
1.3.5 Debugging and Testing:
Debugging is the discovery and correction of programming errors. Even after taking full
care in program design and coding, some errors may remain in the program because the
designer/programmer might have never thought about a particular case. These errors may
appear during compilation or linking or execution of the program. When the error is
appeared, the debugging is necessary. Testing ensures that program performs correctly the
required task Programming theorists often refer to program debugging and testing as
verification and validation respectively. Verification ensures that the program does what
the programmer intends to do. Validation ensures that the program produces the correct
results for a set of test data. Thus, program testing and debugging are closely related. The
Unit 1 Introduction to Computer Programming
different tools like simulators. logic analyzers, breakpoints, trance routines, software
interrupts can be used for effective debugging process. For testing process, test data are
supplied to the program and output is observed. If the output is as expected, the program
can be considered error free. The sample of real data can also be used for testing the
program correctly.
Error: Error means failure of compiling and executing the program successfully and/or
getting incorrect results. The debugging and testing tasks are for avoiding errors in the
program. In the programming, errors are categorized into two as follows:
• Syntax Errors and
• Semantic Errors
With the printf( ) function, the string should be closed by the Quotation marks (“), however
programmer missed that one leading to compiler error “Unterminated string or character
constant”. This single error misleads the compiler generating additional errors like it did
not identify the closing of the function by small braces and so the error “Function call
missing)” is additionally generated. This type of error is simply categorized as syntax error.
Your first attempt of compiling a program won't be successful if you mistakenly typed the
program which may not be coded into properly defined syntax. Compiler will generate the
syntax error if the program code is not correctly written.
Sometimes, the program will successfully be compiled and executed but we are not getting
the correct result. After execution, we shall see the errors of like memory overflow, floating
point error etc. however, we are not getting the correct result even if the program is
executed without error. It is called runtime error. This happens due to the presence of error
in your programming logic. Due to the lack of extra knowledge of the problem domain,
you shall make mistakes in scientific rules and formula writing leading to incorrect results.
This type of error is categorized as semantic error or logical error.
1.3.6 Documentation
Program documentation is the description of program and its logic written to support
understanding of the program. Documentation of program helps to those who use, maintain
and extend the program in future. A program may be difficult-to-understand even to the
programmer who wrote the code after some days. If a program coded by one person is to
Unit 1 Introduction to Computer Programming
be maintained and extended by other person, there will be more difficult to understand it.
Properly documented program is necessary which will be useful and efficient in debugging,
testing, maintenance and redesign process. A properly documented program can easily be
used again when needed and an undocumented program usually requires much extra work.
Thus, good documentation simplifies maintenance and redesign and makes subsequent
programming tasks much simpler. Among the techniques commonly found in
documentation are flowcharts, comments, memory maps, and parameter & definition list.
There are two types of documentations:
A) Programmer’s Documentation (Technical Documentation)
Programmer's documentation is prepared for future reference to the programmers who
maintain, redesign and upgrade the system. It may contain detailed requirements of
program, format of reports & forms, logic of the program, data flow diagram, E-R
diagram, algorithm and flow chart of each component, input data and output data used
for testing and other references.
B) User Documentation (User Manual)
The user documentation provides support to the user of the program. This provides
instructions for installation of the program and use of it effectively.
Write an algorithm and draw flowchart for finding the sum of any two numbers.
Algorithm:
1. Start
2. Display “Enter two numbers”
3. Read A and B
4. C= A+B
5. Display “C as sum of two numbers”.
6. Stop
Unit 1 Introduction to Computer Programming
Flowchart:
Write an algorithm and draw flowchart to find the largest among three numbers.
Algorithm:
1. Start
2. Print “Enter three numbers”
3. Read three numbers: A, B, and C
4. If A>=B and A>=C, then print “A is greatest”.
5. If B>=A and B>=C then print “B is greatest” else print “C is greatest”.
6. Stop
Unit 1 Introduction to Computer Programming
Flowchart: