Computer programming

25
COMPUTER PROGRAMMING: A programming language is a set of instructions used to communicate with the computer and the set of instructions written in a programming language is called a Program. Programming is the implementation of logic to facilitate specified computing operations and functionality. Programming language semantics and syntax are used when constructing an application. Thus, programming requires knowledge of application domains, algorithms and programming language expertise. Programming language logic differs by developer. From a high level, good code can be evaluated with factors such as: Robustness: Focuses on program continuation capability, regardless of errors or incorrect data Reliability: Focuses on correct design and algorithm implementation Efficiency: Focuses on memory, hardware or other properties used to optimize programs Readability: Proper documentation and indentation availability, which provides insight to other program developers or designers It occurs in one or more languages, which differ by application, domain and programming model. Programming languages for computers are Suneel Dogra 1 SD College Hoshiarpur

description

 

Transcript of Computer programming

Page 1: Computer programming

COMPUTER PROGRAMMING:

A programming language is a set of instructions used to communicate with the

computer and the set of instructions written in a programming language is called

a Program. Programming is the implementation of logic to facilitate specified

computing operations and functionality.

Programming language semantics and syntax are used when constructing an

application. Thus, programming requires knowledge of application domains,

algorithms and programming language expertise. Programming language logic

differs by developer. From a high level, good code can be evaluated with factors

such as:

Robustness: Focuses on program continuation capability, regardless of

errors or incorrect data

Reliability: Focuses on correct design and algorithm implementation

Efficiency: Focuses on memory, hardware or other properties used to

optimize programs

Readability: Proper documentation and indentation availability, which

provides insight to other program developers or designers

It occurs in one or more languages, which differ by application, domain and

programming model. Programming languages for computers are developed with

the primary objective of facilitating a large number of persons to use computers

without the need to know in detail the internal structure of a computer.

Languages are matched to the type of applications which are to be programmed

using the language. The ideal language would be one which expresses precisely

the specification of a problem to be solved, and converts it into a series of

instructions for a computer. It is not possible to achieve this ideal as a clear

specification of a problem is often not available and developing an algorithm from

Suneel Dogra 1SD College Hoshiarpur

Page 2: Computer programming

specifications requires subject knowledge and expertise. In actual practice, a

detailed algorithm to solve a problem is the starting point and it is expressed as a

program in a programming language. A large number of languages, over a

thousand, exist each catering to a different class of applications. All modern

programming languages (with one exception) are designed to be machine

independent. In other words, the structure of the programming language would

not depend upon the internal structure of a specified computer; one should be

able to execute a program written in the programming language on any computer

regardless of who manufactured it or what model it is. Such languages are known

as high level machine independent programming languages. A computer

language means, a set of rules and symbols used to operate a computer.

Whatever command we give to computer, it is first converted in its own language.

A computer language is also known as Programming Language. There are various

types of programming languages. These languages are used to write programs to

tell the computer what to do. Users who write these programs are called

programmers. Each computer language has its own set of rules and grammar.

These are called the syntax rules of the language. This syntax rules should be

followed while writing a program. Computer Languages have progressed over the

years just like hardware. Starting from the language made up to just two symbols

0 and 1, today, you can make the computer programs using common English and

Mathematical terms.

CLASSIFICATION OF PROGRAMMING LANGUAGES:

Suneel Dogra 2SD College Hoshiarpur

Page 3: Computer programming

Classification of programming languages. We have classified high level machine

independent languages into three groups, namely, procedural, non-procedural

and Problem-oriented.

Classification of Programming Languages.

LOW LEVEL LANGUAGES:

The languages which use only primitive operations of the computer are known as

low language. In these languages, programs are written by means of the memory

and registers available on the computer. Since the architecture of computer

differs from one machine to another, so far each type of computer there is a

separate low level programming language. In the other words, Programs written

in one low level language of one, architectural can’t be ported on any other

machine dependent languages. Examples are Machine Language and Assembly

Language.

MACHINE LEVEL LANGUAGE

Suneel Dogra 3SD College Hoshiarpur

Page 4: Computer programming

You know that a computer can understand only special signals, which are

represented by 1’s and 0’s. These two digits are called binary digits. Computer

understands program written in binary digits. The language, which uses binary

digits, is called the machine level language. Computer works in bits and bytes. It

understands the language of the binary digits, 0 and 1. You may write a program

in whichever language you want, but it is finally converted into the language of 0s

and 1s before it gets executed. Writing a program in machine language is

definitely very difficult. It is not possible to memorize a long string of 0s and 1s for

every instruction that you want to derive executed. It is proper that before the

higher levels of programming languages were designed, machine languages were

old-fashioned for writing programming codes, but they are no longer traditional

for designing computer programs.

A machine language is a collection of very detailed cryptic instructions that

control computer’s internal circuitry. It is language that a computer actually

understands. It is viewed as sequences of 1`s and 0`s that a program written in

any language is finally translated to. In machine language program, the

computation is based on binary numbers. All the instructions including operations,

registers, data and memory locations are given in there binary equivalent.

The machine directly understands this language by virtue of its circuitry design so

these programs are directly executable on the computer without any translations.

This makes the program execution very fast. Machine languages are also known

as first generation languages.

A machine language consists of the numeric codes for the operations

that a particular computer can execute directly. The codes are strings of 0s and

1s, or binary digits (“bits”), which are frequently converted both from and to

hexadecimal (base 16) for human viewing and modification. Machine language

instructions typically use some bits to represent operations, such as addition, and

Suneel Dogra 4SD College Hoshiarpur

Page 5: Computer programming

some to represent operands, or perhaps the location of the next instruction.

Machine language is difficult to read and write, since it does not resemble

conventional mathematical notation or human language, and its codes vary from

computer to computer.

Machine language is the only language that a computer understands.

Each statement in a machine language program is a sequence of bits. Each bit

may be set to 0 or 1. Series of bits represent instructions that a computer can

understand. For example, the number 455 is represented by the bit sequence

111000111. Machine language is a low-level programming language. It is easily

understood by computers but difficult to read by people. This is why people use

higher level programming languages. Programs written in high-level languages

are compiled and/or interpreted into machine language so computers can execute

them.

ASSEMBLY LANGUAGE:

The first step in the evolution of programming languages was the development of

what is known as an assembly language. In an assembly language, mnemonics

are used to represent operations to be performed by the computer and strings of

characters to represent addresses of locations in the computer’s memory where

the operands will be stored. Thus the language is matched to a particular

computer’s processor structure and is thus machine dependent. A translator

called an assembler translates a program written in assembly language to a set of

machine instructions, which can be executed by a computer. Now-a-days

programs are written in assembly language only in applications which are cost

sensitive or time critical as efficiency of machine code is of paramount importance

in these types of applications. A cost sensitive application is one in which

microprocessors are used to enhance the functionality of consumer items such as

Suneel Dogra 5SD College Hoshiarpur

Page 6: Computer programming

washing machines or music systems. In these cases the program is stored in a

read only memory and its size is small. Thus code optimization

is important. A time-critical application is use of microprocessors in aircraft

controls where real time operation of the system is required. Here again the

number of machine instructions executed should be minimized.

Assembly languages are also known as second generation languages. These

languages substitutes alphabetic or numeric symbols for the binary codes of

machine language. That is, we can use mnemonics for all opcodes, registers and

for the memory locations which provide us with a facility to write reusable code in

the form of macros. Has two parts, one is macro name and the other is macro

body which contains the line of instructions. A macro can be called at any point of

the program by its name to use the instruction. A macro can be called at any

point of the program by its name to use the instructions given in the macro

repetitively.

These languages require a translator known as “Assembler” for translating the

program code written in assembly language to machine language. Because

computer can interpret only the machine code instruction, once the translation is

completed the program can be executed.

Assembly language is one level above machine language. It uses short mnemonic

codes for instructions and allows the programmer to introduce names for blocks of

memory that hold data. One might thus write “add pay, total” instead of

“0110101100101000” for an instruction that adds two numbers. Assembly

language is designed to be easily translated into machine language. Although

blocks of data may be referred to by name instead of by their machine addresses,

assembly language does not provide more sophisticated means of organizing

complex information. Like machine language, assembly language requires

detailed knowledge of internal computer architecture. It is useful when such

Suneel Dogra 6SD College Hoshiarpur

Page 7: Computer programming

details are important, as in programming a computer to interact with input/output

devices (printers, scanners, storage devices, and so forth).

Assembly language is a representation of machine language. In other words,

each assembly language instruction translates to a machine language instruction.

The advantage of assembly language is that its instructions are readable. For

example, assembly language statements like MOV and ADD are more

recognizable than sequences of 0s and 1s. Though assembly language statements

are readable, the statements are still low-level. Another disadvantage of assembly

language is that it is not portable. In other words, assembly language programs

are specific to a particular hardware. Assembly language programs for a Mac will

not work on a PC. But this can be an advantage for programmers who are

targeting a specific platform and need full control over the hardware.

HIGH LEVEL LANGUAGE

High-level languages are what most programmers use. A high level language is a

collection of instructions that resemble human languages. This provides more

compatibility of the language with the human thought process. E.g. FORTRAN,

BASIC, PASCAL, C, JAVA etc. Languages such as C++ and Java are all high-level

languages. One advantage of high-level languages is that they are very readable.

The statements in these languages are English-like. For example, you can gain a

basic understanding of what a Java program is doing by simply reading the

program source code. High-level languages use English words as statements.

Loops in Java programs are indicated by the words for, while and do. Another

advantage of high-level languages is that they are less tedious to use. A single

statement in a high-level language can translate into many machine language

statements. Finally, high-level languages are usually portable. A disadvantage of

high-level languages is that they are usually less powerful and less efficient. Since

Suneel Dogra 7SD College Hoshiarpur

Page 8: Computer programming

statements are high-level, you cannot code at the bit level the way you can with

assembly language. High-level languages also need to be compiled and/or

interpreted into machine language before execution.

High-level languages offer many advantages over low level languages and are

thus preferred for program development. Some main advantages are:

SIMPLICITY & PROGRAMMING EFFICIENCY: Generally, a single instruction in

a high-level language corresponds to a set of multiple machine language

instructions. This greatly enhances simplicity of program development i.e. leads

to higher programming efficiency and productivity.

UNIFORMITY: The rules that apply to the programming, in a particular high-level

language, for a particular computer are much the same for other computers also.

PORTABILITY: Uniformity supports high degree of portability on computers of

same type. Even if the instruction sets of the computers differ, it is the task of the

compiler to suit them (see below for compiler) and the rules for programming

remain essentially the same i.e. the same program can be run on different

computers. Since, portability depends on the compiler of the language (may be

available for the instruction set or not) or the libraries accompanying the

language, high degree of portability is the benefit provided by only few high-level

languages and absolute portability is still a big quest of the day.

Although, nearly whole of the programming world is dominated by the high - level

programming languages due to their higher program efficiency and productivity,

the assembly language still maintains its place in low level programming and

hardware interaction, especially for high performance systems.

Suneel Dogra 8SD College Hoshiarpur

Page 9: Computer programming

PROCEDURAL LANGUAGE:

Procedural language is a computer programming language that specifies a series

of well-structured steps and procedures within its programming context to

compose a program. It contains a systematic order of statements, functions and

commands to complete a computational task or program. Procedural language is

also known as imperative language.

Procedural language, as the name implies, relies on predefined and well-

organized procedures, functions or sub-routines in a program’s architecture by

specifying all the steps that the computer must take to reach a desired state or

output.

Procedural language segregates a program within variables, functions,

statements and conditional operators. Procedures or functions are implemented

on the data and variables to perform a task. These procedures can be

called/invoked anywhere between the program hierarchy, and by other

procedures as well. A program written in procedural language contains one or

more procedures.

Procedural language is one of the most common programming languages in use

with notable languages such as C/C++, Java, Cold Fusion and PASCAL.

Suneel Dogra 9SD College Hoshiarpur

Page 10: Computer programming

The procedural programming methodology involves dividing a large program

into a set of sub-procedures or subprograms that perform specific tasks. In

this methodology, a program is divided into one or more units or modules.

These modules can be user-defined or can be taken from libraries.

A module can consist of single or multiple procedures. These procedures are

also known as functions, routines, subroutines, or methods in various

programming languages.

A procedural program can consist of multiple levels or scopes. A procedure

can be defined independently or within another procedure. The level of the

procedure depends on the definition. Similarly, the data available in the

procedure also exhibits various levels or scopes. The procedure of a higher or

outer scope cannot access the data within the procedure that has lower or

inner scope.

A procedure or a subprogram is a set of commands that can be executed

independently. In a program following procedural methodology, each step of

a subprogram is linked to the previous step.

For example, in a program that needs to accept, display, and print data, you

can divide the program into subprograms. You can create three subprograms

that accept data, display data, and print data, respectively. Each subprogram

performs a defined function while the combined action of subprograms

makes a complete program.

In procedural programming, you can use a subprogram at multiple locations

within a program to perform a specific task.

Suneel Dogra 10SD College Hoshiarpur

Page 11: Computer programming

This enables you to reuse the program code as and when required in a

program, without rewriting the entire code, as shown in the figure:

The figure displays a program that consists of three procedures. Accept Data,

Display Data, and Print Data. Data is accepted in the Accept Data procedure,

displayed in the Display Data procedure, and printed in the Print Data

procedure.

Procedural programming is used for developing simple applications. The

languages that use the procedural programming methodology include Pascal

and C languages.

Some of the benefits of the procedural programming methodology are:

Easy to read program code.

Easy maintainable program code as various procedures can be

debugged in isolation.

Code is more flexible as you can change a specific procedure that gets

implemented across the program.

The features of procedural programming methodology are:

Large programs are divided into smaller programs.

Most of the data is shared as global that can be accessed from

anywhere within the program.

Suneel Dogra 11SD College Hoshiarpur

Page 12: Computer programming

In the procedural programming approach, portions of the code are so

interdependent that the code in one application cannot be reused in another.

For example, the module used to calculate the salary of the employees in a

bank management system cannot be used to calculate the salary of the

employees in an educational institute. You need to create a separate module

to calculate the salary of the employees in an educational institute.

When you need to reuse the procedural program in another application, a

change in the application results in rewriting a large portion of the code. This

results in decreased productivity and increased maintenance cost of the

application. This was one of the reasons that led to the evolution of the

object-oriented approach.

NON-PROCEDURAL LANGUAGE:

Programming languages that are based on functions or logic are

representatives of what is called declarative programming, due to the fact

that (to some extent) the users state what to be solved and the computers

solve it. Programs written in declarative languages are usually self-

explanatory, succinct, and much shorter than their counterparts in

procedural or object-oriented languages.

Non-procedural programming languages allow users and professional

programmers to specify the results they want without specifying how to solve

the problem. Examples are FORTRAN, C++, COBOL, ALGOL etc. A computer

language that does not require writing traditional programming logic. Also

Suneel Dogra 12SD College Hoshiarpur

Page 13: Computer programming

known as a "declarative language," users concentrate on defining the input

and output rather than the program steps required in a procedural

programming language such as C++ or Java. Functional programming is

about writing and composing functions. The central idea lies in symbolic

manipulation as computation -- the use of recursive functions defined over

lists provides a computational framework with the full power of expressing

deterministic computations. You will be presented with a brief overview of

functional programming in LISP, and you will use it to solve a number of

problems. We will introduce lambda calculus as a foundation for functional

programming, and an abstract machine called SECD as a model of executing

compiled lisp programs.

Logic programming draws the idea of programming as specifying solutions in

a logic and logic deduction as computation. You will learn how to turn a

subset of logic into a programming language. For the practical aspect, you

will write small yet interesting programs in Prolog (Programming in Logic).

We will also introduce constraint programming, perhaps one of the most

exciting developments in programming languages in the last decade.

PROBLEM-ORIENTED LANGUAGE:

These are high level language designed for developing a convenient

expression of the given class of problem.

STRUCTURED PROGRAMMING

Structured programming (sometimes known as modular programming) is a

subset of procedural programming that enforces a logical structure on the

program being written to make it more efficient and easier to understand and

Suneel Dogra 13SD College Hoshiarpur

Page 14: Computer programming

modify. Certain languages such as Ada, Pascal, and dBASE are designed with

features that encourage or enforce a logical program structure.

Structured programming frequently employs a top-down design model, in

which developers map out the overall program structure into separate

subsections. A defined function or set of similar functions is coded in a

separate module or sub module, which means that code can be loaded into

memory more efficiently and that modules can be reused in other programs.

After a module has been tested individually, it is then integrated with other

modules into the overall program structure.

Program flow follows a simple hierarchical model that employs looping

constructs such as "for," "repeat," and "while." Use of the "Go To" statement

is discouraged.

Structured programming was first suggested by Corrado Bohm and Guiseppe

Jacopini. The two mathematicians demonstrated that any computer program

can be written with just three structures: decisions, sequences, and loops.

Edsger Dijkstra's subsequent article, Go To Statement Considered Harmful

was instrumental in the trend towards structured programming. The most

common methodology employed was developed by Dijkstra. In this model

(which is often considered to be synonymous with structured programming,

although other models exist) the developer separates programs into

subsections that each have only one point of access and one point of exit.

Almost any language can use structured programming techniques to avoid

common pitfalls of unstructured languages. Unstructured programming must

rely upon the discipline of the developer to avoid structural problems, and as

a consequence may result in poorly organized programs. Most modern

Suneel Dogra 14SD College Hoshiarpur

Page 15: Computer programming

procedural languages include features that encourage structured

programming. Object-oriented programming (OOP) can be thought of as a

type of structured programming, uses structured programming techniques

for program flow, and adds more structure for data to the model.

STRUCTURED PROGRAMMING CAN BE DEFINED AS :

1. Top-Down Analysis for Program Solving

2. Modularization for program structure

3. Structure code for individual modules

TOP-DOWN ANALYSIS FOR PROGRAM SOLVING

A top-down approach (also known as stepwise design) is essentially the

breaking down of a system to gain insight into the sub-systems that make it

up. In a top-down approach an overview of the system is formulated,

specifying but not detailing any first-level subsystems. Each subsystem is

then refined in yet greater detail, sometimes in many additional subsystem

levels, until the entire specification is reduced to base elements. Once these

base elements are recognized then we can build these as computer modules.

Once they are built we can put them together, making the entire system

from these individual components.

Suneel Dogra 15SD College Hoshiarpur

Page 16: Computer programming

In recent years, computer memory ceased to be the limitation factor for most

of the application programs. This, along with increasing software complexity

and maintenance hitches, shifted the focus from the execution time to the

programming techniques adopted in development of a program. It allowed

programs to be written in a more organized manner, i.e., in a structured

manner, producing code that is easier to read, analyze, and modify later – if

the need arose. With increasing demands for software efficiency and

programming standardization, a changed approach saw the programmers

examining the problem as a whole and outlining the major steps to solve the

problem. Then the process was repeated and the steps thus obtained were

broken down in finer details. This is the top-down programming approach and

is used in structured programming.

1. Define exactly what data the program will get and what it has to do with

them.

2. If the task is simple enough, write the program code.

3. Otherwise, split the task into smaller parts and define exactly the duty of

each part and interface to the rest of the program.

4. Repeat the steps 1–4 separately for each subtask.

MODULAR PROGRAMMING

Modular programming (also called "top-down design" and "stepwise

refinement") is a software design technique that emphasizes separating the

functionality of a program into independent, interchangeable modules, such

that each contains everything necessary to execute only one aspect of the

desired functionality. Conceptually, modules represent a separation of

Suneel Dogra 16SD College Hoshiarpur

Page 17: Computer programming

concerns, and improve maintainability by enforcing logical boundaries

between components. Modules are typically incorporated into the program

through interfaces. A module interface expresses the elements that are

provided and required by the module. The elements defined in the interface

are detectable by other modules. The implementation contains the working

code that corresponds to the elements declared in the interface. Modular

programming is the process of subdividing a computer program into separate

sub-programs.

A module is a separate software component. It can often be used in a variety

of applications and functions with other components of the system. Similar

functions are grouped in the same unit of programming code and separate

functions are developed as separate units of code so that the code can be

reused by other applications.

Object-oriented programming (OOP) is compatible with the modular

programming concept to a large extent. Modular programming enables

multiple programmers to divide up the work and debug pieces of the

program independently.

Modules in modular programming enforce logical boundaries between

components and improve maintainability. They are incorporated through

interfaces. They are designed in such a way as to minimize dependencies

between different modules. Teams can develop modules separately and do

not require knowledge of all modules in the system.

Each and every modular application has a version number associated with it.

This provides developers flexibility in module maintenance. If any changes

have to be applied to a module, only the affected subroutines have to be

Suneel Dogra 17SD College Hoshiarpur

Page 18: Computer programming

changed. This makes the program easier to read and understand.

Modular programming has a main module and many auxiliary modules. The

main module is compiled as an executable (EXE), which calls the auxiliary

module functions. Auxiliary modules exist as separate executable files, which

load when the main EXE runs. Each module has a unique name assigned in

the PROGRAM statement. Function names across modules should be unique

for easy access if functions used by the main module must be exported.

Languages that support the module concept are IBM Assembler, COBOL, RPG,

FORTRAN, Morpho, Zonnon and Erlang, among others.

The benefits of using modular programming include:

1. Less code has to be written.

2. A single procedure can be developed for reuse, eliminating the need to retype the code

many times.

3. Programs can be designed more easily because a small team deals with only a small

part of the entire code.

4. Modular programming allows many programmers to collaborate on the same

application.

5. The code is stored across multiple files.

6. Code is short, simple and easy to understand.

7. Errors can easily be identified, as they are localized to a subroutine or function.

8. The same code can be used in many applications.

9. The scoping of variables can easily be controlled.

Suneel Dogra 18SD College Hoshiarpur

Page 19: Computer programming

Suneel Dogra 19SD College Hoshiarpur