Professional Documents
Culture Documents
UNIT 1
Introduction to
Programming
1
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
LESSON 1:
Basic Programming Concepts
OBJECTIVES:At the end of the lesson, students will be
able to: ▪ Explain what a program is.
▪ Differentiate the categories of programming languages
2
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
Introduction
Think about some various ways that people use computers. In school, students
use computers for tasks such as writing papers, searching for articles, sending email,
and participating in online classes. At work, individuals use PCs to analyze data, make
presentations, conduct business transactions, communicate with colleagues and
customers, control machines in manufacturing facilities, and do numerous different
things. At home, individuals use PCs for different tasks such as paying bills, shopping
online, communicating with friends and family, and of course playing games.
Figure 1. shows screens from two commonly used programs, Microsoft Word, and
Adobe Photoshop. Microsoft Word is a word processing program that allows you to
create, edit, and print documents with your computer. Adobe Photoshop is an image
editing program that allows you to work with graphic images, such as photos taken
with your digital camera.
What is Computer
Programming?
A computer program is a
set
of instructions written in a
programming language that a
computer can execute.
Programs
are designed to perform specific
tasks. These programs are written
by programmers refers to the
person who designs or creates,
codes, tests, debugs and
documents a computer programs.
Figure 1.1. A Computer Programming
Clipart Source: https://www.freecodecamp.org/
3
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
Computer Programming
The stages of programming include design, or making decision about what the
program should accomplish; and coding which refers to the use of a programming
language to express the program’s logic in computer-readable form.
What we take for granted is that communication with a human being is different
than communicating with a computer. A human has prior knowledge and life
experience, they know where to find the tea, they know that the milk is stored in a
fridge or where it is kept.
Each programming language has its own set of words and rules, called the syntax of
that language. If you're going to write a program, you have to follow the syntax of the
language you're using, otherwise you won't be understood.
Machine Language
Source: https://dfarq.homeip.net/
4
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
Example:
A Programmer must have additional knowledge about the architecture of the particular
machine, before programming in machine language. Developing programs using
machine language is tedious job. Since, it is very difficult to remember sequence of
binaries for different computer architectures. Therefore, nowadays it is not much in
practice.
▪ Programs developed using low level languages are fast and memory efficient. ▪
Programmers can utilize processor and memory in better way using a low-level
language.
▪ There is no need of any compiler or interpreters to translate the source to
machine code. Thus, cuts the compilation and interpretation time. ▪ Low level
languages provide direct manipulation of computer registers and storage.
▪ It can directly communicate with hardware devices.
▪ Programs developed using low level languages are machine dependent and are
not portable.
▪ It is difficult to develop, debug and maintain.
▪ Low level programs are more error prone.
▪ Low level programming usually results in poor programming productivity. ▪
Programmer must have additional knowledge of the computer architecture of
particular machine, for programming in low level language.
5
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
Advantages of High-level language
High Level Language
▪ High level languages are programmer friendly. They are easy to write, debug
and maintain.
▪ It provides higher level of abstraction from machine languages. ▪
It is machine independent language.
▪ Easy to learn.
▪ Less error prone, easy to find and debug errors.
▪ High level programming results in better programming productivity.
High-level languages, such as *COBOL, VISUAL BASIC, JAVA etc. are some
of the popular languages used by the programmers. These are called high-level
languages to distinguish them from the machine and symbolic languages. They are
also called compiler languages, since these languages require a special program
called a compiler, which translates programs written in a particular language into a
machine language. When these languages are used, a compilation run is required
before data can be processed. The original program that is written in a high-level
language is called the source program, and its translation in machine language is
called object program.
6
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
Defense. The language is named in honor of
Countess Ada Lovelace, an influential and
historical figure in the field of computing.
7
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
applications quickly. VB was originally created in
the early 1990s.
Assembly Language
Now we could say that high level languages are a lot easier to understand over the
low level languages. Let us take a look on the example below:
Let us say we want the value of a memory cell to be equal to number fifteen (15). In
high level language, we could simply accomplish this by writing: int x = 15;
where:
int (based from the dictionary word integer is a reserved word used to identify the data
type of a number equal (=) sign ( a generally accepted mathematical symbol) is used
to denote an equation process, 15 is simply the decimal value of the number we
wanted to be in memory cell, x is any valid variable representing the computer memory
cell.
MOV is an instruction telling the processor to equate the value of the computer
memory (base memory – Bx) to OOFh in hexadecimal or IIII in binary (decimal
equivalent of 15)
Mnemonics are short abbreviated English words used to specify a computer instruction.
Each instruction in binary has a specific mnemonic. They are architecture dependent and there
is a list of separate mnemonics for different computer architectures.
8
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
▪ It takes a lot of time and effort to write the code for the same.
▪ It is very complex and difficult to understand.
▪ The syntax is difficult to remember.
▪ It has a lack of portability of program between different computer architectures. ▪
It needs more size or memory of the computer to run the long programs written in
Assembly Language
Compilers and Interpreters
As we learned in the previous section, computer understands program only if it
is coded in its machine language. In this section, we explain the procedure for turning
a program written in high level language into machine language. Because the
computer can understands only machine language instructions, programs that are
written in a high-level language must be translated into machine language. Once a
program has been written in a high-level language, the programmer will use a compiler
or an interpreter to make the translation.
There are generally two types of programming languages: compiled languages and
interpreted languages. A compiler is needed to translate a program written in a
compiled language into machine-understandable code (that is, binary code) before you
can run the program on your machine. When the translation is done, the binary code
can be saved into an application file. You can keep running the application file without
the compiler unless the program (source code) is updated and you have to recompile
it. The binary code or application file is also called executable code (or an executable
file).
9
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
It is the job of the programmer to write and test the program. There are four
steps in this process: (1) writing and editing the program, (2) compiling the
program, (3) linking the program with the required library modules, and (4)
executing the program.
This is the process of creating and modifying source code - the name given to
the program instructions you write. Some C compilers come with a specific editor that
can provide a lot of assistance in managing your programs. In fact, an editor often
provides a complete environment for writing, managing, developing and testing your
programs. This is sometimes called an integrated development environment, or
IDE.
You can also use other editors to create your source files, but they must store the code
as plaintext without any extra formatting data embedded in it. In general, if you have a
compiler system with an editor included, it will provide a lot of features that make it
easier to write and organize your source programs. There will usually be automatic
facilities for laying out the program text appropriately, and color highlighting for
important language elements, which not only makes your code more readable but also
provides a clear indicator when you make errors when keying in such words.
On a PC you could use one of the many freeware and shareware programming
editors. These will often provide a lot of help in ensuring your code is correct with
syntax
10
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
highlighting and auto indenting of your code. Do not use a word processor such as
Microsoft Word, as these are not suitable for producing program code because of the
extra formatting information they store along with the text. Of course, you also have
the option of purchasing one of the professionally created programming development
environments that support C, such as those from Borland or Microsoft, in which case
you will have very extensive editing capabilities. Before parting with your cash though,
it is a good idea to check that the level of C that is supported is approximate to the
current C standard.
✓ Compiling Programs
The compiler converts your source code into machine language and detects and
reports errors in the compilation process. The input to this stage is the file you produce
during your editing, which is usually referred to as a source file.
The compiler can detect a wide range of errors that are due to invalid or
unrecognized program code, as well as structural errors where, for example, part of a
program can never be executed. The output from the compiler is known as object
code and is stored in files called object files, which usually have names with the
extension .obj in the Microsoft Windows environment, The result of a successful
compilation is a file with the same name as that used for the source file, but with the
.o or .obj extension.
✓ Linking Programs
The linker combines the various modules generated by the compiler from
source code files, adds required code modules from program libraries supplied as part
of C, and welds everything into an executable whole. The linker can also detect and
report errors, for example, if part of your program is missing or a nonexistent library
component is referenced.
A failure during the linking phase means that once again you have to go back
and edit your source code. Success on the other hand will produce an executable file.
In a Microsoft Windows environment, this executable file will have an .exe extension.
Many IDEs also have a Build option, which will compile and link your program in one
11
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
step. This option will usually be found, within an IDE, in the Compile menu;
alternatively, it may have a menu of its own.
✓ Executing Programs
The execution stage is where you run your program, having completed all the
previous processes successfully. Unfortunately, this stage can also generate a wide
variety of error conditions that can include producing the wrong output or just sitting
there and doing nothing, perhaps crashing your computer for good measure. In all
cases, it’s back to the editing process to check your source code.
In most IDEs, you’ll find an appropriate menu command that allows you to run
or execute your compiled program. This Run or Execute option may have a menu of
its own, or you may find it under the Compile menu option.
In Windows, you can run the.exe file for your program as you would any other
executable. The processes of editing, compiling, linking, and executing are
essentially the same for developing programs in any environment and with any
compiled language.
Programming Paradigm
Programming is a creative process carried out by programmers to instruct a computer
on how to do a task. A program is a set of instructions that tells a computer what to do
in order to come up with a solution to a particular problem. There are a number of
alternative approaches to the programming process, referred to as programming
paradigms. Different paradigms represent fundamentally different approaches to
building solutions to specific types of problems using programming. Most programming
languages fall under one paradigm, but some languages have elements of multiple
paradigms. Two of the most important programming paradigms are the procedural
paradigm and the object-oriented paradigm. Let's look at each of these in a bit more
detail.
Procedural Programming
Procedural programming uses a list of instructions to tell the computer what to
do step-by-step. Procedural programming relies on - you guessed it - procedures, also
known as routines or subroutines. A procedure contains a series of computational
steps to be carried out. Procedural programming is also referred to as imperative
programming. Procedural programming languages are also known as top-down
languages.
Procedural programming is intuitive in the sense that it is very similar to how you would
expect a program to work. If you want a computer to do something, you should provide
step-by-step instructions on how to do it. It is, therefore, no surprise that most of the
early programming languages are all procedural. Examples of procedural languages
include Fortran, COBOL and C, which have been around since the 1960s and 70s.
Object-Oriented Programming
Object-oriented programming, or OOP, is an approach to problem-solving
where all computations are carried out using objects. An object is a component of a
12
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
program that knows how to perform certain actions and how to interact with other
elements of the program. Objects are the basic units of object-oriented programming.
A simple example of an object would be a person. Logically, you would expect a person
to have a name. This would be considered a property of the person. You would also
expect a person to be able to do something, such as walking. This would be considered
a method of the person.
Adding new data and functions is Adding new data and function is easy.
not easy.
13
IT 103: COMPUTER PROGRAMMING 1
LESSON 2:
Program Development
Once you have reached this more precise specification, you can work out the series
of steps that will lead to your final objective. So having an idea that you want to build a
house just isn’t enough. You need to know what kind of house you want, how large it’s going
to be, what kinds of materials you have to build it with, and where you want to build it. This
kind of detailed planning is also necessary when you want to write a program.
These steps are called phases in program development. The program development life
cycle is a set of steps or phases that are used to develop a program in any programming
language.
In this phase, it is the problem definition, we define the problem statement and we
decide the boundaries of the problem. In this phase we need to understand the problem
statement, what is our requirement, what should be the output of the problem solution.
These are defined in this first phase of the program development life cycle.
Also, the first step is to get a clear idea of what you want to do.
For example:
▪ It would be lunacy to start building your house before you had established what
facilities it should provide: how many bedrooms, how many bathrooms, how
big it’s going to be, and so on.
▪ All these things affect the cost of the house in terms of materials and the work
involved in building it. Generally, it comes down to a compromise that best
meets your needs within the constraints of the money, the workforce, and the
time that’s available for you to complete the project.
It is the same with developing a program of any size. Even for a relatively
straightforward problem ,you need to know what kind of input to expect, how the input
2
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
is to be processed, and what kind of output is required—and how it’s going to look. The
input could be entered with the keyboard, but it might also involve data from a disk file or
information obtained over a telephone line or a network.
The output could simply be displayed on the screen, or it could be printed; perhaps
it might involve updating a data file on disk.
Note:
For more complex programs, you’ll need to look at many more aspects
of what the program is going to do. A clear definition of the problem that your program is
going to solve is an essential part of understanding the resources and effort that are going
to be needed for the creation of a finished product. Considering these details also forces
you to establish whether the project is actually feasible.
For example:
▪ To get the house built, you will need detailed plans. These plans enable the
construction workers to do their job and the plans describe in detail how the
house will go together the dimensions, the materials to use, and so on.
▪ you’ll want the foundation dug before the walls are built, so the plan must involve
segmenting the work into manageable units to be performed in a logical
sequence.
It’s the same with a program. You will need to specify what the program does by
dividing it into a set of well-defined and manageable chunks that are reasonably self-
contained. You will also need to detail the way in which these chunks connect, as well as
what information each chunk will need when it executes. This will enable you to develop the
logic of each chunk relatively independently from the rest of the program.
If you treat a large program as one huge process that you try to code as a single
chunk, chances are that you’ll never get it to work. Three tools will help us in this task: (1)
structure charts,
(2) pseudocode, and
(3) flowcharts.
The structure chart is used to design the whole program. Pseudocode and
flowcharts, on the other hand, are used to design the individual parts of the program. These
parts are known as modules in pseudocode or functions in the C++ language.
3
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
The hierarchy chart (also known
as a structure chart) shows the relationship
between various modules. Its name
comes from its general use in showing the
organization (or structure) of a business.
The President at the top, then vice presidents
on the next level, etc. Within the context of a
computer program, it shows the
relationship between modules (or
functions). Detail logic of the program is
not presented. It does represent the
organization of the functions used within
the program showing which functions are
calling on a subordinate function. Those above are calling those on the next level down.
Hierarchy charts are created by the programmer to help document a program. They
convey the big picture of the modules (or functions) used in a program.
The Pseudo code is a term which is often used in programming and algorithm
based fields. It is a methodology that allows the programmer to represent the
implementation of an algorithm. Simply, we can say that it’s the cooked-up representation
of an algorithm. Often at times, algorithms are represented with the help of pseudo codes
as they can be interpreted by programmers no matter what their programming background
or knowledge is. Pseudo code, as the name suggests, is a false code or a representation
of code which can be understood by even a layman with some school level programming
knowledge.
▪ Algorithm: It’s an organized logical sequence of the actions or the approach towards
a particular problem. A programmer implements an algorithm to solve a problem.
Algorithms are expressed using natural verbal but somewhat technical annotations.
▪ Pseudo code: It’s simply an implementation of an algorithm in the form of annotations
and informative text written in plain English. It has no syntax like any of the
programming language and thus can’t be compiled or interpreted by the computer.
Implementation
During this phase, we develop a step by step procedure to solve the problem using
the specification given in the previous phase. This phase is very important for program
development.
Given the detailed design of a house, the work can start. Each group of construction
workers will need to complete its part of the project at the right time. Each stage will need
to be inspected to check that it’s been done properly before the next stage begins.
Omitting these checks could easily result in the whole house collapsing.
Of course, if a program is large, you’ll write the source code one unit at a time. As
one part is completed, you can write the code for the next. Each part will be based on the
detailed design specifications, and you’ll verify that each piece works, as much as you can,
before proceeding. In this way, you’ll gradually progress to a fully working program that does
everything you originally intended.
During this phase, we check whether the code written in previous step is solving the
specified problem or not. That means we test the program whether it is solving the problem
for various input data values or not. We also test that whether it is providing the desired
output or not.
The house is complete, but there are still a lot of things that need to be tested: the
drainage, the water and electricity supplies, the heating, and so on. Any one of these areas
can have problems that the contractors need to go back and fix. This is sometimes an
iterative process, in which problems with one aspect of the house can be the cause of things
going wrong somewhere else.
The mechanism with a program is similar. Each of your program modules the pieces
that make up your program will need to be tested individually. When they don’t work
properly, you need to debug them. Debugging is the process of finding and correcting
errors in your program. This term is said to have originated in the days when finding the
errors in a program involved tracing where the information went and how it was processed
by using the circuit diagram for the computer.
The story goes that it was discovered that a computer program error was caused by an
insect shorting part of the circuit in the computer. The problem was caused by a bug.
Subsequently, the term bug was used to refer to any error in a program.
With a simple program, you can often find an error simply by inspecting the code. In
general, though, the process of debugging usually involves adding extra program code to
produce output that will enable you to check what the sequence of events is and what
intermediate values are produced in a program. With a large program, you’ll also need to
test the program modules in combination because, although the individual modules may
work, there’s no guarantee that they’ll work together! The jargon for this phase of program
development is integration testing.
5
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
There are two types of testing: Blackbox and Whitebox. The system test engineer
and the user do Blackbox testing. Whitebox testing is the responsibility of the programmer.
Blackbox Testing
Blackbox Testing gets its name from the concept of testing the program
without knowing what is inside it- without knowing how It works. In other words, the
program is like a black box that we cannot see into.
Plan that will then be used when the system is tested as a whole. We should
ask to see this test plan before we write our program. The test engineer’s plan will
help us make sure we fully understand the requirements and also help us create our
own test plan.
Whitebox Testing
Whereas, Blackbox testing assume that the tester knows nothing about the
program, Whitebox testing assumes that the tester knows everything about the
program. In this case, the program is like a glass house in which everything inside is
visible.
1) Verify that every line of code has been executed at least once. Fortunately,
there are programming tools on the market today that will do this for us.
2) Verify that every conditional statement in our program has executed both
the true and false branches, even if one of them is null.
3) For every condition that has a range, make sure the tests include the first
and last items in the range, as well as items below the first and above the
last-the most common mistakes in array range tests occur at the extreme
of the range.
4) If error conditions are being checked, make sure all error logic is tested.
This may require us to make temporary modifications to our program to
force the errors.
6
IT 103: COMPUTER PROGRAMMING 1
UNIT 1: Basic Programming Concepts
Program Documentation
When the program is finished and thoroughly tested, the documentation on the program is
included for its distribution. Documentation usually includes the necessary information about
the requirements of the program – the operating system and hardware requirements needed
for the program to run. It also contains technical information such as who created the
program, who to contact when there’s a problem with the program and instructions on the
use and maintenance of the program.
Maintenance
During this phase, the program is actively used by the users. If any enhancements
found in this phase, all the phases are to be repeated again to make the enhancements.
That means in this phase, the solution (program) is used by the end user. If the user
encounters any problem or wants any enhancement, then we need to repeat all the phases
from the starting, so that the encountered problem is solved or enhancement is added.
This final stage in programming is maintaining or updating the program. This is where the
programmer is tasked to keep the program running smoothly and updated with the
developments and changes in the field where it is used
7
IT 103: COMPUTER PROGRAMMING 1
Lesson 3:
LOGIC FORMULATION Objectives:
At the end of the lesson, students will be able to:
- Define the meaning and purpose of program logic formulation. - Distinguish
the tools used in program logic formulation. - Utilize algorithms and flowchart.
Program logic formulation
Computers are logical machines. They follow instructions down to the letter
(follows steps as instructed than as intended). Although computers perform
accurate calculations or computations, an error in a formula or a value may
cause difficulty detecting errors. Computers cannot think and, therefore,
cannot correct wrong instructions. To be achieved, a program must be
carefully designed before they are created or written.
Program Logic Formulation deals with this problem. It provides the tools
needed to design the right solution by helping you define and identify the
problem, its requirements ,and the required instructions.
The two most commonly used tools will be of great help for you to formulate a
better solution. These are the flowchart and algorithm.
algorithm
Flowchart development
The phases that encompass a flowchart must be ordered in an arranged,
understandable, and transparent manner. The program that executes this
flowchart is likewise in an orderly, understandable, and transparent way. An
orderly, legible, and exact solution is called a structured flowchart.
Structured flow charts are designed using the four program structures ,namely:
a) Sequential – Steps are carried out in a firmly sequential manner ;each stage
is performed exactly once.
b) Selection - The execution of instructions by section, wherein conditions for a
series of alternative statements are evaluated to specify which instruction is to
be executed.
c) Repetition - It consists of repeating steps on more than one set of data.
d) Modular - The approach is sometimes called divide and conquer because an
enormous task is split up into several smaller tasks that are easily performed.
These structures will be our guide to produce a correct structured flowchart
necessary to prepare the right program.
sequential
In this structure, steps are performed in a strictly sequential manner and are
executed once. This is the simplest method of control. It is easy to develop and
understand. The following problems in flowcharting illustrate simple logic
flow.
Problem 1
Design an algorithm and flowchart that would determine the product of two
numbers.
sequential
Problem 2
Design an algorithm and a flowchart that will compute for the sum, difference,
product, and quotient of two number and print them out.
sequential
Problem 3
Convert an input inch(es) into its equivalent centimeters (cm). Take note that
one inch is equivalent to 2.54 cm.