1 Introduction To Programming Concepts
1 Introduction To Programming Concepts
Computer is an electronic device used for storing and processing information. Most computers follow the
basic principle of input, process, and output for any problem. To perform these input, process, and output
activities, computers need a set of instructions called a program.
This chapter introduces the input, process, and output requirements of a computer. It explains the role of
programs and programming languages in passing instructions to a computer. In addition, it describes various
tools for problem solving.
Objectives
You might have noticed computers being used at airline reservation counters, shops, restaurants, and
various other locations. At each of these places, a user enters some values in the computer and the
computer generates an output, which is either displayed on the computer screen or printed on paper. Let us
consider the example of an airline reservation counter. When you want to reserve a seat on a particular
flight, you provide information about your requirements such as destination, date and time of your
departure, and the class in which booking is required. The executive at the reservation counter enters this
information into a computer. Details regarding the availability of tickets are then displayed on the screen.
The preceding automated process is not carried out as one activity. It is broken down into a set of activities
that are carried out in separate phases. Let us see what are the phases involved and the process followed
for each of the tasks.
Phases
To understand what happens when you enter some values in the computer, let us break the entire set of
activities into separate phases. In the first phase, flight requirements are keyed into the computer. This
phase is called the input phase. The flight requirement information is then processed to determine whether
seats are available on the particular flight. This phase is known as the process phase. Once the processing is
complete, the result is displayed on the computer screen, indicating the status of seat availability. This phase
is called the output phase. The cycle of activities performed by a computer is also known as the Input-
Process-Output (I-P-O) cycle.
I-P-O Cycle
A computer consists of several components such as a keyboard, a mouse, a monitor, a printer, and a
Central Processing Unit (CPU). Each component participates in either one of the input, process, or output
phases. For example, the keyboard and mouse are used for input. The CPU and the memory inside the
CPU are used for processing. The monitor and printer are used for output. The following figure depicts the
I-P-O cycle.
A computer needs a set of instructions called a program to perform the I-P-O cycle. A program needs to
be written in a specific language called programming language, so that computer can understand the
instructions.
Programs
How does a computer know the steps to be followed to process reservation requests? How does it
manage to calculate your shopping bills at the local store? How does it generate your report card at school?
Is it an all-knowing machine? Do you have to buy a different computer for each of these preceding
activities? The answer to the last two questions is ‘No’. It is not an all-knowing machine and you do not
require a separate computer for each of these activities.
Then, how does a computer function? A computer is designed to accept input, process it, and generate
output. However, it has to be provided with a set of instructions that specify:
The kind of input that will be provided. For example, in the case of reservation requests, flight
date, flight time, class, and destination will be some of the inputs.
The processing that needs to be done. For example, accepting the values, checking for
availability of seats, and displaying results.
The kind of output expected. For example, the seat availability status.
Therefore, for each job that you want the computer to perform, you require a separate program.
Instructions in a program can be:
Programming Languages
A computer system uses a number system that consists of only two digits, 0 and 1, which is called the
machine language. For example, the following table displays the machine code of the commands, ADD and
SUBTRACT.
ADD 00000001
SUBTRACT 00000010
Machine Code of ADD and SUBTRACT Command
However, it is difficult for anybody to remember instructions in the form of machine language. Therefore,
we use high-level programming languages to write programs. A high-level programming language consists of
a set of instructions, which are represented using simple English words. So, when you want the computer to
display the output on the screen, you type the instruction ‘WRITE ON SCREEN’ and not ‘00100000’.
There are many high-level programming languages available, such as C, C++, and Java.
All languages have a vocabulary, which is a list of words that have a specific meaning in that language.
Languages also have their own grammar rules, which state the rules for combining words to form sentences.
The vocabulary of the programming languages is referred to as the set of keywords of that language, and
the grammar is referred to as the syntax.
Compilers
Can computers directly interpret the instructions written in any programming language? No, they cannot.
Then, how do you ensure that a computer executes the instructions from a program, as intended? What
would you do if you got hold of a recipe for a delicious dish written in a foreign language that you do not
understand? You would get it translated into English or any other language you understand to be able to
prepare the dish. Similarly, you need a translator to convert the instructions written in a programming
language into machine language.
A compiler is a special program that processes the statements written in a particular programming language
and converts them into machine language at one go. Like everything else in the computer, the compiler also
follows the I-P-O cycle. It takes the programming language instructions as input. It then processes these
instructions to convert them to machine language. These instructions can then be executed by the computer.
This process of conversion is called compilation. For each programming language, there is a different
compiler available. For example, for compiling a program written in the C language, you require a C
compiler, and likewise, for a Java program, you require a Java compiler.
Before we actually start writing a program, we need to derive a procedure to solve the problem. The
procedure to solve a problem can be represented by using the following tools:
Algorithm
Flowchart
Pseudocode
Algorithm
For example, to withdraw a required amount from an ATM, the algorithm will be:
Consider another example where a television company may provide the following algorithm in the television
manual to troubleshoot the ‘No sound’ problem:
Flowchart
Advantages of Flowchart
Flowchart offers many advantages. Some of these advantages are:
Disadvantages of Flowchart
Although flowchart provides several advantages, it has a few disadvantages as well. Some of these
disadvantages are:
A lengthy flowchart may extend over multiple pages, which reduces readability.
Drawing a flowchart using any graphical tool is a time consuming process.
The changes made to a single step may cause redrawing the entire flowchart.
Pseudocode
Pseudocode is a detailed yet readable description of what an algorithm must do, expressed in a formally
styled natural language rather than in a programming language. Pseudocode is used as an initial step in the
process of developing a program. It provides programmers a detailed template for writing instructions in a
specific programming language.
Advantages of Pseudocode
The advantages of using pseudocode are:
Limitations of Pseudocode
The limitations of using pseudocode are:
Exercises
Exercise 1
Write an algorithm to accept two numbers, divide the first number by the second, and display their quotient.
Exercise 2
Write an algorithm that accepts distance in kilometers, converts it into meters, and then displays the result.
Summary