A computer program is a
collection of instructions, or statements (also called code)
carried out by the computer's CPU. These instructions can be written in
many different languages. Except for operating systems and utilities,
all the categories of programs listed in the preceding section are
application programs. These programs are developed to help users solve
problems or perform tasks, such as sending mail messages, editing text,
or finding a specific article in a library.
Application programs are
usually composed of many files. Some of these files contain instructions
for the computer, whereas other files contain data. On DOS- or
Windows-based PCs, some common extensions for program files are .exe
(executable), .dll (dynamic link library.), .ini (initialization), and .hlp
(help). These extensions define the type of file.
By default, most pro-gram
files are stored in the folder that bears the application's name or an
abbreviation of it. To view a list of the files needed to run an
application, you can open that application's directory or folder.
The system software that
controls your computer also includes many files.
Program Control Flow
Although all the files in the
program's folder are considered parts of the program, usually one file
represents the core. This file is often called the executable file
because it is the one that the computer executes when you launch the
program. On PCs running DOS or Windows, the executable file has the same
name as the program (or an abbreviation of it), plus the extension .exe.
When you launch a program, the
computer begins reading and carrying out statements at the executable's
main entry point. Typically, this entry point is the first line (or
statement) in the file, although it may be elsewhere. After execution of
the first statement, program control passes, or flows, to another
statement, and so on, until the last statement of the program has been
executed. Then the program ends. The order in which program statements
are executed is called program control flow.
The example in the below picture
shows the flow of a small program that controls a furnace. The program
constantly checks the thermostat setting and current temperature. If the
current temperature is at or above the thermostat setting, the program
executes the statements that turn off the furnace. If the current
temperature is below the thermostat setting, the program executes the
statements that turn on the furnace.
One element that most computer
languages have in common is variables--placeholders for data being
processed. For example, imagine you are writing a program that prompts
users to enter their ages. You need a placeholder, or variable, to
represent the data (different ages) they enter. In this case, you might
choose to name the variable Age. Then, when a user enters a number at
the prompt, this data becomes the value of the variable Age.
Just as in algebra, you can
use variables in programs to perform actions on data. For example,
consider the instruction:
Age + 2
If the value of Age is 20, the
result of this instruction is 22; if the value is 30, the result is 32;
and so on.
Algorithms and Functions
Algorithms are the series of
steps by which problems are solved. Algorithms have been worked out for
solving a wide range of mathematical problems, such as adding numbers or
finding a square root.
solutions to problems. The steps to the solution (instructions) remain
the same, whether you are working out the solution by computer or by
Functions are the
expression of algorithms in a specific computer language. You reuse
functions when you need them. You do not have to rewrite the lines of
code represented by the function each time. For example:
is a way of referring to the
square root's function. The (x) after the name of the function is the
argument. You use arguments to pass input to functions as the program
runs. In this example, x is a variable representing a number. If x is
equal to 12, then the function will find the square root of 12. After
the function finds the square root, it returns this value to the
program. You can then use the value in other calculations.
languages use different names, such as subroutines, procedures, and
routines, for these blocks of reusable code. There are subtle
differences between these terms, but for now, the term "function"
will be used for all of them.
TW0 APPROACHES TO
Until the 1960s, relatively
little structure was imposed on how programmers wrote code. As a result,
following control flow through hundreds or thousands of lines of code
was almost impossible. Programmers, for example, often used goto
statements to jump to other parts of a program. A goto statement
does exactly what the name implies. It identifies a different line of
the program to which control jumps. (It goes to.) The issue with goto
statements is identifying how program control flow proceeds after the
jump. Does control return to the jumping-off place, or does it continue
at the new location?
Structured programming evolved
in the 1960s and 1970s. The name refers to the practice of building
programs using a set of well-defined structures. One goal of structured
programming is the elimination of goto statements.
Software developers have found
that using structured programming results in improved efficiency, but
they continue to struggle with the process of building software quickly
Reuse is recognized as the key
to the solution. Reusing code allows programs to be built quickly and
correctly. Functions, which are the building blocks of structured
programming, are one step along this path. In the 1980s, computing took
another leap forward with the development of object-oriented programming
(OOP). The building blocks of OOP, called objects, are reusable, modular
components. Experts claim that OOP will be the dominant programming
approach through at least the end of the 1990s.
OOP builds on and enhances
structured programming. You do not leave structured programming behind
when you work with an object-oriented language. Objects, for example,
are composed of structured program pieces, and the logic of manipulating
objects is also structured.
Researchers in the 1960s
demonstrated that programs could be written with three control
If-Then and If-Else Flowchart
defines the default control flow in a program. Typically, this structure
is built into programming languages. As a result, unless directed
otherwise, a computer executes lines of code in the order in which they
are written. The picture above shows a flowchart of this sequential flow. The
commands in the rectangles represent two sequential lines of code.
Program control flows from the previous line of code to the next line.
The commands are written in pseudo-code, which is an informal language
programmers use as they are working through the logic of a program.
After the command sequence is developed, the programmers translate the
pseudo-code into a specific computer language.
are built around a
condition statement. If the condition statement is true, certain
lines of code are executed. If the condition statement is false, those
lines of code are not executed. The two most common selection structures
are: If-Then and If-Else (sometimes called If-Then-Else). The picture to
the right shows a flowchart of the if-then-else structure.
Repetition (or looping) structures are also built around condition statements. If
the condition is true, then a block of one or more commands is repeated
until the condition is false. The computer first tests the condition
and, if it is true, executes the command block once. It then tests the
condition again. If it is still true, the command block is repeated.
Because of this cycling, repetition structures are also called loops.
Three common looping structures are: For-Next, While, and Do-While.
In 1981 when the original IBM Personal Computer was announced,
IBM released three operating systems for it. How many of you remember that?
Since I wrote the first IBM course on how to fix this original PC, I had to know
at least a little about all three of them.
IBM decided early in the development process of the PC that they
did not want to hire a bunch of programmers to write software for it -
especially an operating system. IBM wanted the hardware business and did not
care about the software. Since there was no clear-cut contender for an operating
system at the time, IBM approached three organizations about writing one for the