CHAPTER ONE
INTRODUCTION TO COMPUTER PROGRAMMING
1.1 WHAT IS COMPUTER PROGRAMMING?
A program is a set of instructions that a computer or someone else follows to do some task. For
instance, when someone executes (put in action) some set of instructions, such as recipe, the person
is doing something, preparing food. The instructions must be written with a language that the
person can understand (using some human language). Similarly, computers execute instructions
of a program to do something, such as to add two numbers, to search data, etc. The list of
instructions followed by computers to transform data in to information is called a Computer
Program. Since the instructions followed by human beings are written with human language,
computer programs must be written with a language that the computer called Computer
Programming Language that the computer can understand. A Computer Programming
language is a set of rules that tells the computer what operations to do. It is a tool used by a
programmer to write a computer program. BASIC, FORTRAN, COBOL, PASCAL, C, C++, Java,
VISUAL BASIC. Hence, Computer Programming the activity of designing and writing
computer programs with an intention to accomplish tasks using computer.
1.2 REASONS TO STUDY PROGRAMMING
It is natural for students to wonder how they will benefit from the study of programming language
concepts. After all, many other topics in computer science are worthy of serious study. The
following is what we believe to be a compelling list of potential benefits of studying concepts of
programming languages:
Programming is about problem solving. Learning to code improves your problem-solving and
logical skills and teaches you how to efficiently solve a specific problem. You’ll quickly see that
there is a solution for everything and being able to write small or large programs will make things
(much!) easier. Spending less time on solving a complex and/or repetitive problem gives you more
time to learn new things, think outside the box, and to develop new and interesting (research)
questions.
Another reason to study programming is that it helps you understand computers. If you learn how
to write simple programs, you will gain more knowledge about how a computer works.
HUC 1
Furthermore, being able to code will also increase your chances on the job market. Understanding
what programs can do and how they can be deployed is more and more required in companies of
all sizes and sectors. Even if you might not be employed as a full-time programmer; having a
programming background helps you to improve communication with programmers.
And, last but not least, programming is so much fun! Writing programs can be extremely
creative, if one allows it to be.
1.3. TYPES AND USAGE OF COMPUTER LANGUAGES
Programming has gone through an evolution, which is derived by the need to make program
writing and maintenance simpler. There are three major levels, or types of programming
languages:
a. Machine Language: during the early ages of computers, computers needed to be
instructed using 0s and 1s. The first-generation programming language, which was
written as a string of 0sand 1s is called Machine language. Machine languages are
the only language the computer understands. Once the machine language is written,
the computer can directly execute it, hence itis the fastest type of computer program.
The problem with machine language is that, it is difficult for programmers to write
and maintain machine languages. Imaging instructing computers to read data,
programmers must express the command – read in terms of 0s and 1s. The other
difficulty of machine language is that it is machine dependent. A machine language
program written for one type of computer may not be executed on another type of
computers.
b. Assembly language (low level language): Since writing programs with machine
languages was difficult for programmers, they devised a translator called Assemblers,
and start to write computer programs with Assembly languages. Assembly language
is a symbolic representation of machine language, in that each machine language
instruction is represented by a symbol or abbreviation in assembly language. This
simplifies the task of program writing because programmers can easily remember
symbols, and abbreviations than sequences of 0s and 1s.
For example, which set of instructions below are easier to remember and use
for human beings?
HUC 2
Assembly language instructions Machine language instructions
MOV A, 47 1010 1111
ADD A, B 0011 0111
You can see that for human beings it is much easier to remember Assembly language
that Machine Languages because assembly language uses English like symbols for
instance ADD to add and MOV to move whereas machine language uses sequence of
0s and 1s to represent the two operation which makes it difficult for us to use and
remember.
Though assembly language seems easier for human beings to use than machine
languages, the computer cannot understand the symbolic instructions. As a result, the
assembly language instructions must first be translated in to machine language
instructions before they can be executed by computers. An assembler is a program
that translate a computer program written in assembly language to machine language.
Even if there were some improvements in simplifying the program writing task, it was
still difficult to remember all symbols and abbreviations of assembly languages.
Assembly languages also suffer similar problems with that of machine languages in
that, it was still easier to make mistakes when using assembly languages and they are
machine dependent (to mean a program written for one machine may not able to run
in another machine).
c. High Level Languages: The solution to the problems of machine language and
assembly language was to enable programmers write computer programs with a
language, which is close to human language, using familiar notations and words called
High level languages. Since High Level programming languages use more common
English language codes that are easier to remember and commonly used mathematical
representations and operations like ‘+’ to add , ‘-‘ to subtract etc, instructing
computer becomes as easy as instructing human beings.
Example
Assembly language instructions High Level language instructions
MOV A, 47 A = 47;
HUC 3
ADD A, B A = A + B;
Just like the case of assembly language, the computer cannot understand high level
language instructions so it must be translated in to machine language by the use of
compilers or interpreters.
✓ Compiler: is a program that converts the entire program (source code) of a
high-level language into machine code before the computer executes the
program.
✓ Interpreter: is a program that converts each high-level language statement
(line of code) into machine language, when needed to be executed
immediately, statement by statement or line by line.
✓ Compiled Vs Interpreted Computer Programming Languages
o Compiled Languages: Languages that use compiler include C, C++,
C#, BASIC, JAVA, COBOL, SWIFT,
o Interpreted Languages: Languages that use interpreter include PHP,
JavaScript, VBScript, Python, Perl etc
o Compiled Languages are faster than interpreted languages
1.4. Programming paradigms
A programming paradigm is a way of programming that recommends “preferred practices” and
discourages or makes impossible “risky practice.” There are various types of programming
paradigms. Here we will consider three of them.
1.4.1. Procedural programming
Procedural programming can also be referred to as imperative programming. It is a
programming paradigm based upon the concept of procedure calls, in which statements are
structured into procedures (also known as subroutines or functions). They are a list of
instructions to tell the computer what to do step by step, Procedural programming lang uages
are known as top-down languages. Most of the early programming languages are all procedural.
Features of Procedural Code
• Procedural Programming is excellent for general-purpose programming
• The coded simplicity along with ease of implementation of compilers and interpreters
• A large variety of books and online course material available on tested algorithms,
making it easier to learn.
HUC 4
• The source code is portable
• The code can be reused in different parts of the program, without the need to copy it
• The program flow can be tracked easily as it has a top-down approach.
1.4.2. Functional programming
Functional programming is a programming paradigm where you treat programming as an
evaluation of mathematical functions and you avoid changing-state and mutable data.
Functional programming consists only of PURE functions. Pure functions are those which take
an argument list as an input and whose output is a return value. Now you may feel that all
functions are pure as any function takes in values and returns a value.
For example, if a function relies on the global variable or class member’s data, then it is not
pure. And in such cases, the return value of that function is not entirely dependent on the list of
arguments received as input and can also have side effects. So, what do you understand by the
term side effect? A side effect is a change in the state of an application that is observable outside
the called function other than its return value. For example: Modifying any external variable
or object property such as a global variable, or a variable in the parent function scope chain.
Features of Functional Paradigm
• Pure functions –the output depends only on the input.
• Functions are First-Class and can be Higher-Order-A programming language is said
to have First-class functions when functions in that language are treated like any other
variable. For example, in such a language, a function can be passed as an argument to
other functions, can be returned by another function and can be assigned as a value to
a variable. Higher-order functions are functions that take at least one first-class function
as a parameter.
• Variables are Immutable-In functional programming you cannot modify a variable
after it has been initialized. You can create new variables and this helps to maintain state
throughout the runtime of a program.
1.4.3. Object-oriented Programming
In Object-oriented Programming, all real-world entities are represented by Classes. Objects are
instances of classes so each object encapsulates a state and behavior. State implies the fields,
HUC 5
attributes of the object and behavior is what you do with the state of the object and they are the
methods. Objects interact with each other by passing messages.
Features of OO:
• Encapsulation – This is a fundamental feature of Object-Oriented Programming. Here
you hide unnecessary details in classes and deliver a simple and clear interface for
working. It describes the idea of bundling data and methods that work on that data within
one unit. This concept is also often used to hide the internal representation, or state, of
an object from the outside
• Inheritance is a mechanism where you can derive a class from another class for a
hierarchy of classes that share a set of attributes and methods. It explains how the class
hierarchies develop code readability and support to the reuse of functionality.
• Polymorphism - Polymorphism is an object-oriented programming concept that refers
to the ability of a variable, function or object to take on multiple forms.
HUC 6