0% found this document useful (0 votes)
23 views

1 Introduction To Programming Concepts

Uploaded by

vishnu haran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

1 Introduction To Programming Concepts

Uploaded by

vishnu haran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter 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

In this chapter, you will learn about:

Input and output requirements of a computer problem


Programs and programming languages
Various tools for problem solving

Input, Process, and Output

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.

The I-P-O Cycle

Programs and Programming Languages

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.

Let us discuss program and programming language in more detail.

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:

Sequential: Instructions that are executed one after the other


Decision making: Instructions that evaluate an expression (relation or condition) first and then,
depending upon whether the value of the expression is true or false, it transfers the control to a
particular statement
Iterative: Instructions that are executed repeatedly, depending on the value of an expression
(relation or condition)
Any real-life problem will consist of a number of small jobs. Therefore, to solve a real-life problem, you
have a number of programs that together form an application. For example, a payroll application for
processing employee compensation details and generating paychecks and associated reports might have the
following two programs:

Program to generate paychecks


Program to print reports

Programming Languages

A computer is made up of a number of electronic components. An electronic component can be either in an


on state or in an off state. Therefore, the components of a computer are represented as a combination of on
and off states. The on state is represented by 1 and the off state by 0. This implies that a computer can
understand only 0s and 1s. Therefore, any instruction given to a computer must be a sequence of 0s and 1s.

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.

Command Machine Code

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.

Tools Used in Problem Solving

As seen earlier, a program is a set of instructions to solve a particular problem.

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

An algorithm is a sequence of steps required to solve a problem. It is a list of well-defined instructions to


solve a problem. According to D.E. Knuth, a pioneer in the field of computer science, an algorithm has the
following characteristics:

An algorithm ends after a fixed number of steps.


Each step in an algorithm clearly specifies the action to be performed.
The steps in an algorithm specify basic operations. These operations could include calculations,
input/output operations, and comparisons.
An algorithm accepts input data, in a defined format, before it can be processed.
An algorithm generates one or more outputs after the input is processed. The resultant
information termed as output can be displayed or stored for future reference.

For example, to withdraw a required amount from an ATM, the algorithm will be:

Step 1: Start the algorithm.


Step 2: Go to any local ATM.
Step 3: Insert or swipe your ATM card.
Step 4: Press the language button that you want to choose.
Step 5: Enter the pin code number.
Step 6: Press the account type (Savings or Current) button from which you want to withdraw the money.
Step 7: Press the cash withdrawal button.
Step 8: Enter the amount you want to withdraw.
Step 9: Collect the amount from the ATM machine.
Step 10: Collect the statement from the ATM machine.
Step 11: Collect your ATM card.
Step 12: End the algorithm.
An algorithm follows the I-P-O cycle to solve a problem. Here, the inputs to withdraw a required amount
from an ATM would be the ATM card, the pin code number, and the amount that you want to withdraw.
These inputs are then accepted and the process of withdrawing the amount takes place. At the end of the
process, the amount, which is the output, is withdrawn.

Consider another example where a television company may provide the following algorithm in the television
manual to troubleshoot the ‘No sound’ problem:

Step 1: Start the algorithm.


Step 2: Ensure that the Mute button is not pressed.
Step 3: Ensure that the desired volume is set.
Step 4: If sound is still inaudible, call the television engineer.
Step 5: End the algorithm.
Consider another algorithm that displays the product of two numbers:

Step 1: Start the algorithm.


Step 2: Get the first number.
Step 3: Get the second number.
Step 4: Multiply the two numbers.
Step 5: Display the result.
Step 6: End the algorithm.

Flowchart

A flowchart is a graphical representation of an algorithm. It consists of a set of symbols. Each symbol


represents a specific kind of activity depicted in the algorithm. A typical problem would involve accepting
input, processing the input, and displaying the output. The processing would involve the computations and
the decisions that are being taken.

Advantages of Flowchart
Flowchart offers many advantages. Some of these advantages are:

Flowcharts help in analyzing the problems effectively.


Flowcharts act as a guide during the program development phase.
Flowcharts help easy debugging of logical errors.

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:

It is easy and fast to write as it uses English like statements.


It needs no rewriting if any changes are made in the steps. This is because each step is written
independent of the other.
It can be converted to a program by using any programming language. This is because the format
used by pseudocode is similar to a program. They both contain a set of sequential statements
using a defined set of keywords.

Limitations of Pseudocode
The limitations of using pseudocode are:

Pseudocode does not provide a graphical representation of an algorithm; therefore, it might be


difficult to understand the complex logic in a pseudocode.
Pseudocode depicting too many nested conditions may be difficult to understand.

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.

Hint: Distance in meters = Distance in kilometers * 1000


Exercise 3
Write an algorithm that accepts five numbers and displays the sum and average of the numbers.

Summary

In this chapter, you learned that:

The cycle of activities performed by a computer follows the I-P-O cycle.


A set of instructions to perform a particular job is called a program.
In a computer, everything is represented as a combination of on and off states.
The on state is represented by 1 and the off state by 0.
A high-level programming language consists of a set of instructions.
The language that the computer understands is called the machine language.
A compiler is a special program that converts the instructions given in high-level programming
language into machine language.
An algorithm is a sequence of steps required to solve a problem.
A flowchart is a graphical representation of the steps to be followed for solving a problem.
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.

You might also like