Download CHAPTER 9 CHAPTER 9

Transcript
CUT3e_chap12_3P.qxd
2/2/07
4:32 PM
Page 544
TechVisionary
THE FIRST PROGRAMMER
Lady Lovelace
DAUGHTER OF THE POET LORD BYRON, Augusta
THE SUBROUTINE A subroutine is a part of a
Ada, countess of Lovelace (1815–1852), became
Charles Babbage’s most enthusiastic supporter.
She wrote programs, or sequences of instructions, for Babbage’s invention, the Analytical
Engine and made several innovations that are
central to programming today. These included
the subroutine, the loop, and the
conditional jump.
program that can be used repeatedly. An example of a subroutine is a sequence of instructions
that sorts numbers into ascending order.
THE LOOP A loop is a sequence of repeating
instructions. A loop works by means of a command that tells the computer to back up to a
previous command, as in the following
sequence, which loops endlessly, printing numbers from 0 to infinity by increments of 0.5:
10
20
30
40
LET N
PRINT
LET N
GO TO
= 0
N
= N + .5
20
THE CONDITIONAL JUMP A conditional jump is
an instruction that tells the computer to go to a
different instruction if a certain condition is
met. The conditional jump has many uses in
programming. For example, it can be used to
keep a loop from repeating endlessly, as in the
following set of instructions, which prints numbers from 0 to 6.5 by increments of 0.5:
10
20
30
40
50
LET N = 0
PRINT N
LET N = N + .5
IF N ≤ 6.5 GO TO 20
END
Source: Shepherd, Robert D. Introduction to Computers and
Technology, Paradigm Publishing: October 1997.
repetition is the dullest part of any mental activity. Humans naturally become
bored when required to repeat a series of actions over and over again. This is not a
problem for computers because they never get bored with performing even the
simplest and most repetitive chores. Unlike humans, computers excel at doing the
same thing again and again very quickly.
Decision Statements A program that simply follows a linear sequence of
actions without variation is of limited use. However, if a program consists of a
544
C H A P T E R 1 2 Programming Concepts and Languages