Vous êtes sur la page 1sur 37

1.1. What is Programming?

A problem is defined as any question, something involving doubt, uncertainty, difficulty or a


situation whose solution is not immediately obvious. A situation which is a problem for one
person may not be a problem for another person. Figure 1.1 graphically represents the concept of
problem solving.

A Computer is a tool that can be used to solve problems by writing programs. Writing a program
without knowing the problem that we are trying to solve is like swinging a knife without
knowing what it is that we want to cut.

A program is a set of instructions that tells the computer how to do a task. When a computer
follows these instructions, we say it executes the program

Fig 1.1: Problem Solving

A programming language is a computer language that programmers use to develop applications.


An application is a set of instructions for a computer to execute. A set of rules that provides a
way of telling a computer what operations to perform is called a Program.

Programming is the process of designing, developing, testing, debugging and maintaining the
source code of computer programs. It is used to create a set of instructions that computers use to
perform specific operations or to exhibit desired behaviors. Source code can be written using
programming language such as C++, Java, Python, Smalltalk, C#, etc. The process of writing
source code often requires expertise in many different subjects, including knowledge of the
specialized algorithms, application domain and formal logic.

Within software engineering, programming (implementation) is considered as a phase in the


software development process.

The Programming Process

Developing a program involves different steps similar to any problem solving task. There are
five main steps in the programming process. They are as follows:

1. Defining the problem


2. Planning the solution

3. Coding the program

4. Testing the program

5. Documenting the program

Let us discuss each of these steps.

1. Defining the problem

As a programmer, you meet with end users from the client organization to analyze the problem,
or you meet with a systems analyst who outlines the project. Specifically, the task of defining the
problem consists of identifying what you know (input-given data), and what you want to obtain
(output-the result). Eventually, you create a document or an agreement that specifies the kind of
input, processing, and output required to complete the task.

2. Planning the solution

There are two ways that are commonly used to plan the solution to a problem. These are to draw
a flowchart and to write pseudo code, or both.

Pseudo code

Pseudo code is an English-like non-standard langauge that permits you to focus on the program
logic without having to be concerned about the precise syntax of a particular programming
language. However, pseudo code is not executable on the computer. It lets you state your
solution with more precision than plain English. However psuedo code has lesser precision than
is required when using a formal programming language. An example is given below:

Example : Read number n and print the integers counting up to n.

Read n

Initialize the variable i to 1

while i 'less than or equal to n' do

Print i

Increment i

end while

Stop
In the above example, the program enters the while loop only if the condition (i less than or
equal to n) is true. Two statements Print i and Increment i are executed for each iteration. At the
end of each iteration, the condition is evaluated again and the loop continues as long as the
condition is true.

Flowchart

It is a pictorial representation of a step-by-step solution to a problem. It consists of arrows


representing the direction the program takes and boxes and other symbols representing actions. It
is a map of the steps involved in a program. The American National Standards Institute (ANSI)
has come up with a standard set of flowchart symbols. The following figure shows the flowchart
symbols and how they might be used in a simple flowchart of an everyday action preparing a
letter for mailing.

Figure 1.2: Flowchart

Please refer the links below to learn more about pseudo code and flow chart.
3. Coding the program

As a programmer, your next step is to code the program to express the solution in a programming
language. You will translate the logic from the flowchart or pseudo code to a programming
language. As we have already discussed, a programming language is a set of rules that provides a
way of instructing the computer what operations to perform. There are many programming
languages such as BASIC, COBOL, Pascal, FORTRAN, C, etc.

Programming languages are precise like the English language. To get your program to work, you
have to follow the exact syntax (rules) of the language you are using. However, there is no
guarantee that your program will work because the syntax in programming language is correct.
The correctness of the language is the first step that you require. After that, the program must be
keyed, probably using a personal computer, in a form that the computer can understand.

4. Testing the program

Some experts claim that a well designed program can be written correctly the first time and there
are mathematical ways to prove that a program is correct. Most of the programs may have a few
bugs (errors) when executed for the first time. This is a little discouraging at first, since
programmers tend to be careful, precise and detail-oriented people who take pride in their work.
There are many ways in which mistakes or problems may happen in programs. You will probably
have made some of them. Eventually, after coding, we must prepare to test the program on the
computer.

Testing involves three phases:

1. Desk-checking

2. Translating

3. Debugging

Let us discuss each of them.

Desk-checking

This is similar to proofreading. This phase is sometimes avoided by the developer/programmer


who is looking for a shortcut and is eager to run the program on the computer once it is written.
However, with careful desk-checking we can identify several errors and possibly save our time in
the long run. In desk-checking, you simply sit down and mentally check or trace the logic of the
program in an attempt to ensure that it is error-free and is in workable condition. Many
organizations take this phase a step further called walk through, a process in which a group of
programmers review the program and make suggestions or comments in a collegial way.
Translating

A translator is a program that

I. Checks the syntax of the program to make sure that the programming language was used
correctly, giving you all the syntax related error messages called diagnostics, and

II. Then translates the program into a form which the computer can understand. A by-product of
the process is that the translator tells us if we have improperly used the programming language.
These mistakes are called syntax errors. The translator gives descriptive error messages.

Programs are most commonly translated using a compiler. A compiler translates the entire
program at one time. The translation involves your original program (Source file) which is
transformed by a compiler into an object module. Pre-written programs from a system library
may be added during the load/link phase, which results in a load module. The load module can
then be executed by the computer.

Debugging

Debugging is used extensively in programming. It means detecting, locating, and correcting bugs
(mistakes) by running the program. These bugs are called as logic errors, which tells the
computer to repeat an operation but not telling it when to stop repeating. In this phase we run the
program using the test data that we devise. We must plan the test data carefully to make sure we
test every part of the program.

5. Documenting the program

Documenting is an ongoing process. It is a detailed description of the programming cycle and


specific facts about the program in the written form.

Typical program documentation include the origin and nature of the problem, a brief description
of the program, logic tools such as pseudo code and flowcharts, program listings, data-record
descriptions and test results. Comments in the program are also considered as an essential part of
documentation. Many programmers document as they code.

A wise programmer continues to document the program throughout its design, development and
testing. Documentation is used to supplement human memory and to help organize program
planning. Documentation is also critical to communicate with others who have an interest in the
program and may be part of a programming team.

1.2. Why programming is required?

We may have already used software, in the form of spreadsheets or word processing, or to solve
problems. Perhaps we are now curious to learn how programmers write software. A program is a
set of step-by-step instructions that directs the computer to do the tasks we want it to do and
produce the results we want.

There are three good reasons for learning programming:

1. Programming helps us to understand computers. If we learn how to write simple programs, we


will know better about how a computer works.

2. Writing a few simple programs increases our confidence level. We will find great personal
satisfaction in creating a set of instructions that solvea a problem.

3. Learning programming lets us find out quickly whether we like programming and whether we
have the analytical turn of mind that programmers need. Even if we decide that programming is
not for us, understanding the process certainly will increase our appreciation of what computers
and programmers can do.

What Programmers Do?

The programmer prepares the set of instructions of a computer program and runs those
instructions on the computer, tests the program to see if it is working properly, and then makes
corrections to it. These activities are all done for the purpose of helping a user to fill a need, such
as paying employees, admitting students to college or billing customers.

Programming can be done as a solo activity, but a programmer typically interacts with a variety
of people. For example, if a program is a part of a system of several programs, the programmer
coordinates with the other programmers to make sure that all the programs fit together correctly.
If you are a programmer, you might also have coordination meetings with users, systems
analysts, managers, and with peers who evaluate your work just as you evaluate theirs.

Levels of Language

Programming languages can be "lower" or "higher," depending on how close they are to the
language the computer itself uses (0s and 1s = low) or to the language people use (English like
high). We will consider five levels of language. These are numbered 1 through 5 to correspond to
generations. In terms of ease of use and capabilities, each generation or level is an improvement
over its predecessor.

The five generations of languages are:

1. Machine languages

2. Assembly languages
3. High-level languages

4. Very high-level languages

5. Natural languages

Let us look at each of these categories

1. Machine languages

Humans do not like to deal with numbers alone; they prefer letters and words also. Machine
language consists of numbers. Each type of computer has its own machine language. This is the
lowest level of language. It represents data and program instructions as 1s and 0s- the binary
digits corresponding to the on and off electrical states in the computer. During the early days of
computing, programmers used rudimentary systems for combining numbers to represent
instructions such as add or compare. These programs are not convenient for people to read and
use, and the industry quickly moved to develop assembly languages.

2. Assembly languages

Assembly languages are considered to be very low level languages. They are not as convenient
to use as the more recent languages. However, at the time when they were developed, they were
considered as a great leap forward. To replace the 1s and 0s used in machine language, the
assembly languages use mnemonic codes. Mnemonic codes are abbreviations that are easy to
remember: A for add, C for compare, MP for Multiply and so on. Although these codes are not
English words, they were better accepted than numbers (0s and 1s) alone. Just like machine
language, each type of computer has its own assembly language.

The programmer who uses an assembly language requires a translator to convert the assembly
language program into machine language. A translator is required because machine language is
the only language that the computer can actually execute. The translator is an assembler
program, which is also referred to as an assembler. It takes the programs written in assembly
language and converts them into machine language. Programmers need not worry about the
translating aspect, they only need to write programs in assembly language.

Although assembly languages represent a step forward, they still have many disadvantages. A
key disadvantage is that assembly language is detailed to the extreme, making assembly
language programming repetitive, error prone and tedious.

3. High-Level Languages

The widespread use of high-level languages in the early 1960's transformed programming into
something quite different from what it had been. Programs were written in an English-like
manner, thus making them easier and more convenient to use. Using high-level languages, a
programmer can accomplish more with less effort, and programs can now take care of much
more complex tasks.
These so-called third generation languages spurred a great increase in data processing that
characterized the 1960's and 1970's. During that period, the number of mainframes in use
increased from hundreds to tens of thousands.

A translator is used to translate the symbolic statements of a high level language into computer
executable machine language. This translator is called a compiler. There are many compilers for
each language and one for each type of computer. For example, it is necessary to have a COBOL
compiler for each type of computer on which COBOL programs are to be run. A program can be
compiled to different machine language versions on different machines. The source program
itself, that is, the COBOL version is essentially identical on each machine.

Some languages are used to serve a specific purpose, such as creating graphics or controlling
industrial robots. Many languages are extraordinarily flexible and are considered to be general-
purpose languages. In the past, the majority of programming applications were written in
BASIC, COBOL or FORTRAN. All of them are general purpose languages.

4. Very High-Level Languages

Very high-level languages are often known by their generation number. They are called fourth-
generation languages or 4GLs. Fourth-generation languages are beneficial because:

I.They are result-oriented; they emphasize "what" instead of "how".

II.They improve productivity because programs are easy to write and modify.

III.They can be used with minimum training by both programmers and non-programmers.

IV.They shield users from needing an awareness of program structure and hardware.

5. Natural Languages

The word "natural" has become very popular in computing circles. Fifth generation languages
are often called natural languages because of their resemblance to the "natural" spoken English
language. Instead of being forced to key correct commands and data names in correct order, a
manager tells the computer what to do by keying in their own words.

Choosing a Language

In a work environment, the manager may decide that everyone in the project will use a
certain language.
We may need to use a certain language, particularly in a business environment, based on
the need to interface with other programs. If two programs are to work together, it
becomes easy when they are written in the same language.

We may choose a language based on its suitability or criteria. For example, COBOL will
be the best option for a business program that handles large files.

If a program is to be run on different computers, it must be written in a language that is


portable and suitable on each type of computer, so that the program needs to be written
only once.

We may be limited by the availability of languages. Not all languages are available on all
computers and in all installations.

The language may be limited to the expertise of a programmer. The program may have to
be written in a language that the available programmer knows.

Major Programming Languages are FORTRAN (a scientific language), COBOL (a business


language), BASIC (simple language used for education and business), Pascal (education), Ada
(military), and C (general purposed), C, C++, Java, and Javascript.
EXTRA

Computer Programming

Why Programming?
You may already have used software, perhaps for word processing or
spreadsheets, to solve problems. Perhaps now you are curious to learn how
programmers write software. A program is a set of step-by-step instructions
that directs the computer to do the tasks you want it to do and produce the
results you want.

There are at least three good reasons for learning programming:

o Programming helps you understand computers. The computer is only a


tool. If you learn how to write simple programs, you will gain more
knowledge about how a computer works.

o Writing a few simple programs increases your confidence level. Many


people find great personal satisfaction in creating a set of instructions
that solve a problem.

o Learning programming lets you find out quickly whether you like
programming and whether you have the analytical turn of mind
programmers need. Even if you decide that programming is not for
you, understanding the process certainly will increase your
appreciation of what programmers and computers can do.

A set of rules that provides a way of telling a computer what operations to


perform is called a programming language. There is not, however, just one
programming language; there are many. In this chapter you will learn about
controlling a computer through the process of programming. You may even
discover that you might want to become a programmer.

An important point before we proceed: You will not be a programmer when


you finish reading this chapter or even when you finish reading the final
chapter. Programming proficiency takes practice and training beyond the
scope of this book. However, you will become acquainted with how
programmers develop solutions to a variety of problems.

What Programmers Do
In general, the programmer's job is to convert problem solutions into
instructions for the computer. That is, the programmer prepares the
instructions of a computer program and runs those instructions on the
computer, tests the program to see if it is working properly, and makes
corrections to the program. The programmer also writes a report on the
program. These activities are all done for the purpose of helping a user fill a
need, such as paying employees, billing customers, or admitting students to
college.

The programming activities just described could be done, perhaps, as solo


activities, but a programmer typically interacts with a variety of people. For
example, if a program is part of a system of several programs, the
programmer coordinates with other programmers to make sure that the
programs fit together well. If you were a programmer, you might also have
coordination meetings with users, managers, systems analysts, and with
peers who evaluate your work-just as you evaluate theirs.

Let us turn to the programming process.

The Programming Process


Developing a program involves steps similar to any problem-solving task.
There are five main ingredients in the programming process:

1. Defining the problem

2. Planning the solution

3. Coding the program

4. Testing the program

5. Documenting the program

Let us discuss each of these in turn.

6. Defining the Problem


Suppose that, as a programmer, you are contacted because your services are
needed. You meet with users from the client organization to analyze the
problem, or you meet with a systems analyst who outlines the project.
Specifically, the task of defining the problem consists of identifying what it is
you know (input-given data), and what it is you want to obtain (output-the
result). Eventually, you produce a written agreement that, among other
things, specifies the kind of input, processing, and output required. This is not
a simple process.
7. Planning the Solution

8.
T

wo common ways of planning the solution to a problem are to draw a flowchart


and to write pseudocode, or possibly both. Essentially, a flowchart is a pictorial
representation of a step-by-step solution to a problem. It consists of arrows
representing the direction the program takes and boxes and other symbols
representing actions. It is a map of what your program is going to do and how it
is going to do it. The American National Standards Institute (ANSI) has developed
a standard set of flowchart symbols. Figure 1 shows the symbols and how they
might be used in a simple flowchart of a common everyday act-preparing a letter
for mailing.

Pseudocode is an English-like nonstandard language that lets you state your


solution with more precision than you can in plain English but with less precision
than is required when using a formal programming language. Pseudocode
permits you to focus on the program logic without having to be concerned just
yet about the precise syntax of a particular programming language. However,
pseudocode is not executable on the computer. We will illustrate these later in
this chapter, when we focus on language examples.

9. Coding the Program


As the programmer, your next step is to code the program-that is, to express
your solution in a programming language. You will translate the logic from the
flowchart or pseudocode-or some other tool-to a programming language. As we
have already noted, a programming language is a set of rules that provides a
way of instructing the computer what operations to perform. There are many
programming languages: BASIC, COBOL, Pascal, FORTRAN, and C are some
examples. You may find yourself working with one or more of these. We will
discuss the different types of languages in detail later in this chapter.

Although programming languages operate grammatically, somewhat like the


English language, they are much more precise. To get your program to work, you
have to follow exactly the rules-the syntax-of the language you are using. Of
course, using the language correctly is no guarantee that your program will
work, any more than speaking grammatically correct English means you know
what you are talking about. The point is that correct use of the language is the
required first step. Then your coded program must be keyed, probably using a
terminal or personal computer, in a form the computer can understand.

One more note here: Programmers usually use a text editor, which is somewhat
like a word processing program, to create a file that contains the program.
However, as a beginner, you will probably want to write your program code on
paper first.
10. Testing the Program
Some experts insist that a well-designed program can be written
correctly the first time. In fact, they assert that there are mathematical
ways to prove that a program is correct. However, the imperfections of
the world are still with us, so most programmers get used to the idea
that their newly written programs probably have a few errors. This is a
bit discouraging at first, since programmers tend to be precise, careful,
detail-oriented people who take pride in their work. Still, there are
many opportunities to introduce mistakes into programs, and you, just
as those who have gone before you, will probably find several of them.

Eventually, after coding the program, you must prepare to test it on


the computer. This step involves these phases:

Desk-checking. This phase, similar to proofreading, is


sometimes avoided by the programmer who is looking for a
shortcut and is eager to run the program on the computer once
it is written. However, with careful desk-checking you may
discover several errors and possibly save yourself time in the
long run. In desk-checking you simply sit down and mentally
trace, or check, the logic of the program to attempt to ensure
that it is error-free and workable. Many organizations take this
phase a step further with a walkthrough, a process in which a
group of programmers-your peers-review your program and offer
suggestions in a collegial way.

Translating. A translator is a program that (1) checks the


syntax of your program to make sure the programming language
was used correctly, giving you all the syntax-error messages,
called diagnostics, and (2) then translates your program into a
form the computer can understand. A by-product of the process
is that the translator tells you if you have improperly used the
programming language in some way. These types of mistakes
are called syntax errors. The translator produces descriptive
error messages. For instance, if in FORTRAN you mistakenly
write N=2 *(I+J))-which has two closing parentheses instead of
one-you will get a message that says, "UNMATCHED
PARENTHESES." (Different translators may provide different
wording for error messages.) Programs are most commonly
translated by a compiler. A compiler translates your entire
program at one time. The translation involves your original
program, called a source module, which is transformed by a
compiler into an object module. Prewritten programs from a
system library may be added during the link/load phase, which
results in a load module. The load module can then be executed
by the computer.

Debugging. A term used extensively in programming,


debugging means detecting, locating, and correcting bugs
(mistakes), usually by running the program. These bugs are logic
errors, such as telling a computer to repeat an operation but not
telling it how to stop repeating. In this phase you run the
program using test data that you devise. You must plan the test
data carefully to make sure you test every part of the program.

11. Documenting the Program


Documenting is an ongoing, necessary process, although, as many
programmers are, you may be eager to pursue more exciting
computer-centered activities. Documentation is a written detailed
description of the programming cycle and specific facts about the
program. Typical program documentation materials include the origin
and nature of the problem, a brief narrative description of the program,
logic tools such as flowcharts and pseudocode, data-record
descriptions, program listings, and testing results. Comments in the
program itself are also considered an essential part of documentation.
Many programmers document as they code. In a broader sense,
program documentation can be part of the documentation for an entire
system.

The wise programmer continues to document the program throughout


its design, development, and testing. Documentation is needed to
supplement human memory and to help organize program planning.
Also, documentation is critical to communicate with others who have
an interest in the program, especially other programmers who may be
part of a programming team. And, since turnover is high in the
computer industry, written documentation is needed so that those who
come after you can make any necessary modifications in the program
or track down any errors that you missed.

2 Programming as a Career
There is a shortage of qualified personnel in the computer field. Before you
join their ranks, consider the advantages of the computer field and what it
takes to succeed in it.

The Joys of the Field


Although many people make career changes into the computer field, few
choose to leave it. In fact, surveys of computer professionals, especially
programmers, consistently report a high level of job satisfaction. There are
several reasons for this contentment. One is the challenge-most jobs in the
computer industry are not routine. Another is security, since established
computer professionals can usually find work. And that work pays well-you
will probably not be rich, but you should be comfortable. The computer
industry has historically been a rewarding place for women and minorities.
And, finally, the industry holds endless fascination since it is always changing.
What It Takes
You need, of course, some credentials, most often a two- or four-year degree
in computer information systems or computer science. The requirements and
salaries vary by the organization and the region, so we will not dwell on these
here. Beyond that, the person most likely to land a job and move up the
career ladder is the one with excellent communication skills, both oral and
written . These are also the qualities that can be observed by potential
employers in an interview. Promotions are sometimes tied to advanced
degrees (an M.B.A. or an M.S. in computer science).

Open Doors
The overall outlook for the computer field is promising. The Bureau of Labor
Statistics shows, through the year 2010, a 72 percent increase in
programmers and a 69 percent increase in system use today, and we will
discuss the most popular ones later In the chapter. Before we turn to specific
languages, however, we need to discuss levels of language.

3 Levels of Language
Programming languages are said to be "lower" or "higher," depending on how
close they are to the language the computer itself uses (Os and 1s = low) or
to the language people use (more English-like-high). We will consider five
levels of language. They are numbered 1 through 5 to correspond to levels, or
generations. In terms of ease of use and capabilities, each generation is an
improvement over its predecessors. The five generations of languages are

1. Machine language

2. Assembly languages

3. High-level languages

4. Very high-level languages

5. Natural languages

Let us look at each of these categories.

Machine Language
Humans do not like to deal in numbers alone-they prefer letters and words.
But, strictly speaking, numbers are what machine language is. This lowest
level of language, machine language, represents data and program
instructions as 1s and Os-binary digits corresponding to the on and off
electrical states in the computer. Each type of computer has its own machine
language. In the early days of computing, programmers had rudimentary
systems for combining numbers to represent instructions such as add and
compare. Primitive by today's standards, the programs were not convenient
for people to read and use. The computer industry quickly moved to develop
assembly languages.

Assembly Languages

Figure 2: Example Assembly Language Program

Today, assembly languages are considered very low level-that is, they are not as
convenient for people to use as more recent languages. At the time they were
developed, however, they were considered a great leap forward. To replace the Is
and Os used in machine language, assembly languages use mnemonic codes,
abbreviations that are easy to remember: A for Add, C for Compare, MP for Multiply,
STO for storing information in memory, and so on. Although these codes are not
English words, they are still- from the standpoint of human convenience-preferable
to numbers (Os and 1s) alone. Furthermore, assembly languages permit the use of
names- perhaps RATE or TOTAL-for memory locations instead of actual address
numbers. just like machine language, each type of computer has its own assembly
language.

The programmer who uses an assembly language requires a translator to convert


the assembly language program into machine language. A translator is needed
because machine language is the only language the computer can actually execute.
The translator is an assembler program, also referred to as an assembler. It takes
the programs written in assembly language and turns them into machine language.
Programmers need not worry about the translating aspect; they need only write
programs in assembly language. The translation is taken care of by the assembler.

Although assembly languages represent a step forward, they still have many
disadvantages. A key disadvantage is that assembly language is detailed in the
extreme, making assembly programming repetitive, tedious, and error prone. This
drawback is apparent in the program in Figure 2. Assembly language may be easier
to read than machine language, but it is still tedious.

High-Level Languages
The first widespread use of high-level languages in the early 1960s transformed
programming into something quite different from what it had been. Programs were
written in an English-like manner, thus making them more convenient to use. As a
result, a programmer could accomplish more with less effort, and programs could
now direct much more complex tasks.

These so-called third-generation languages spurred the great increase in data


processing that characterized the 1960s and 1970s. During that time the number of
mainframes in use increased from hundreds to tens of thousands. The impact of
third-generation languages on our society has been enormous.

Of course, a translator is needed to translate the symbolic statements of a high-


level language into computer-executable machine language; this translator is
usually a compiler. There are many compilers for each language and one for each
type of computer. Since the machine language generated by one computer's COBOL
compiler, for instance, is not the machine language of some other computer, it is
necessary to have a COBOL compiler for each type of computer on which COBOL
programs are to be run. Keep in mind, however, that even though a given program
would be compiled to different machine language versions on different machines,
the source program itself-the COBOL version-can be essentially identical on each
machine.

Some languages are created to serve a specific purpose, such as controlling


industrial robots or creating graphics. Many languages, however, are
extraordinarily flexible and are considered to be general-purpose. In the past
the majority of programming applications were written in BASIC, FORTRAN, or
COBOL-all general-purpose languages. In addition to these three, another
popular high-level language is C, which we will discuss later.

Very High-Level Languages


Languages called very high-level languages are often known by their
generation number, that is, they are called fourth-generation languages or,
more simply, 4GLs.

Definition
Will the real fourth-generation languages please stand up? There is no
consensus about what constitutes a fourth-generation language. The 4GLs
are essentially shorthand programming languages. An operation that requires
hundreds of lines in a third-generation language such as COBOL typically
requires only five to ten lines in a 4GL. However, beyond the basic criterion of
conciseness, 4GLs are difficult to describe.

Characteristics
Fourth-generation languages share some characteristics. The first is that they
make a true break with the prior generation-they are basically non-
procedural. A procedural language tells the computer how a task is done: Add
this, compare that, do this if something is true, and so forth-a very specific
step-by-step process. The first three generations of languages are all
procedural. In a nonprocedural language, the concept changes. Here, users
define only what they want the computer to do; the user does not provide the
details of just how it is to be done. Obviously, it is a lot easier and faster just
to say what you want rather than how to get it. This leads us to the issue of
productivity, a key characteristic of fourth-generation languages.

Productivity
Folklore has it that fourth-generation languages can improve productivity by
a factor of 5 to 50. The folklore is true. Most experts say the average
improvement factor is about 10-that is, you can be ten times more productive
in a fourth-generation language than in a third-generation language. Consider
this request: Produce a report showing the total units sold for each product,
by customer, in each month and year, and with a subtotal for each customer.
In addition, each new customer must start on a new page. A 4GL request
looks something like this:
TABLE FILE SALES
SUM UNITS BY MONTH BY CUSTOMER BY PRODUCT
ON CUSTOMER SUBTOTAL PAGE BREAK
END

Even though some training is required to do even this much, you can see that
it is pretty simple. The third-generation language COBOL, however, typically
requires over 500 statements to fulfill the same request. If we define
productivity as producing equivalent results in less time, then fourth-
generation languages clearly increase productivity.

Downside
Fourth-generation languages are not all peaches and cream and productivity.
The 4GLs are still evolving, and that which is still evolving cannot be fully
defined or standardized. What is more, since many 4GLs are easy to use, they
attract a large number of new users, who may then overcrowd the computer
system. One of the main criticisms is that the new languages lack the
necessary control and flexibility when it comes to planning how you want the
output to look. A common perception of 4GLs is that they do not make
efficient use of machine resources; however, the benefits of getting a
program finished more quickly can far outweigh the extra costs of running it.

Benefits
Fourth-generation languages are beneficial because

o They are results-oriented; they emphasize what instead of how.

o They improve productivity because programs are easy to write and


change.

o They can be used with a minimum of training by both programmers


and nonprogrammers.

o They shield users from needing an awareness of hardware and


program structure.

It was not long ago that few people believed that 4GLs would ever be able to
replace third-generation languages. These 4GL languages are being used, but
in a very limited way.

Query Languages
A variation on fourth-generation languages are query languages, which can
be used to retrieve information from databases. Data is usually added to
databases according to a plan, and planned reports may also be produced.
But what about a user who needs an unscheduled report or a report that
differs somehow from the standard reports? A user can learn a query
language fairly easily and then be able to input a request and receive the
resulting report right on his or her own terminal or personal computer. A
standardized query language, which can be used with several different
commercial database programs, is Structured Query Language, popularly
known as SQL. Other popular query languages are Query-by-Example, known
as QBE, and Intellect.

Natural Languages
The word "natural" has become almost as popular in computing circles as it
has in the supermarket. Fifth-generation languages are, as you may guess,
even more ill-defined than fourth-generation languages. They are most often
called natural languages because of their resemblance to the "natural"
spoken English language. And, to the manager new to computers for whom
these languages are now aimed, natural means human-like. Instead of being
forced to key correct commands and data names in correct order, a manager
tells the computer what to do by keying in his or her own words.

Figure 3: Example of Natural Language Interaction

A manager can say the same thing any number of ways. For example, "Get me
tennis racket sales for January" works just as well as "I want January tennis racket
revenues." Such a request may contain misspelled words, lack articles and verbs,
and even use slang. The natural language translates human instructions-bad
grammar, slang, and all-into code the computer understands. If it is not sure what
the user has in mind, it politely asks for further explanation.
Natural languages are sometimes referred to as knowledge-based languages,
because natural languages are used to interact with a base of knowledge on some
subject. The use of a natural language to access a knowledge base is called a
knowledge-based system.

Consider this request that could be given in the 4GL Focus: "SUM ORDERS BY DATE
BY REGION." If we alter the request and, still in Focus, say something like "Give me
the dates and the regions after you've added up the orders," the computer will spit
back the user-friendly version of "You've got to be kidding" and give up. But some
natural languages can handle such a request. Users can relax the structure of their
requests and increase the freedom of their interaction with the data.

Here is a typical natural language request:

REPORT THE BASE SALARY, COMMISSIONS AND YEARS OF


SERVICE BROKEN DOWN BY STATE AND CITY FOR SALESCLERKS
IN NEW JERSEY AND MASSACHUSETTS.

You can hardly get closer to conversational English than that.

An example of a natural language is shown in Figure 3. Natural languages


excel at easy data access. Indeed, the most common application for natural
languages is interacting with databases.

2 Choosing a Language
How do you choose the language with which to write your program?
There are several possibilities:

o In a work environment, your manager may decree that everyone on


your project will use a certain language.

o You may use a certain language, particularly in a business


environment, based on the need to interface with other programs; if
two programs are to work together, it is easiest if they are written in
the same language.

o You may choose a language based on its suitability for the task. For
example, a business program that handles large files may be best
written in the business language COBOL.

o If a program is to be run on different computers, it must be written in a


language that is portable-suitable on each type of computer-so that the
program need be written only once.
o You may be limited by the availability of the language. Not all
languages are available in all installations or on all computers.

o The language may be limited to the expertise of the programmer; that


is, the program may have to be written in a language the available
programmer knows.

o Perhaps the simplest reason, one that applies to many amateur


programmers, is that they know the language called BASIC because it
came with-or was inexpensively purchased with-their personal
computers.

3 Major Programming Languages


Figure 4: Flow Chart For Averaging Numbers

4 The following sections on individual languages will give you an overview of


the third-generation languages in common use today: FORTRAN (a scientific
language), COBOL (a business language), BASIC (simple language used for
education and business), Pascal (education), Ada (military), and C (general
purposed).

This chapter will present programs written in some of these languages. You
will also see output produced by each program. Each program is designed to
find the average of three numbers; the resulting average is shown in the
sample output matching each program. Since all programs perform the same
task, you will see some of the differences and similarities among the
languages. We do not expect you to understand these programs; they are
here merely to let you glimpse each language. Figure 4 presents the
flowchart and pseudocode for the task of averaging numbers. As we discuss
each language, we will provide a program for averaging numbers that follows
the logic shown in this figure.

FORTRAN: The First High-Level Language

Figure 5: Example Fortran Program To Average Numbers


5 Developed by IBM and introduced in 1954, FORTRAN-for FORmula TRANslator-
was the first high-level language. FORTRAN is a scientifically oriented
language-in the early days use of the computer was primarily associated with
engineering, mathematical, and scientific research tasks.

FORTRAN is noted for its brevity, and this characteristic is part of the reason
why it remains popular. This language is very good at serving its primary
purpose, which is execution of complex formulas such as those used in
economic analysis and engineering. Although in the past it was considered
limited in regard to file processing or data processing, its capabilities have
been greatly improved.

Not all programs are organized in the same way. Organization varies
according to the language used. In many languages (such as COBOL),
programs are divided into a series of parts. FORTRAN programs are not
composed of different parts (although it is possible to link FORTRAN programs
together); a FORTRAN program consists of statements one after the other.
Different types of data are identified as the data is used. Descriptions for data
records appear in format statements that accompany the READ and WRITE
statements. Figure 5 shows a FORTRAN program and a sample output from
the program.

COBOL: The Language of Business


6 In the 1950s FORTRAN had been developed, but there was still no accepted
high-level programming language appropriate for business. The U.S.
Department of Defense in particular was interested in creating such a
standardized language, and so it called together representatives from
government and various industries, including the computer industry. These
representatives formed CODASYL-COnference of DAta SYstem Languages. In
1959 CODASYL introduced COBOL-for COmmon BusinessOriented Language.

The U.S. government offered encouragement by insisting that anyone


attempting to win government contracts for computer-related projects had to
use COBOL. The American National Standards Institute first standardized
COBOL in 1968 and, in 1974, issued standards for another version known as
ANSI-COBOL. After more than seven controversial years of industry debate,
the standard known as COBOL 85 was approved, making COBOL a more
usable modern-day software tool. The principal benefit of standardization is
that COBOL is relatively machine independent- that is, a program written for
one type of computer can be run with only slight modifications on another
type for which a COBOL compiler has been developed.

COBOL is very good for processing large files and performing relatively simple
business calculations, such as payroll or interest. A noteworthy feature of
COBOL is that it is English-like-far more so than FORTRAN or BASIC. The
variable names are set up in such a way that, even if you know nothing about
programming, you can still understand what the program does. For example:

7 IF SALES-AMOUNT IS GREATER THAN SALES-QUOTA


8 COMPUTE COMMISSION = MAX-RATE * SALES-AMOUNT
9 ELSE
10 COMPUTE COMMISSION = MIN-RATE * SALES-AMOUNT.

11
Once you understand programming principles, it is not too difficult to add
COBOL to your repertoire. COBOL can be used for just about any task related
to business programming; indeed, it is especially suited to processing
alphanumeric data such as street addresses, purchased items, and dollar
amounts-the data of business. However, the feature that makes COBOL so
useful-its English-like appearance and easy readability-is also a weakness
because a COBOL program can be incredibly verbose. A programmer seldom
knocks out a quick COBOL program. In fact, there is hardly such a thing as a
quick COBOL program; there are just too many program lines to write, even
to accomplish a simple task. For speed and simplicity, BASIC, FORTRAN, and
Pascal are probably better bets.

As you can see in Figure 6, a COBOL program is divided into four parts called
divisions. The identification division identifies the program by name and often
contains helpful comments as well. The environment division describes the
computer on which the program will be compiled and executed. It also relates
each file of the program to the specific physical device, such as the tape
drive or printer, that will read or write the file. The data division contains
details about the data processed by the program, such as type of characters
(whether numeric or alphanumeric), number of characters, and placement of
decimal points. The procedure division contains the statements that give the
computer specific instructions to carry out the logic of the program.

It has been fashionable for some time to criticize COBOL: It is old-fashioned,


cumbersome, and inelegant. In fact, some companies, devoted to fast,
nimble program development, are converting to the more trendy language C.
But COBOL, with more than 30 years of staying power, is still famous for its
clear code, which is easy to read and debug.

BASIC: For Beginners and Others

Figure 7: Example Basic Program to Average Numbers


12 BASIC-Beginners' All-purpose Symbolic Instruction Code-is a common
language that is easy to learn. Developed at Dartmouth College, BASIC was
introduced by John Kemeny and Thomas Kurtz in 1965 and was originally
intended for use by students in an academic environment. In the late 1960s it
became widely used in interactive time-sharing environments in universities
and colleges. The use of BASIC has extended to business and personal
computer systems.

The primary feature of BASIC is one that may be of interest to many readers
of this book: BASIC is easy to learn, even for a person who has never
programmed before. Thus, the language is used often to train students in the
classroom. BASIC is also used by non-programming people, such as
engineers, who find it useful in problem solving. For many years, BASIC was
looked down on by "real programmers," who complained that it had too many
limitations and was not suitable for complex tasks. Newer versions, such as
Microsoft's QuickBASIC, include substantial improvements. An example of a
BASIC program and its output are shown in Figure 7.

Pascal: The Language of Simplicity


Named for Blaise Pascal, the seventeenth-century French mathematician,
Pascal was developed as a teaching language by a Swiss computer scientist,
Niklaus Wirth, and first became available in 1971. Since that time it has
become quite popular, first in Europe and now in the United States,
particularly in universities and colleges offering computer science programs.

The foremost feature of Pascal is that it is simpler than other languages -it
has fewer features and is less wordy than most. In addition to the popularity
of Pascal in college computer science departments, the language has also
made large inroads in the personal computer market as a simple yet
sophisticated alternative to BASIC. Over the years new versions have
improved on the original capabilities of Pascal. Today, Borland's Turbo Pascal
leads the Pascal world because its designers eliminated most of the
drawbacks of the original Pascal. Turbo Pascal is used by the business
community and is often the choice of nonprofessional programmers who need
to write their own programs.

Ada: Named for the Countess


Is any software worth over $25 billion? Not any more, according to Defense
Department experts. In 1974 the U.S. Department of Defense had spent that
amount on all kinds of software for a hodgepodge of languages for its needs.
The answer to this problem turned out to be a new language called Ada-
named for Countess Ada Lovelace, "the first programmer" (see Appendix B).
Sponsored by the Pentagon, Ada was originally intended to be a standard
language for weapons systems, but it has also been used successfully for
commercial applications. Introduced in 1980, Ada has the support not only of
the defense establishment but also of such industry heavyweights as IBM and
Intel, and Ada is even available for some personal computers. Although some
experts have said Ada is too complex, others say that it is easy to learn and
that it will increase productivity. Indeed, some experts believe that it is by far
a superior commercial language to such standbys as COBOL and FORTRAN.
Widespread use of Ada is considered unlikely by many experts. Although
there are many reasons for this (the military services, for instance, have
different levels of enthusiasm for it), probably its size- which may hinder its
use on personal computers-and complexity are the greatest barriers.
Although the Department of Defense is a market in itself, Ada has not caught
on to the extent that Pascal and C have, especially in the business
community.

C, C++, Java, and Javascript


A language invented by Dennis Ritchie at Bell Labs in 1972, C produces code
that approaches assembly language in efficiency while still offering high-level
language features. C was originally designed to write systems software but is
now considered a general-purpose language. C contains some of the best
features from other languages, including Pascal. C compilers are simple and
compact. A key attraction is that it is independent of the architecture of any
particular machine, a fact that contributes to the portability of C programs.
That is, a C program can be run on more than one type of computer after it
has been compiled for that machine.

Although C is simple and elegant, it is not simple to learn. It was developed


for gifted programmers, and the learning curve may be steep.
Straightforward tasks may be solved easily in C, but complex problems
require mastery of the language.

An interesting side note is that the availability of C on personal computers


has greatly enhanced the value of personal computers for budding software
entrepreneurs. A cottage software industry can use the same basic tool-the
language C-used by established software companies such as Microsoft and
Borland. Today C is has been replaced by its enhanced cousin, C++. C++ in
turn is being challenged by web-aware languages like Java and Javascript,
that look and act a lot like C++, but add features to support working with
networked computers, among other things.

The Programming Process


All programming involves creating something that solves a problem. The problems can range
from something of great scientific or national importance, through to something as trivial as
relieving personal boredom!

This section describes one approach to solving such problems - think of it as a rough guide to the
things you should do when entering the land of programming.

In broad terms, those things are:

1. Identify the Problem

2. Design a Solution
3. Write the Program

4. Check the Solution

Of these, only the third step is usually called "programming", but as you'll see later, it's probably
the least important stage of the process.

Identify the Problem


In fact, this stage should really be called identifying the solution because what you're really
trying to do is to tie down exactly what it is that you're trying achieve.

There are two stages to identifying a solution:

Requirements

Specification

Requirements

The first step is to examine the problem carefully to try to identify what qualifies as a solution. A
single problem may have many different solutions, but they will all have something in common.
So here you're trying to work out exactly what your program will be required to do.

For example, if we were asked to write a calculator program, we could choose many different
ways for the user to enter calculations - from entering equations, pressing buttons or even
writing them on the screen - but if the software can't add up correctly then it won't have
solved the problem. Therefore our first few requirements must be that:

the user can enter sums (we don't care how they do this)

and that the program will then evaluate those sums correctly
and display the result for the user.

We also have to decide what sort of sums our calculator will be required to evaluate. Again,
we have a fair amount of choice - we could be ambitious and ask it to solve simultaneous
equations or complex expressions, however since this is our first program we should probably
make the requirements more simple. So the third requirement is that:

The calculator must be able to evaluate sums made up of two whole


numbers (integer operands) and one addition (+), subtraction (-),
multiplication (*) or division (/) sign (operator).
Note that computer scientists traditionally use * instead of x and / instead of to indicate multiplication and
division respectively.

Thus our calculator must be able to deal with sums like 1 + 1, 10 - 6, 43 * 5 and 42 / 7.
However it won't have to handle 67.345 + 6, the cube root of PI or 15 . 2

Specification

The second step is to then look at the list of requirements and to decide exactly what your
solution should do to fulfil them. As we mentioned above, there are usually many different
solutions to a single problem; here, your aim is to decide on which of those solutions you want.
Therefore, you're trying to specify, in a fairly accurate manner, just what it is your final program
will do.

For example, for the calculator, we've already decided that the program must allow us to enter
simple sums and then must evaluate them correctly and display an answer. We must now tie
down exactly what this means.

Therefore, we have to decide which method of entering sums to use. We could specify any
one of a number of methods, but for now, we'll choose a simple method. We should also
specify what other behaviour we're expecting the program to have:

When the program runs it will display a welcome message, followed by


some simple instructions.

The program will then display a prompt sign ( [number]>) and the user can
then type the first number of their sum at the keyboard followed by the
RETURN (<-') key.

The program will display a second prompt sign ( [+-/*]>) and the user can
then enter the operator that they wish to use, followed by RETURN.

A third prompt sign will be displayed ([number]>) and the user will then
enter the second number, again followed by RETURN.

The calculator program will then display the mathematically correct


answer to the sum on the screen and end.

By the time you have worked out your specification, you should have a very clear idea of what
your final program will do: your goal.
Design a Solution
Once you've identified the things required to solve your problem, and specified what form your
solution will take, the next step is to work out just how you're going to turn that specification into
a working program. This is usually the hardest task!

As mentioned before, a program is simply a list of steps describing to the computer what it
should do. A design is simply a higher-level description of those steps. In effect it's a program
written as if the computer was a person. So, it doesn't have to completely spell out every step -
because humans know how to do a lot of things already and have a lot of common sense,
meaning that they can work the simple steps out for themselves. It also doesn't have to be written
in any special programming language - English will do (although people often use special
notations like pseudocode or flow charts for the more complicated sections).

Another way of looking at is that a programmer should be able to take a design and write the
program from it without having to think too hard. It's a bit like an architect's drawing: it contains
all the important structures without showing every bit of brick and mortar.

Working out a design to fulfil a particular specification can be difficult for several reasons:

1. You may need to learn a bit more about the capabilities of your computer and
your chosen programming language/environment to see what things it makes
easy or difficult.

2. You may also need to learn some extra information about the problem or find
a technique to solve it before you can work out how to build the program.

3. Finally, you may be able to think of several ways to build the program, but
they will all have different strengths and weaknesses and so some choices
will have to be made.

We'll return to these problems a bit later on in the course.

For our calculator, we have a fairly comprehensive specification and since it is a fairly simple
program we can turn the that quite easily into a design:

1. BEGIN

2. PRINT welcome message

3. PRINT instructions

4. PRINT [number]>
5. READ first_number

6. PRINT [+-/*]>

7. READ the_operator

8. PRINT [number]>

9. READ second_number

10. calculate result of using the_operator on


the first_number and the second_number

11. PRINT result

12.END

Here we assume that PRINT means 'put something on the screen' and READ means 'get something typed on the
keyboard' - both fairly standard programming operations.

Notice how step ten is actually hiding quite a complicated procedure. Although we (as
humans) could work out which operator was which and do the appropriate arithmetic, the
computer itself will need to be told exactly how to do this - but we'll leave that until the
programming stage.

Notice also how the design includes all of the important steps needed to fulfil our
specification - but that doesn't go into too much unnecessary detail. This is called abstraction.

When your design is completed you should have a very clear idea of how the computer is going
to fulfil your specification, which in turn meets your requirements, which in turn should solve
your original problem.

Program
Programming is then the task of describing your design to the computer: teaching it your way of
solving the problem.

There are usually three stages to writing a program:

1. Coding

2. Compiling
3. Debugging

Coding

Coding is the act of translating the design into an actual program, written in some form of
programming language. This is the step where you actually have to sit down at the computer and
type!

Coding is a little bit like writing an essay (but don't let that put you off). In most cases you write
your program using something a bit like a word processor. And, like essays, there are certain
things that you always need to to include in your program (a bit like titles, contents pages,
introductions, references etc.). But we'll come on to them later.

When you've finished translating your design into a program (usually filling in lots of details in
the process) you need to submit it to the computer to see what it makes of it.

As an example, we shall develop and present the code for the calculator later on.

Compiling

Compilation is actually the process of turning the program written in some programming
language into the instructions made up of 0's and 1's that the computer can actually follow. This
is necessary because the chip that makes your computer work only understands binary machine
code - something that most humans would have a great deal of trouble using since it looks
something like:

01110110
01101101
10101111
00110000
00010101
Early programmers actually used to write their programs in that sort of a style - but luckily they
soon learnt how to create programs that could take something written in a more understandable
language and translate it into this gobbledy gook. These programs are called compilers and you
can think of them simply as translators that can read a programming language, translate it and
write out the corresponding machine code.

Compilers are notoriously pedantic though - if you don't write very correct programs, they will
complain. Think of them as the strictest sort of English teacher, who picks you up on every
single missing comma, misplaced apostrophe and grammatical error.
Debugging

This is where debugging makes it first appearance, since once the compiler has looked at your
program it is likely to come back to you with a list of mistakes as long as your arm. Don't worry
though, as this is perfectly normal - even the most experienced programmers make blunders.

Debugging is simply the task of looking at the original program, identifying the mistakes,
correcting the code and recompiling it. This cycle of code -> compile -> debug will often be
repeated many many times before the compiler is happy with it. Luckily, the compiler never ever
gets cross during this process - the programmer on the other hand...

It should also be said at this point that it isn't actually necessary to write the entire program
before you start to compile and debug it. In most cases it is better to write a small section of the
code first, get that to work, and then move on to the next stage. This reduces the amount of code
that needs to be debugged each time and generally creates a good feeling of "getting there" as
each section is completed.

Finally though, the compiler will present you with a program that the computer can run:
hopefully, your solution!

Solution!
The final step in the grand programming process is that of testing your creation to check that it
does what you wanted it to do. This step is unfortunately necessary because although the
compiler has checked that your program is correctly written, it can't check whether what you've
written actually solves your original problem.

This is because it is quite possible to write a sentence in any language that is perfectly formed
with regards to the language that it's written in (syntacticly correct) but at the same time be utter
nonsense (semantically incorrect). For example, 'Fish trousers go sideways.' is a great sentence -
it's got a capital letter and a full stop - but it doesn't mean a lot. Similarly, 'Put the ice cube tray in
the oven.' has verbs and nouns and so on - but it's pretty useless if you wanted to make ice cubes.

So your program needs to be tested, and this is often initially done informally (or perhaps,
haphazardly) by running it and playing with it for a bit to see if it seems to be working correctly.
After this has been done, it should also be checked more thoroughly by subjecting it to carefully
worked out set of tests that put it through its paces, and check that it meets the requirements and
specification - but we shall discuss this more later on in the course.

Where mistakes are identified, it is a case of donning a Sherlock Holmes hat and trying to figure
out where in the code the mistake is. Once identified, the problem should be fixed by changing
the code and recompiling. Care should be taken at this point that this fix doesn't break something
else, so careful retesting is important. This process is also known as debugging.

Once all the testing and debugging has been completed, you should be pretty certain that your
program works according to your requirements and your specification and so you should finally
have a solution to your problem!

Easy isn't it?!2.

Summary

1. Identify the Problem - What Are You Trying To Do?

o Requirements

o Specification

2. Design a Solution - How Is It Going To Be Done?

3. Write the Program - Teaching the Computer

o Code

o Compile

o Debug

4. Check the Solution - Testing it Understands You

While this may sound like a great deal of effort to go to to build a simple program, don't worry,
as after a while it will become second nature, and for small programs, most of the stages can be
done in your head.

The next section will introduce you to your first programming language, and your very first
program!

Vous aimerez peut-être aussi