Perl
Perl
PROGRAMMING AND
PROGRAMMING LANGUAGES
Structure
3.0 Objectives
.-
3.1 Introduction
3.2 Categories of Programming Languages
3.2.1 Machine Language
3.3.3 Statements
3.3.5 Modules
3.5 Summary
3.6 Answers of Self Check Exercises
3.7 Keywords
3.8 References and Further Reading
3.0 OBJECTIVES
After reading the Unit, you will be able to:
3.1 INTRODUCTION
A computer can neither think nor make decisions on its own. To solv~ a problem
using computers, a set of instructions should be given to the computer in a form
which it can understand. The process of writing these instructions is called
programming.
I
Theoretical Aspects of
known example. Consider searching the name of an author from the catalogue in
Programming
the library.
Manual solution to this problem is to flip through the catalogue for the initial letter
of the name. Once found, search for the subsequent letters of the name and on
finding the name, note the details of the book by the author.
2) Match the names available in the catalogue one by one until the exact match is
found. (It is simplest logic but we can apply more efficient logic)
3) Display the details of the book as output for the input name.
1) Identify the programming needs: Specify the program objectives and program
users, and state the input, output and processing requirements.
3) Code the program: Select the appropriate high level programming language and
code the program in that language following its syntax.
4) Test the program: The fourth step is to test the program and "debug" it of errors
so that it works properly.
Computers cannot understand natural languages such .as English or Hindi for solving
problems. To communicate instructions and commands we need programming
languages. And learning programming languages requires learning the symbols,
words and rules of the language. There are many languages available to do
programming. These are developed for specific applications.
Based on these applications, programs are of two types- system programs and
application programs .,
42 " System program: These are the programs which makes computer easier to use ..
Operating Systems such as DOS and Windows which consist of many other programs Introduction to
Programming and
for controlling input/output devices, memory, processor, etc is system program. The
Programming
C language is widely used to develop system software. Languages
Let us see the categories of these languages and their basics, and also, the advanced
features of a language. C is the programming language considered through out this
Unit.
Programs written for a computer may be either in one ofthe categories of languages.
o An instruction prepared in any machine language will have at least two parts. The
first part is the Command or Operation, which tens ,the computer what functions is
to be performed. All computers have an operations code for each of its functions.
The second part of the instruction is the operand or it tells the computer where to
find or store the data that has to be manipulated. Machine language is considered
to be the first generation language.
When symbols (letters, digits or special characters) are used for the operation part,
the address part and other parts of the instruction code, this representation is called
an assembly language progra~. This is considered to be the second generation 43
/
Theoretical Aspects of language. Machine and Assembly languages are referred to as low level languages
Programming since the coding for a problem is at the individual instruction level. Each machine
has got its own assembly language which is dependent upon the internal architecture
of the processor.
Since a computer can execute programs only in the machine language, a translator
or converter is needed if the program is written in any other language. An assembler
is such a language translator which takes its input in the form of an assembly
language program and produces machine language code in some memory location.
High level languages are also called procedural languages. Programming languages
such as C, CaBaL, FORTRAN and BASIC are high level languages. As the time
and cost for creating machine and assembly languages are high, the high level
languages were developed.
A program written in a high level language should be translated into a form the
machine can understand and this is done by a software (language translator) called
compiler which takes the source code input and produces as output the machine
language code of the machine on which it is to be executed. There is another kind
of software which also does the translation. This is called an interpreter.
For example, assume you need to display the details of a particular book (Introduction
to Computers) from the catalogue. In a procedural language, the programmer
would have to write a series of instructions in the following steps:
In a 4GL, however, the programmer would write a single instruction that says:
Majority of the fourth generation languages are used to get information from files
and databases and display or print the information. These fourth generation languages
contain a query language which is used to answer queries or questions with data
from a database: -
!
Structured Query Language (SQL) is a standard language and universally. used.
Like most other computer languages like C, C++, Pascal, or Java, you need a way
to use it on your computer. More details about the languages are given in Block
2.
Most database applications use the SQL, as their language. to communicate with
the database, like add, delete, modify the data.
44 -'i
/
3.2.5. Natural Languages Introduction to
Programming and
Programmipg
Natural languages are oftwo types. The first are the ordinary human languages Languages
like English. The second are programming languages that use human language to
give people more natural interaction with computers. Natural languages allow
questions or commands to be framed in a more conversational way. Natural
languages are the part of the field of study known as artificial intelligence.
Artificial intelligence is a group of related technologies that attempt to develop
machines capable of emulating human-like qualities, such as learning-reasoning,
communicating, seeing, and hearing,
1) Name some third generation languages, and mention what are they used for?
~-:.:-~ .
The data used in programs are stored in internal memory of the computer. In order
to store or retrieve information from memory we need to give name of each
'location in the memory. These memory location names can be declared so that only
a particular type of data will be stored. For example, we can store only whole
numbers such as 50 or numbers with fractional part such as 345.570. This
specification of types of data stored is done using some key words called data
types.
In 'C' language, the basic data types are integer, float and char. Many variations
on size leads for further classification on these basic data type like short int,
Int: A type of memory location which can hold only integer values, i.e. no fractional
part. The precise range of integers which are supported varies from one version
of C to another. ""
Float: A type of memory location which can hold a real (i.e. floating point) number.
e.g, numbers with fractional digits.
Char: Atype of memory location which can hold a single character. A character
is what you get when you .press a key on a keyboard. <t 45
/
~
Theoretical Aspects of Data types and storage capacity are language dependent. One needs to be aware
Programming of them while learning a language. There are two broad categories of data. These
are called variables and constants.
'Constants: Constants have fixed value. -For example, p = 3.1444. All data types
can have constant value assigned to them. Few examples are given below. Integers
can have values like 2, 4, etc. Sequence of characters in a word say, "computer"
are string constants (string is sequence of more than one characters). Operations
such as addition, deletion, multiplication and division can be performed using
constants.
The size and limit of data types in C, C++ and JAVA are given below.
In C
In C++
/
In JAVA Introduction to
Programming and
Programming
Data type Number Minimum Maximum
Languages
of Bytes Value value
In the above tables let us consider the C language table, In that the number of bytes
column gives details of how many bytes a particular data type can occupy in
memory as Char data type, value stored can occupy maximum 1 byte, like values
stored in short integer data type cannot exceed 4 bytes. For example, consider three
integer variables used in a program, then the maximum storage of the data is 3 x4
::::12 bytes.
The minimum and maximum values gives us limit of actual value that can be stored
in that particular data type. The range of the values stored is also limited according
to the number of bytes. The value that will be stored in Integer data type should
be between -32768 to 32767.
In programming we deal with large amount of data. To represent each data element
we have to consider them as separate variables. For example, we need process
marks of student in an organization. There are 5 semesters, for which we need 5
different variables. Instead if we can use only one name to refer all the five
locations then it simplifies the program. This can be done by the user by defining
a data type of his own. The basic and simple user defined data type is called array .
.
',
Array is a collection of same type of data, all of which are referenced by the same
name. Generally used arrays are one dimensional or two dimensional.
One Dimensional Array: It is a list of related data of same type referred by one
variable name. For example marks of a student can be referred as follows
65 70 82 68 83
Two dimensional array: In a single dimensional array the data of array is dependent
upon a single factor such as marks in the semesters. In some cases, we need
• array which is dependent upon two factors, such, marks of five semesters for a
particular student who obtained the marks. In this case we use two dimensional
arrays which arerepresented in tabular form (in rows and columns) as shown
below. Rows represents the, student name and columns the semester.
/
47
/
Theoretical Aspects of Sem I Sem2 Sem3 Sem4 Sem5
Programming
Bharat 60 73 68 84 53
Jagjit 75 91 82 63 70
Rajan 86 54 75 78 85
3.3.3 Statements
.-
We have seen data types to store values in memory location. Statements are those
which manipulate these data using predefined structure. There may be many types
of statements, which are described below.
Input/Output Statements
The function of input statement is to get value of input from user through some
input device like keyboard. Suppose, we need to get book title for searching the
availability of a book, it looks like
Now after obtaining the availability we need to display the output. This is done by
the following statement.
printj("%s ",BookTitle);
printj("%f", circumference);
These input, output statements are provided as built in library along with the language.
In C we need to specify the library file which is called header file in the beginning
of the program. The header file for input, output statements in C is "stdio.h". To
specify this, you must write include <stdio.h> in the beginning of the program.
Depending on the language the input, output statement varies, for example in BASIC
input would be written as: -
INP UT Radius
Arithmetic Expressions
~e can do arithmetic operations using expressions like A+B, A-B, A*B, AIB and
so on. Several simple expressions can even be nested together using parentheses
to form complex expressions. Every-computer language specifies the.order in which
various arithmetic operators are evaluated in an expression. An expression may
contain operators such as
/
48
Parentheses () Intr«,?~1;lc.ti~n to
Programming and
Programm ing
e.g. Total Salary = (Basic Salary + Allowances) - Detections
Languages
1\
Exponentiation
Negotiation
Invert = ~1
Multiplication, Division *, /
Salary = 27 * 650
Addition, Substractions +, -
Assignment statement
Variable = assignment;
C = A*B+IOO;
If you assign a value to a variable it looses its previous value and the new value
remains in the memory till next assignment is made on the same variable.
Control Statements
In any language sequential flow of the statement is default i.e; a program executes
)
instructions sequentially from first to last. Apart from sequential there are two
types of flow of execution available. These are:
if (Boolean condition)
SI .-
else
S2
If-then-else allows an empty else case also. For some cases we need few statements
to be executed if condition becomes true, else we want to continue the actual flow
i.e. nothing is to be done in the else part. In general
if (Boolean condition)
SI
In looping it is not just the next statement to be executed that is decided, but a
block of statements called loop body which is repeated for the number of times
depending upon the result of a Boolean condition. The condition is tested before
executing the loop body and when the condition becomes false the loop body wi 11
not be executed again. In general;
SI
After knowing various elements of programming language and their use, we should
write them out in a structured manner while writing a complete program. The points
given below gives us the order in which the elements are to be used.
• Necessary header files (stdio.h is minimal requirement for any program as input,
output will be there)
Comment (optional)
Main function
declarations (optional)
50
/
main function body Introduction to
Programming and
} Programming
. Languages
An example of the above is given below:
II Program to compare two values and printer the greatest among them.
#include <stdio.h>
mainO
int A, B;
A = 10;
B = 15;
if (A>B)
else
3.3.5 Modules
Solution to complex problem needs complex and large programs. If we write the
entire thing in a single main function it may be very lengthy and difficult to understand.
Changes in the code in future are not easy. So breaking the program into many sub
tasks and design proper communication between them will help solve the problem
of complex programs more easily.
The sub task or part of code to do a specific task is called function. If we use
functions, the main program can just need to invoke these functions. Calculating
circumference, area computation of a circle problem can have three functions to
get an input, compute circumference, and compute area. This can be illustrated as
follows:
Once the execution \of main is over the execution returns back to main function
function header
{
51
I
Theoretical Aspects of local declarations (optional)
Programming
function body
For example, consider the function swaps two values by receiving them from main
function
maim) .-
{
int Temp;
Temp = b;
b = a;
a = Temp;
• Return type
• Function name /
• Parameter list
Return type is thetype of variable i~{s given as a result to the calling function, for
example, the main function. If the function is not returning any value like, it simply
prints some message then the function's return type is void.
Function name can be any user defined name (Don't use keywords of language
as function name)
The parameter list is a list of variables given by main function as an input to the
sub function. The list.and order of parameter in the main function and parameter
list in function should match. The data type is given as part of the list of parameters.
#include <stdio.h>
52 I'void main( )
,I
{ Introduction to
Programming and
Programming
float Radius; Languages
Get_Input(Radius, "C");
scanf("%f' ,&Radius);
if (Task to do == 'C')
Do~Circumference(Radius);
Else
Do_Area(Radius);
float Circumference;
Circumference = 2·3.144*Radius;
float Area;
Area = 3.144*Radius*Radius;
printf(\"Area is %f',Area);
Here the return type of the functions Do_Area and Do_Circum- ference are void.
The parameters in the functions are passed as their values from main function. This
type of parameter passing As called passing by values. That is the copy of the 53
/
rheoretical Aspects of radius value is passed to them. But they are aware of the memory location where
Jrogramming "
the original value is stored.
If the main function wants to change the original value then instead of sending the
copy of the value present in the memory location it will send reference of that
memory location itself. The changed value will be stored in the memory location by
the sub function. This is called pass by reference.
The parameters should be passed by value if the function doesn't need to change
the values instead just use them for some operations. If the change should reflect
in the memory of that variable then pass by reference is preferred. The clarity in
the choice of pass by value and pass by reference is efficiency in modularity.
2) Declare an integer array with three columns and three rows and what will be the
size of total array in bytes if the integer takes four bytes of memory?
3) Compare two values (15, 7) and give the higher value as an output? Write a
program.
The fundamental operation of these languages are storing and retrieving data values
For example
a = 1
? ~
54
This statement stores value I in location a
I
a, c = a +b Introduction to
Programming and
Programming
This statement retrieves a and b, add and stores the result in c. Some of the
Languages
procedural languages are discussed below.
FORTRAN
The name FORTRAN derives from FORmula TRANslation. The name itself
indicates that the applications of language with "formula" of engineering-type
application. The first commercial version of FORTRAN released Hi 1957. This
makes FORTRAN the first high-level programming language. FORTRAN has
some features ideally suitable for problems that are heavily mathematical or
computational oriented.
COBOL
The name COBOL derives from Common Business Oriented Language. COBOL
was designed to. serve business needs such as managing inventories and payrolls.·
In such applications summary reports are important output. Much of the business
world concerns updating master files with change in transaction files. For example
master file contain names, manufacturers, and quantities available for various items
inventory; a transaction file would contain names; quantities and items sold out of
inventory over a period of time. The master file would be updated from the transaction
file on weekly or daily basis to reflect new quantities available and print summary
report.
PASCAL
The programming language Pascal was named after Blaise Pascal, the inventor of
Pascaline calculator.
The design of Pascal was to easily learn and enforce good programming techniques.
Pascal looks very similar to pseudocode. So it is easy to read and the syntax is easy
to learn. Even though Pascal is not much useful as commercial 'language but,
programming environment like Delphi uses Pascal language with a facility to develop
windows based applications with modem graphical user interface.
The strong feature of C is that it provides a data type Called pointer. Variables of
pointer type contains memory addresses instead of integers, real numbers and
characters. For example the statement, .
int* intPointer;
declares intl'ointer asa pointer variable that will contain the address of amemory
location containing integer data.
int A = 3;
intPointer = & N: 55
,/
Theoretical Aspects of The first statement declares a variable called A and assigns the value 3 to that.
Programming The second statement makes the intPointer pointing to A. We can also assign the
integer value 10 to variable A by the statement
*intPointer = 10;
The powerful use of pointers are in the areas of writing system programming,
operating systems, assemblers, and programs that allows the computer to interact
with input/output devices and so on.
--
For example consider a problem of writing a device driver (software to interact
with a hardware device) for mouse on a PC. The port in which the mouse is
connected reads the changes in mouse position by voltage levels. It stores voltage
levels in a fixed location of memory. C provides facilities to access this memory and
manipulate it.
Even though C is powerful for system programming it is widely used for general
purpose programming also.
ADA
Ada development was for a common high-level language needed for various branches
\ \ of United States armed services for defense contracts. Ada is a large language
like C++ and has been accepted not only in the defense industry, but for other
technical applications and is a general purpose language as well.
Ada is known for its multi-processing capability - the ability to allow multiple tasks
to execute independently and then synchronize the communication when directed.
Object Oriented Programming started by Alan Kay's work at Xerox Palo Alto
Research Centre in early 1970s. The resulted language is smalltalk.
The concept of Object-oriented programming is dividing the major task into sub
tasks. The program can be thought of a giant statement executor designed to carry
out major tasks, even though the main program may simply call the various modules
that do the sub task work.
The procedural languages that we saw so far are more or less general purpose
languages. There are several special purpose languages designed for specialized
tasks. Here we take three representative special purpose languages, which are
very popularly used. ""
SQL is designed to be used with databases, which are collections of related facts
and information. A database stores data; the user of the database must be able
to add new data and to retrieve data already stored. For example, a database of
library catalogue may contain titles of book, its author's name, publisher name, year
of publication, etc. The database of catalogue user should be able to add information
on a book and to retrieve information on a<ibookalready in the database. In addition,
56 " databases can also be queried with the user posing questions to the database. For
I
/
example, the library catalogue could be queried to reveal books by a particular Introduction to
Programming and
author or by a publisher. Such queries may be framed in SQL. SQL is the Programming
language used to frame database queries. Sample SQL statement is as follows: Languages
FROM CATALOGUE
This SQL statement will retrieve all the book titles with publisher and year details
of the author Guha.
Examples of prominent database management systems that use SQL are Visual
FoxPro, Oracle, MS-Access and SQL Server. Visual FoxPro is a commonly used
database management system in libraries. For example, the library catalogue can
be created in Visual FoxPro and the SQL can be used to query the database by
name of the author, title of the book, keyword, etc.
Perl is designed to scan arbitrary text files, extract various kinds of information that
is contained within the text, and print reports based on the extracted information.
The language syntax is somewhat based on C. Perl uses sophisticated pattern
matching techniques to speed up the process of scanning large amounts of data for
a particular text string.
This is the language used to create HTML documents that, when viewed with Web
browser software, become Web pages. An HTML document consists of the text
to be displayed on the Web page, together with a number of special characters
called tags that achieve formatting, special effects, and references to other HTML
documents. Tags are enclosed in angle brackets (<» and often come in pairs. The
end tag, the second tag in the pair, looks like the begin tag, the first tag in the pair,
but with an additional/in front.
<html>
<head>
<lhead>
<body>
<Jhtml>
/
,;
Theoretical Aspects of 5) Write an SQL statement to display the books issued from the library for a member
Programming named Amit.
.-
3.5 SUMMARY
In this Unit, we have dealt with programming and programming languages. The
focus of attention in this Unit are the following:
ifa> b
printf("%d" ,a);
else
printf("%d" ,b);
5) SELECT BOOK_IS,SUED
FROM CIRCULATION DATA
WHERE BORROWER NAME = "Amit"
3.7 KEYWORDS
Application program : Program that has been developed to solve a
particular problem, perform useful work on general-
/ purpose tasks, or provide entertainment
-'1
58
,/
I
Assembly language It is a low level programming language that allows Introduction to
Programming and
a computer user to write a program using
Programming
abbreviations or more easily remembered words Languages
instead of numbers.
,;
S9
/
,/
,I