Fundamentals of programming
Bahir Dar University
College of Science
Chapter 1
Introduction to Programming
Programming language
What is programming?
Programming is a skill that can be acquired by a computer professional that gives him the
knowledge of making the computer perform the required operation or task.
Computer programming is critical if one wants to know how to make the computer perform a
task. Most users of a computer only use the available applications on the computer. These
applications are produced by computer programmers. Thus if someone is interested to make such
kind of applications, he/she needs to learn how to talk to the computer, which is learning computer
programming.
Why do we need to learn computer programming?
To improve our ability to develop effective algorithms and increase your capacity to
express ideas (both programming and in general)
Increase your ability to select a PL for a specific use.
Increase your ability to learn new languages.
Provide you with a better understanding of implementation issues in programming.
Increase your ability to design a new PL.
Provide you with a better understanding of computational theory.
What is programming language?
Programming Language is a set of different category of written symbols that instruct computer
hardware to perform specified operations required by the designer. It is simply a computer
language used to write computer applications/programs/.
1
Fundamentals of programming
What skills do we need to be a programmer?
For someone to be a programmer, in addition to basic skills of computer, he/she needs to have the
following major skills:
Programming Language Skill: knowing one or more programming language to talk to the
computer and instruct the machine to perform a task.
Problem Solving Skill: skills on how to solve real world problem and represent the solution
in understandable format.
Algorithm Development: skill of coming up with sequence of simple and human
understandable set of instructions showing the step of solving the problem. Those set of steps
should not be dependent on any programming language or machine.
In every programming language there are sets of rules that govern the symbols used in a
programming language. These set of rules determine how the programmer can make the computer
hardware perform a specific operation. These sets of rules are called syntax.
Generations of programming language
st nd rd th th
Programming languages are categorized into five generations: (1 , 2 , 3 , 4 and 5
generation languages)
These programming languages can also be categorized into two broad categories: low level
and high level languages.
Low level languages are machine specific or dependent.
High level languages like COBOL, BASIC,C++, Java, C#/C Sharp/ etc are machine
independent and can run on variety of computers.
From the five categories of programming languages, first and second generation languages are
low level languages and the rest are high level programming languages.
The higher the level of a language, the easier it is to understand and use by programmers.
Languages after the forth generation are refereed to as a very high level languages.
1. First Generation (Machine languages, 1940’s)
Difficult to write applications with.
Dependent on machine /computer/ being used.
Machine languages allow the programmer to interact directly with the hardware, and it can be
executed by the computer without the need for a translator.
2
Fundamentals of programming
Is more powerful in utilizing resources of the computer.
Gives power to the programmer.
They execute very quickly and use memory very efficiently.
2. Second Generation (Assembly languages, early 1950’s)
Uses symbolic names for operations and storage locations.
A system program called an assembler translates a program written in assembly language to
machine language.
Programs written in assembly language are not portable. i.e., different computer architectures
have their own machine and assembly languages.
They are highly used in system software development. Eg. Assembly language.
3. Third Generation (High level languages, 1950’s to 1970’s)
Uses English like instructions and mathematicians were able to define variables with
statements such as Z = A + B.
Such languages are much easier to use than assembly language.
Programs written in high level languages need to be translated into machine language in order
to be executed.
The use of common words (reserved words) within instructions makes them easier to learn.
All third generation programming languages are procedural languages.
In procedural languages, the programmer is expected to specify what is required and how to
perform it.
First introduced in the late 1950s, FORTRAN, ALGOL, and COBOL are early examples of
this sort of language.
Most popular general-purpose languages today, such as C, C++, C#, Java, and Python, are also
third-generation languages.
4. Fourth Generation (since late 1970’s)
Have a simple, English like syntax rules; commonly used to access databases.
Forth generation languages are non-procedural languages.
The non-procedural method is easier to write, but you have less control over how each task is
actually performed.
3
Fundamentals of programming
In non-procedural languages, the programmer is not required to write traditional programming
logic. Programmers concentrate on defining the input and output rather than the program steps
required.
For example, a command, such as LIST, might display all the records in a file on screen,
separating fields with a blank space. In a procedural language, all the logic for inputting each
record, testing for end of file and formatting each column on screen has to be explicitly
programmed.
Forth generation languages have a minimum number of syntax rules. This saves time and free
professional programmers for more complex tasks.
Some examples of 4GL are structured query languages (SQL), report generators, application
generators and graphics languages.
5. Fifth Generation (1990’s)
These are used in artificial intelligence (AI) and expert systems; also used for accessing
databases.
5GLs are “natural” languages whose instruction closely resembles human speech. E.g. “get me
Jone Brown’s sales figure for the 1997 financial year”.
5GLs require very powerful hardware and software because of the complexity involved in
interpreting commands in human language.
Prolog can be an example.
Major Programming Paradigms (types of programming language)
The major landmarks in the programming world are the different kinds of features or properties
observed in the development of programming languages. Among these, the following are worth
mentioning: Procedural, Structured and Object Oriented Programming Paradigms.
1. Procedural Programming
Procedural programming is a programming paradigm based upon the concept of procedure
call. Procedural programming is often a better choice than simple sequential programming in
many situations which involve moderate complexity or which require significant ease of
maintainability.
Possible benefits: the ability to re-use the same code (function or procedure) at different
places, an easier way to keep track of program flow than a collection of “GO TO” or “JUMP”
statements.
4
Fundamentals of programming
2. Structured Programming
Process of writing a program in small, independent parts. This makes it easier to control a
program's development and to design and test its individual component parts.
Structured programs are built up from units called modules, which normally correspond to
single procedures or functions.
Can be seen as a subset or sub discipline of procedural programming. It is most famous for
removing or reducing reliance on the GO TO statement.
3. Object-Oriented Programming
The idea behind OOP is that, a computer program is composed of a collection of individual
units, or objects as opposed to traditional view in which a program is a list of instructions to
the computer.
Object-oriented programming is claimed to give more flexibility, easing changes to programs.
The OOP approach is often simpler to develop and maintain.
Basic Program development tips
The program we design in any programming language need to be:
Reliable: the program should always do what it is expected to do and handle all types of
exception.
Maintainable: the program should be in a way that it could be modified and upgraded when
the need arises.
Portable: It needs to be possible to adapt the software written for one type of computer to
another with minimum modification.
Efficient: the program should be designed to make optimal use of time, space and other
resources of the computer.
Ways of stating Algorithms
What is algorithm?
Algorithm is a finite set of well-defined instructions for accomplishing some task which, given
an initial state, will terminate in a corresponding recognizable end-state.
5
Fundamentals of programming
A digital computer is a useful tool for solving a great variety of problems. A solution to a
problem is called an algorithm; it describes the sequence of steps to be performed for the
problem to be solved.
The algorithm should be:
Precise and unambiguous
Simple
Correct
Efficient
Modeling a programs logic using flow chart and pseudocodes
Algorithm could be designed using many techniques and tools. One tool of designing
algorithm is by using flowcharts. Flowchart is a graphical way of expressing the steps needed
to solve a problem.
A flow chart is a schematic (diagrammatic description) representation of a process.
Basic flowcharting symbols are:
Terminal point
Decision
Process
Input/output
Flow line
Inter-page connector
On-page connector
Pseudocode
6
Fundamentals of programming
Pseudo means false so pseudocode is a false code intended for human reading rather than
machine reading.
Pseudocode is a compact and informal high-level description of a computer programming
algorithm that uses the structural conventions of a programming language
Pseudocode typically omits details that are not essential for human understanding of the
algorithm, such as variable declarations, system-specific code and subroutines.
The purpose of using pseudo code is that it is easier for humans to understand than
conventional programming language code, that is a compact and environment-independent
description of the key principles of an algorithm.
No standard for pseudocode syntax exists, as a program in pseudocode is not an executable
program but it must be clear.
Eg1. Write a psedocode and flowchart that calculate payments of a worker with per time?
Flow Chart Pseudocode
Insert the hours
Start Insert the rate
Pay=hours *rate
Print pay
Input hours
End
Input rate
Pay=hours*rate
Begin Print pay
End
7
Fundamentals of programming
Eg2. Write a pseudocode and flowchart that calculate the product of two numbers?
Flow Chart
Start
Pseudocode
Begin
Input x
Insert the value of x
Insert the value of y
Input y Sum=x *y
Print sum
Product=x*y End
Output Product
End
Eg3. Write a psedocode and flowchart that calculate the average of three numbers?
Flow Chart
Start
Pseudocode
Begin
Input x, Input y,
Input z Insert the value of x
Insert the value of y
Sum=x + y +z Insert the value of Z
Sum=x + y + z
Avg=sum/3
Avg=sum/3
Print Avg
Print Avg
End
End 8
Fundamentals of programming
Eg4. Write a psedocode and flowchart that calculate overtime payments of the worker. If
the working time of the worker greater that 40 hrs he/she will get an additional payment
with (rate*1.5)?
Flow Chart Pseudocode
Begin
Start Input hours, rate
If hours ≤ 40 then
Pay = hours * rate
Input
hours,rate Else
Pay = 40 * rate + (hours – 40) * rate * 1.5
Print pay
End
hours No
<=40
Yes
Pay=hours*rate
Pay=40*rate+ (hours-
40)*rate*1.5
Print pay
End
Eg5. Your heart rate is the number of times your heart beats each minuets usually about
70 /min for adults (more than 25 years old) and 80/min for children’s(less than 25 years
old) design a program that ask a user to enter his/her approximate age (First ask the
number of years and then the number of months) the program you design should
9
Fundamentals of programming
display the approximate number of times the person heart has bit?(hint a year
considered to have 365 and a month have 30 days)
Flow Chart
Pseudocode
Start
Input the number Begin
of YRS
Input the number of years (No YRS)
Input the Number of Months (No MTH)
If No YRS > 25 then
Heart beat(HB)= ((No YRS*365*24*60)+(No
Input the number
MTH*30*24*60))*70
of MTH
Else
Heart beat(HB)= ((No YRS*365*24*60)+(No
MTH*30*24*60))*80
Print Heart beat(HB)
End
No YRS No
>25
Yes
HB= ((No
YRS*365*24*60)+(No
MTH*30*24*60))*70
HB= ((No
YRS*365*24*60)+(No
MTH*30*24*60))*80
Print HB
End
10
Fundamentals of programming
Exercise
For each of the problems below, develop a flow chart and Psedocode
1. Obtain two numbers from the keyboard, and determine and display which (if either) is
the larger of the two numbers.
2. Add the numbers from 1 to 100 and display the sum.
3. Find the average of two numbers given by the user.
4. Find the area of a circle where the radius is provided by the user.
5. Read an integer value from the keyboard and display a message indicating if this
number is odd or even.
6. A company wants to give all employs a bonus of 10% of their salary. For employs who
have been working in the company more than 14 years would set an additional 5000
birr, design a program that accomplish the specification?
11