Unit-1
Unit-1
Unit-1
Fundamentals of
Programming
Syllabus
Importance of Studying Programming Languages, History of Programming Languages, Impact of
Programming Paradigms, Role of Programming Languages, Programming Environments. Impact of
Machine Architectures: The operation of a computer, Virtual Computers and Binding Times.
Programming paradigms - Introduction to programming paradigms, Introduction to four main
Programming paradigms- procedural, object oriented, functional, and logic and rule based.
Contents
1.1 Importance of Studying Programming Languages
(1-1)
Principles of Programming Languages 1-2 Fundamentals of Programming
Review Question
1) Scientific applications :
e The scientific applications require large numbers of floating point computations.
© Ittypically makes use of arrays.
* The programming language used is - Fortran.
2) Business applications :
« The business applications are characterized by facilities of producing variety of
reports.
3) Artificial intelligence :
* The artificial intelligence is an area in which the computations are symbolic rather
than numeric. Symbolic computation means that symbols, consisting of names
rather than numbers.
« Symbolic computation is more conveniently done with linked lists of data rather
than arrays.
« This kind of programming sometimes requires more flexibility than other
programming domains
* The programming language used in this area of applications is - LISP.
4) Systems programming :
* System programming is a system software which consists of collection of operating
system and programming support tools.
* The system software is used continuously, hence it needs to be efficient.
« It should also have a support for interfacing with external devices to be written.
(For example - some support programs for plug and play devices).
e The general programming languages such as C, C++ are usually preferred for
system programming.
5) Web software :
e The world wide web software is used to represent some dynamic web contents,
representation of text, audio or video data, contents containing some computations
and so on.
It need a support for variety of languages from markup languages (For example -
HTML), scripting(For example - PHP), to general-purpose(For example -Java).
FORTRAN |:
It is designed for new IBM 704 , which had index registers and floating point
hardware.
From this language onwards the idea of compilation came into existence.
The first implemented version of Fortran has following features -
o Names could have up to six characters.
o Post-test counting loop such as DO was present.
oe It has formatted I/O
o It allows user defined subprograms.
o There were no data typing statements
FORTRAN Il:
e It was distributed in 1958
Independent compilation was supported for FORTRAN II
FORTRAN IV,77,90 :
e Itis evolved during 1960-62
e It has explicit type declarations
There was logical selection statement
Subprogram names could be parameters
It was standardized as FORTRAN 66 in 1966
In FORTRAN 77 the character sting handling features was introduced. IF-THEN-
ELSE statement were introduced.
Most significant changes took place in FORTRAN 90 by allowing use of modules,
dynamic arrays, pointers, recursion,CASE statements, parameter type checking.
ALGOL 60:
« It supports the block structure.
« It has two parameter passing methods.
¢ It supports the subprogram recursion.
COBOL :
¢ The first macro facility in a high level language.
e It has a support for hierarchical data structures.
¢ It contains nested selection statements.
« Long names for variables up to 30 characters were allowed with hyphens.
e It has separate data division.
BASIC :
* Itis easy to use and learn.
¢ The current popular version of BASIC is visual basic.
PLI:
« It was designed by IBM and SHARE.
* Characterized by dynamic typing and dynamic storage allocation
« Variables are untyped i.e. A variable acquires a type when it is assigned a value
* Storage is allocated toa variable when it is assigned a value.
PASCAL :
¢ Itis designed initially for teaching structured programming.
¢ It was small, simple and easy to learn.
PROLOG :
* Itis based on formal logic.
* Itis a Non-procedural language.
* Itis considered to be intelligent database system that uses an inferencing process to
infer the truth of given queries.
¢ Itis comparatively inefficient.
ADA:
* For design of this language required huge design effort, involving hundreds of
people, much money, and about eight years.
¢ By allowing use of packages, ADA has a support for data abstraction.
* It support the exception handling mechanism.
* It allows the use of generic units and concurrency mechanism.
« It contains more flexible libraries.
JAVA:
« Developed by Sun Microsystems in 1990.
* Itis based on CH
e Itsupports only OOP.
¢ It contains references but no pointers.
¢ It hasa facility and support for applets and concurrency.
« It has libraries for applets, GUIs, database access.
¢ Itis widely used for web programming.
program is executed with test input data and the output is checked against the
specifications.
If the semantic and syntactic structure is simple then the program verification
becomes simplified.
. Program environment : The programming environment includes well-documented
implementations, special editors and testing packages, version control services and
so on. The technical structure of programming language has a great influence on
use of programming environment appropriately.
Review Questions
Programming Environments
Definition : Programming environment is an environment in which the programs can
be created and tested.
Separate compilation :
There are varying number of programmers working on design, code and test the parts
of programs. This require the language to be structured so that individual subprograms
or other parts can be separately compiled and executed without requirement of other
part. Later on these parts are to be merged into a final program.
Separate compilation is difficult because compiling one subprogram may need some
information which is present in some other sub program or sometimes shared data is
also used in the program due to which separate compilation is not possible. To solve this
problem, some languages have adopted certain features that aid separate compilation.
These features are -
1. The statements such as extern can be used to indicate that particular data is used
from other subprogram.
2. Languages use scoping rules to hide names. One subprogram can be contained
within another subprogram so that the name of the outer subprograms are known
to separately compiled subprogram.
3. In object oriented programming, the feature like inheritance can be used to use the
shared data.
Environment Framework
¢ Environment framework is nothing but the collection of infrastructure services that
can be used for program development purpose. These services are data repository,
Graphical user interface, security and communication services.
¢ Borland JBuilder is a programming environment that provides integrated compiler,
editor and debugger for Java development.
« The modern programming environment makes use of Visual Studio .NET for
developing the applications using C#, Visual Basic, .NET, Jscript, J# and so on.
* Unix is an old programming environment and has a strong support for various
powerful tools for software production and maintenance. The UNIX GUI runs on
the top of basic UNIX kernel. Typical examples of such GUI are GNOME and KDE.
Review Questions
Operation of a Computer
Working of any computer is based on the software present in it. The software contains
collection of programs. These programs are created using suitable programming
languages.
A computer consists of six major components that are associated with the
programming languages. These components are —
1. Data
« Inany programming language there are variety of data items and data structures.
¢ The data is stored and processed within data storage components of a computer.
The three major data storage components are —
o Main memory: Main memory consists of bits subdivided into fixed length words.
o High-speed Registers: The contents of registers represent either data or the
memory address of main memory containing data.
o External Files: The external files is generally stored on hard disk or on CD ROM.
It contains records.
External Files
and
InputtOutput
Main Memory
2. Primitive Operations
* The primitive operations are also used for controlling the sequence of execution of
programming statements.
3. Sequence Control
* Computer must provide the mechanism for controlling the flow of execution of
programming instruction. This mechanism is called as sequence control.
¢ Various primitive operations are used for sequence control. These operations
transfer the control flow from one memory location to another.
« The next instruction to be executed is determined by the special purpose register
called program counter(PC).
« The mechanism of execution of programming instruction is illustrated by following
figure.
Decode Instruction
Execute
Primitive Operations
Halt operation
No halt operation
Stop
4. Data Access
Computer provides a controlling mechanism for supplying data for execution of
programming instruction. This mechanism is called data access.
5. Storage Management
« Computer provides the mechanism for controlling the storage for programs and
data resources.
« There are three types of storage components in computer and those are — (i) Main
Memory (ii) High speed Registers and (iii) External Files.
* Sometimes the speed of CPU and main memory does not get synchronized. Hence a
special memory called cache memory is used.
* Acache memory is high speed small memory situated in between the main memory
and CPU. The data present in the cache memory is immediately available CPU.
6. Operating Environment
* Computer provides the mechanism by which external programs help in execution
of application or user programs which constitutes the programming environment.
¢ The operating environment consists of peripheral storage and I/O devices.
Relocatable
machine code
Linker
Single relocatable
unit
| Loader
Executable
unit
The modern translation technique uses the combined two technique - translation and
then interpretation. That means the source code is translated into intermediate code and
then the intermediate code is interpreted.
Source Program
Result
Advantages:
1. Modification of user program can be easily made and implemented as execution
proceeds.
2. Type of object that denotes a variable may change dynamically.
3. Debugging a program and finding errors is simplified task for a program used
for interpretation.
4. The interpreter for the language makes it machine independent.
Disadvantages :
1. The execution of the program is slower.
2. Memory consumption is more.
3) Hybrid implementation system : In this system the programs are translated using
both interpreter and compiler.
* Using compilers the high level programs are translated into intermediate language.
These intermediate languages are easy to interpret. The interpreter interprets the
intermediate language to form the machine code.
« Examples - Perl is implemented using with hybrid implementation system. Initially
implementation of Java were all hybrid. The intermediate form of Java code is
called as byte code. This byte code provides the facility of execution of the code on
any platform. Thus portability to any machine(Platform independence) can be
achieved using byte code.
¢ The Just-In-Time (JIT) compiler is a component of the Java Runtime Environment
that improves the performance of Java applications at run time. Java programs
consists of classes, which contain platform neutral bytecode that can be interpreted
by a Java Virtual Machine(JVM) on many different computer architectures. At run
time, the JVM loads the class files, determines the semantics of each individual
bytecode, and performs the appropriate computation.
Source Program
Lexical analyzer
Syntax analyzer
Intermediate code
generator
Input
data ——» Interpreter
Results
Interpreter Compiler
Demerit : The source program gets interpreted Merit: In the process of compilation the
every time it is to be executed, and every time program is analyzed only once and then
the source program is analyzed. Hence the code is generated. Hence compiler is
interpretation is less efficient than Compiler. efficient than interpreter.
The interpreters do not produce object code. The compilers produce object code.
Merit : The interpreters can be made portal Demerit : The compilers has to be present
because they do not produce object code. on the host machine when particular
program needs to be compiled.
Merit : Interpreters are simpler and give us Demerit : The compiler is a complex
improved debugging environment. program and it requires large amount of
memory.
Concept of Preprocessor
e Definition : Preprocessor is a program that processes a program just before the
program is compiled.
Preprocessor instructions are embedded in the program.
Preprocessor macros (instructions) are commonly used to specify that code from
another file is to be included.
For example : The C preprocessor expands #include or #define in the following
manner -
o #include "mytestfile.h"
causes the preprocessor to copy the contents of mytestfile.h into the program at
the position of the 4include.
o #define SIZE5
In the program we can declare an array of size 5 in following manner -
int a[SIZE];
Virtual Computers
Definition of Virtual Machine : Virtual machine is a piece of software which
simulates the actual machine environment. An implementation of programming
language requires that the programs in the language are analysed and then
translated into a form that can be run by interpreter i.e. on virtual machine first and
then on actual real machine.
For a virtual machine environment the source program is translated into an
intermediary abstract form which is then interpretively executed.
In this case, the source program first goes through three stages such as lexical
analyser, syntax analyser and type checker. Then the abstract syntax obtained from
these three phases is interpreted to get the translated output.
Due to this feature it is possible to execute the abstract code on any desired
platform.
For example — Java virtual machine is a kind of virtual machine which plays an
important role in achieving the platform independence.
The general structure of virtual machine is as shown in Fig. 1.5.5.
Source
program
Abstract
syntax
Input —+} Interpreter | Computer
Output
On entry t+—— By
of subpragram programmer
—— At arbitrary ->—— By
points translator
L___ py
Loader
1. Execution Time
* There are many bindings that are performed during execution of the program.
These type of binding are also called as dynamic binding or Run time binding.
2. Translation Time
¢ Chosen By Programmer: In this binding, declaration of bind types to variable name
occurs. Similarly the binding between values and constants occur.
« Chosen by Loader: Some bindings are chosen by loader. For example — global
variables to location source program to object program representation.
The method definition and the method call are The method definition and a method call are not
linked during the compile time. This happens bound until run time.
when all information needed to call a method is
available at the compile time.
Review Questions
4. Logic programming
Examples of various programming languages based on the above mentioned
paradigm is as shown by following table
Cc Java
PASCAL
FORTRAN
Imperative Programming
The Latin word impetrate means to command. Hence this language is command
driven or statement oriented language.
The imperative programming is also called as procedural programming language.
A program consists of sequence of statements. After execution of each statement the
values are stored in the memory.
The central features of this language are variables, assignment statements, and
iterations.
Merits :
1. Simple to implement.
2. These languages have low memory utilibzation.
Demerits :
1. Large complex problems can not be implemented using this category of language.
2. Parallel programming is not possible in this language.
3. This language is less productive and at low level compared to other programming
languages.
Merits :
1. It provides a modular programming approach.
2. It provides abstract data type in which some of the implementation details can be
hidden and protected.
3. Modifying the code for maintenance become easy, due to modules in the program.
Modification in one module can not disturb the rest of the code.
4. Finding bugs become easy.
5. Object oriented programming provides good framework for code library from
which the software components can be easily used and modified by the
programmer.
Demerits :
1. Sometimes real world objects can not be realized easily. Hence it is complex to
implement.
2. Some of the members(data or methods) of the class are hidden. Hence they can not
be accessed by the objects of other class.
3. In object oriented programming everything is arranged in the form of classes and
modules. For the lower level applications it is not desirable feature.
Functional Programming
In this paradigm the computations are expressed in the form of functions.
Functional programming paradigms treat values as single entities. Unlike variables,
values are never modified. Instead, values are transformed into new values.
Merits :
1. Due to use of functions the programs are easy to understand.
2. The functions are reusable.
3. It is possible to develop and maintain large programs consisting of large number of
functions.
Demerits :
1. Functional programming is less efficient than the other languages.
2. They consume large amount of time and memory for execution.
3. Purely functional programming is not a good option for commercial software
development.
Logic Programming
In this paradigm we express computation in terms of mathematical logic only. It is also
called as rule based programming approach.
Rules are specified in no special order.
It supports for declarative programming approach. In this approach how the
computations take place is explained.
The logic paradigm focuses on predicate logic, in which the basic concept is a relation.
Example of Logic programming is Prolog.
Merits :
1. This programming paradigm is reliable.
2. The program can be quickly developed using this approach as it makes use of
true/false statements rather than objects.
3. Itis best suitable for the problems in which knowledge base can be established.
Demerits :
1. Execution of the program is very slow.
2. True/false statements can not solve most of the problems.
Review Questions
Q.4 The feature by which the program that work on one platform can be modified on other
platform is called as
[al reusability [b] portability
robustness locality
Explanation : The context free grammar consists of set of production rules using which it
is possible to specify the syntactic rules of a language.
Q8 Aprogram that interprets each line of high level program at the time of execution is
called .
[al translator [b] interpreter
instructor [d] executor
Q.9 Aprogram which translates high level programming language into machine code is
called .
[al translator [b] compiler
[c] assembler [a] none of these
Q.11 The set of rules used in context free grammar are called as___
Q.12 Pascal inventor Niklaus Wirth designed the language specially for :
Q.16 What is the name of the category of programming languages whose structure is dictated by
the von Neuman computer architecture ?
Q.23 is a program that processes a program just before the program is compiled.
Qa b Q.2 d Q3 d Qa b
Qs a Q.6 a Q7 b Qs b
Q9 b Q.10 c Qa c Qaz c
Q.3 b Q.4 c Q.15 c Q.16 a
Q.17 a Q.18 c Q.19 c Q.20 a
Q.21 b Q.22 d Q.23 a
QOU