Program Logic
Program Logic
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
CHAPTER
An Overview of
1
Computers
and
Programming
In this chapter, you will learn about:
Computer systems
Simple program logic
The steps involved in the program development cycle
Pseudocode statements and flowchart symbols
Using a sentinel value to end a program
Programming and user environments
The evolution of programming models
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
In business, much of the data used is facts and figures about such entities
as products, customers, and personnel. However, data can also be items
such as the choices a player makes in a game or the notes required by a
music- playing program.
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
Random storage. Internal storage is volatile—its contents are lost wflen tfle
access computer is turned off or loses power. Usually, you want to be able to
memory, or
retrieve and perflaps modify tfle stored instructions later, so you
RAM, is a
also store tflem on a permanent storage device, sucfl as a disk.
form of inter-
nal, volatile memory. It Permanent storage devices are nonvolatile—tflat is, tfleir contents
is hardware on which are persistent and are retained even wflen power is lost.
4
the programs that are
cur- rently running and
After a computer program is stored in memory, it must be trans-
the data items that are lated from your programming language statements to machine
currently being used language tflat represents tfle millions of on/off circuits witflin tfle
are stored for quick computer. Eacfl programming language uses a piece of software,
access. called a compiler or an interpreter, to translate your program
code into macfline language. Macfline language is also called
binary language, and is represented as a series of 0s and 1s. Tfle
The program compiler or interpreter tflat translates your code tells you if any
statements programming language component flas been used incorrectly.
you write in Syntax errors are relatively easy to locate and correct because
a
tfle compiler or interpreter you use fligflligflts every syntax error.
programming
language
If you write a computer program using a language sucfl as C++
are but spell one of its words incorrectly or reverse tfle proper order
known as source code. of two words, tfle software lets you know tflat it found a mistake
The translated machine by displaying an error message as soon as you try to translate tfle
language statements are program.
known as object code.
Although there are differences in how compilers and interpreters work, their
basic function is the same—to translate your programming statements into
code the computer can use. When you use a compiler, an entire program is
translated before it can execute; when you use an interpreter, each instruc-
tion is translated just prior to execution. Usually, you do not choose which
type of translation to use—it depends on the programming language. However, there
are some languages for which both compilers and interpreters are available.
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
In each Two Truths and a Lie section, two of the numbered statements are
5
true, and one is false. Identify the false statement and explain why it is false.
1. Hardware is the equipment, or the devices, associated with a
computer. Software is computer instructions.
2. The grammar rules of a computer programming language are its
syntax.
3. You write programs using machine language, and translation software
con- verts the statements to a programming language.
1s.
preter) converts the statements to machine language, which is 0s and
inter-
such as Visual Basic or Java, and a translation program (called a compiler or
language
The false statement is #3. You write programs using a programming
After you learn Just as baking directions can be given correctly in Mandarin, Urdu,
French, you or Spanisfl, tfle same program logic can be expressed in any number
automatically
of programming languages. Because tflis book is not concerned witfl
know, or can
easily figure
any specific language, tfle programming examples could flave been
out, many Spanish written in Visual Basic, C++, or Java. For convenience, tflis book uses
words. Similarly, after instructions written in Englisfl!
6
you learn one
programming lan- guage,
Most simple computer programs include steps tflat perform input,
it is much easier to processing, and output. Suppose you want to write a computer
understand several other program to double any number you provide. You can write sucfl a
languages. program in a programming language sucfl as Visual Basic or Java,
but if you were to write it using Englisfl-like statements, it would
You will learn look like tflis:
about the
input myNumber
odd
set myAnswer = myNumber * 2
elimination of
output myAnswer
the space
between The number-doubling process
words like my and includes tflree instructions:
Number in Chapter 2.
• The instruction to input myNumber is an example of an input
operation. Wflen tfle computer interprets tflis instruction, it
knows to look to an input device to obtain a number. Wflen you
work in a specific programming language, you write instructions
tflat tell tfle computer wflicfl device to access for input. For exam-
ple, wflen a user enters a number as data for a program, tfle user
migflt click on tfle number witfl a mouse, type it from a keyboard,
or speak it into a micropflone. Logically, flowever, it doesn’t really
matter wflicfl flardware device is used, as long as tfle computer
knows to look for a number. Wflen tfle number is retrieved from
an input device, it is placed in tfle computer’s memory at tfle
location named myNumber. The location myNumber is a variable. A
variable is a named memory location wflose value can vary—for
example, tfle value of myNumber migflt be 3 wflen tfle program is
used for tfle first time and 45 wflen it is used tfle next time.
From a logical perspective, when you input a value, the hardware device is
irrelevant. The same is true in your daily life. If you follow the instruction “Get
eggs for the cake,” it does not really matter if you purchase them from a
store or harvest them from your own chickens—you get the eggs either way.
There might be different practical considerations to getting the eggs, just as
there are for getting data from a large database as opposed to an inexperienced user.
Programmers For now, this book is only concerned with the logic of the operation, not the minor
use an aster- details.
isk to
indicate • The instruction set myAnswer = myNumber * 2 is an example
multiplication. of a processing operation. Matflematical operations are not tfle
You will learn only kind of processing operations, but tfley are very typical. As
more about arithmetic witfl input operations, tfle type of flardware used for
statements in Chapter 2. processing
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
1. A program with syntax errors can execute but might produce incorrect
results.
2. Although the syntax of programming languages differs, the same
program logic can be expressed in different languages.
3. Most simple computer programs include steps that perform input,
process- ing, and output.
results.
program
a with no syntax errors can execute, but might produce incorrect
The false statement is #1. A program with syntax errors cannot execute;
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
Understanding the
Program Development
Cycle
A programmer’s job involves writing instructions (sucfl as tflose in
8 tfle doubling program in tfle preceding section), but a professional
programmer usually does not just sit down at a computer keyboard
and start typing. Figure 1-1 illustrates tfle program development
cycle1. , wflicfl can be tfle
Understand broken down into at least seven steps:
problem.
2. Plan tfle logic.
3. Code tfle program.
4. Use software (a compiler or interpreter) to translate tfle
program into macfline language.
5. Test tfle program.
6. Put tfle program into production.
7. Maintain tfle program.
Understand
the
problem
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
needs a Web site to provide buyers witfl an online sflopping cart in The term end
wflicfl to gatfler tfleir orders. Because programmers are providing a user distin-
guishes those
service to tflese users, programmers must first understand wflat tfle
who actually
users want. Altflougfl wflen a program runs, you usually tflink of tfle use and ben-
logic as a cycle of input-processing-output operations; wflen you efit from a software prod-
plan a program, you tflink of tfle output first. After you understand
9
wflat tfle desired result is, you can plan wflat to input and process to uct from others in an
acflieve it. organization who might
purchase, install, or have
Suppose tfle director of Human Resources says to a programmer, other contact with the
“Our department needs a list of all employees wflo flave been flere software.
over five years, because we want to invite tflem to a special tflank-you
dinner.” On tfle surface, tflis seems like a simple request. An experi-
enced programmer, flowever, will know tflat tfle request is incom-
plete. For example, you migflt not know tfle answers to tfle following
questions about wflicfl employees to include:
• Does tfle director want a list of full-time employees only, or a list
of full- and part-time employees togetfler?
• Does sfle want people wflo flave worked for tfle company on a
montfl-to-montfl contractual basis over tfle past five years, or only
regular, permanent employees?
• Do tfle listed employees need to flave worked for tfle organization
for five years as of today, as of tfle date of tfle dinner, or as of
some otfler cutoff date?
• Wflat about an employee wflo, for example, worked tflree years,
took a two-year leave of absence, and flas been back for tflree
years?
The programmer cannot make any of tflese decisions; tfle user (in tflis
case, tfle Human Resources director) must address tflese questions.
More decisions still migflt be required. For example:
• Wflat data sflould be included for eacfl listed employee? Sflould
tfle list contain botfl first and last names? Social Security numbers?
Pflone numbers? Addresses?
• Sflould tfle list be in alpflabetical order? Employee ID number
order? Lengtfl-of-service order? Some otfler order?
• Sflould tfle employees be grouped by any criteria, sucfl as
depart- ment number or years of service?
Several pieces of documentation are often provided to flelp tfle pro-
grammer understand tfle problem. Documentation consists of all tfle
supporting paperwork for a program; it migflt include items sucfl
as original requests for tfle program from users, sample output, and
descriptions of tfle data items available for input.
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
Watch the Really understanding tfle problem may be one of tfle most difficult
video The aspects of programming. On any job, tfle description of wflat tfle user
Program
needs may be vague—worse yet, users may not really know wflat
Development
Cycle, Part 1.
tfley want, and users wflo tflink tfley know frequently cflange tfleir
minds after seeing sample output. A good programmer is often part
coun- selor, part detective!
10
You may
hear
Planning the Logic
programmers The fleart of tfle programming process lies in planning tfle program’s
refer to
logic. During tflis pflase of tfle process, tfle programmer plans tfle
planning a
program as
steps of tfle program, deciding wflat steps to include and flow to order
“developing an algorithm.” tflem. You can plan tfle solution to a problem in many ways. The two
An algorithm is the most common planning tools are flowcflarts and pseudocode. Botfl
sequence of steps neces- tools involve writing tfle steps of tfle program in Englisfl, mucfl as you
sary to solve any would plan a trip on paper before getting into tfle car or plan a party
problem. tfleme before sflopping for food and favors.
You will
The programmer sflouldn’t worry about tfle syntax of any particu-
learn more
about lar language at tflis point, but sflould focus on figuring out wflat
flowcharts sequence of events will lead from tfle available input to tfle desired
and pseudo- output. Planning tfle logic includes tflinking carefully about all tfle
code later in possible data values a program migflt encounter and flow you want
this tfle program to flandle eacfl scenario. The process of walking tflrougfl
chapter. a program’s logic on paper before you actually write tfle program is
In addition to called desk-checking. You will learn more about planning tfle logic
flowcharts tflrougflout tflis book; in fact, tfle book focuses on tflis crucial step
and
almost exclusively.
pseudocode,
programmers
use a variety
of other tools to help in Coding the Program
program development.
After tfle logic is developed, only tflen can tfle programmer write
One such tool is an
IPO chart, which tfle program. Hundreds of programming languages are available.
delineates input, Programmers cfloose particular languages because some flave built-in
processing, and output capabilities tflat make tflem more efficient tflan otflers at flandling
tasks. Some object- certain types of operations. Despite tfleir differences, programming
oriented program- mers languages are quite alike in tfleir basic capabilities—eacfl can flandle
also use TOE charts,
input operations, aritflmetic processing, output operations, and otfler
which list tasks,
standard functions. The logic developed to solve a programming
objects, and events.
problem can be executed using any number of languages. Only after
cfloosing a language must tfle programmer be concerned witfl proper
punctuation and tfle correct spelling of commands—in otfler words,
using tfle correct syntax.
Some very experienced programmers can successfully combine
logic planning and program coding in one step. This may work for
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
planning and writing a very simple program, just as you can plan
and write a postcard to a friend using one step. A good term paper
or a Hollywood screenplay, flowever, needs planning before writing
—and so do most programs.
Wflicfl step is flarder: planning tfle logic or coding tfle program?
Rigflt now, it may seem to you tflat writing in a programming lan- 11
guage is a very difficult task, considering all tfle spelling and syntax
rules you must learn. However, tfle planning step is actually more
difficult. Wflicfl is more difficult: tflinking up tfle twists and turns
to tfle plot of a best-selling mystery novel, or writing a translation
of an existing novel from Englisfl to Spanisfl? And wflo do you
tflink gets paid more, tfle writer wflo creates tfle plot or tfle
translator? (Try asking friends to name any famous translator!)
tfle cat,” tfle compiler at first migflt point out only one syntax error.
The second word, “dg,” is illegal because it is not part of tfle Englisfl
After a pro- language. Only after you corrected tfle word to “dog” would tfle
gram has com- piler find anotfler syntax error on tfle tflird word, “cflase,”
been trans- because it is tfle wrong verb form for tfle subject “dog.” This doesn’t
lated into mean “cflase” is necessarily tfle wrong word. Maybe “dog” is wrong;
12 machine lan-
perflaps tfle subject sflould be “dogs,” in wflicfl case “cflase” is rigflt.
guage, the machine lan-
guage program is saved
Compilers don’t always know exactly wflat you mean, nor do tfley
and can be run any know wflat tfle proper correction sflould be, but tfley do know wflen
num- ber of times sometfling is wrong witfl your syntax.
without repeating the
translation step. You
Wflen writing a program, a programmer migflt need to recompile tfle
only need to retranslate code several times. An executable program is created only wflen tfle
your code if you make code is free of syntax errors. Wflen you run an executable program, it
changes to your source typically also migflt require input data. Figure 1-2 sflows a diagram of
code statements. tflis entire process.
If there are
Write and correct no Compile the syntax Executable
the program program
errors
code
If there are program
syntax errors
List of Program
syntax output
error
messages
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
If you execute tfle program, provide tfle value 2 as input to tfle pro-
gram, and tfle answer 4 is displayed, you flave executed one
successful test run of tfle program.
However, if tfle answer 40 is displayed, maybe tfle program
contains a logical error. Maybe tfle second line of code was
mistyped witfl an extra zero, so tflat tfle program reads:
input myNumber Don’t Do It
set myAnswer = myNumber * 20 The programmer
output myAnswer typed
"20" instead of
"2". The process
Placing 20 instead of 2 in tfle
of finding
multiplication statement caused a logical error. Notice tflat notfling is and
syntactically wrong witfl tflis second program—it is just as correcting
reasonable to multiply a number by 20 as by 2—but if tfle program
programmer intends only to double myNumber, tflen a logical error errors is
flas occurred. called debugging.
input myNumber
set myAnswer = myNumber + 2
output myAnswer
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
from tfle five-year list. Many companies do not know tflat tfleir
software flas a problem until an unusual circumstance occurs—for
Chapter 4
contains more example, tfle first time an employee flas more tflan nine depen-
information dents, tfle first time a customer orders more tflan 999 items at a
on testing time, or wflen (as well-documented in tfle popular press) a new
programs. century begins.
14
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
1. Understanding the problem that must be solved can be one of the most
15
dif- ficult aspects of programming.
2. The two most commonly used logic-planning tools are flowcharts
and pseudocode.
3. Flowcharting a program is a very different process if you use an
older programming language instead of a newer one.
Writing Pseudocode
You flave already seen examples of statements tflat represent pseudo-
code earlier in tflis cflapter, and tflere is notfling mysterious about
tflem. The following five statements constitute a pseudocode repre-
sentation of a number-doubling problem:
start
input myNumber
set myAnswer = myNumber * 2
output myAnswer
stop
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
Using pseudocode involves writing down all tfle steps you will use in
a program. Usually, programmers preface tfleir pseudocode witfl a
beginning statement like start and end it witfl a terminating state-
ment like stop. The statements between start and stop look like
Englisfl and are indented sligfltly so tflat start and stop stand out.
Most programmers do not botfler witfl punctuation sucfl as periods
16
at tfle end of pseudocode statements, altflougfl it would not be wrong
to use tflem if you prefer tflat style. Similarly, tflere is no need to
capi- talize tfle first word in a sentence, altflougfl you migflt cfloose
to do so. This book follows tfle conventions of using lowercase
letters for verbs tflat begin pseudocode statements and omitting
periods at tfle end of statements.
Pseudocode is fairly flexible because it is a planning tool, and not
tfle final product. Therefore, for example, you migflt prefer any of
tfle following:
• Instead of start and stop, some pseudocode developers
would use tfle terms begin and end.
• Instead of writing input myNumber, some developers would
write
get myNumber or read myNumber.
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
Drawing Flowcharts
Some professional programmers prefer writing pseudocode to
drawing flowcflarts, because using pseudocode is more similar to
writing tfle final statements in tfle programming language. Otflers
prefer drawing flowcflarts to represent tfle logical flow, because flow-
cflarts allow programmers to visualize more easily flow tfle program You can draw 17
statements will connect. Especially for beginning programmers, flow- a flowchart
by hand or
cflarts are an excellent tool to flelp tflem visualize flow tfle
use software,
statements in a program are interrelated. such as
Wflen you create a flowcflart, you draw geometric sflapes tflat contain Microsoft Word and
Microsoft PowerPoint,
tfle individual statements and tflat are connected witfl arrows. You
that contains
use a parallelogram to represent
flowcharting tools. You
an input symbol, wflicfl input myNumber can use sev- eral other
indicates an input operation. software pro- grams,
You write an such as Visio and Visual
input statement in Englisfl inside tfle Figure 1-3 Input Logic, specifically to
parallelogram, as sflown in Figure 1-3. symbol create flowcharts.
Some software programs that use flowcharts (such as Visual Logic) use
a left-slanting parallelogram to represent output. As long as the flowchart
creator and the flowchart reader are communicating, the actual shape
used is irrelevant. This book will follow the most standard convention
of always using the right-slanting parallelogram for both input and output.
Appendix B
To sflow tfle correct sequence of tflese statements, you use arrows, or
contains a
flowlines, to connect tfle steps. Wflenever possible, most of a flow- summary of
cflart sflould read from top to bottom or from left to rigflt on a page. all the flow-
That’s tfle way we read Englisfl, so wflen flowcflarts follow tflis con- chart symbols
vention, tfley are easier for us to understand. you will see in this book.
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
Flowchart Pseudocode
start
input myNumber
start
input myNumber
set myAnswer =
set myAnswer = myNumber * 2
myNumber * 2
output myAnswer
stop
output myAnswer
stop
Repeating Instructions
After tfle flowcflart or pseudocode flas been developed, tfle
programmer only needs to: (1) buy a computer, (2) buy a language
compiler, (3) learn a programming language, (4) code tfle pro-
gram, (5) attempt to compile it, (6) fix tfle syntax errors, (7) com-
pile it again, (8) test it witfl several sets of data, and (9) put it into
production.
“Wfloa!” you are probably saying to yourself. “This is simply not wortfl
it! All tflat work to create a flowcflart or pseudocode, and then all
tflose otfler steps? For five dollars, I can buy a pocket calculator tflat
will
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
double any number for me instantly!” You are absolutely rigflt. If tflis When you tell
were a real computer program, and all it did was double tfle value of a a friend how
to get to your
number, it would not be wortfl tfle effort. Writing a computer
house, you
program would be wortflwflile only if you flad many—let’s say 10,000 might write a
—numbers to double in a limited amount of time—let’s say tfle next series of instructions or
two minutes. you might draw a
19
map.
Unfortunately, tfle number-doubling program represented in Pseudocode is similar to
Figure 1-6 does not double 10,000 numbers; it doubles only one. You
could execute tfle program 10,000 times, of course, but tflat would written, step-by-step
require you to sit at tfle computer and tell it to run tfle program over instructions; a flowchart,
and over again. You would be better off witfl a program tflat could like a map, is a visual
process 10,000 numbers, one after tfle otfler. representation of the
same thing.
One solution is to write tfle program sflown in Figure 1-7 and execute
tfle same steps 10,000 times. Of course, writing tflis program would
be very time consuming; you migflt as well buy tfle calculator.
start
input myNumber
set myAnswer = myNumber * 2
output myAnswer
input myNumber
set myAnswer = myNumber * 2
output myAnswer Don’t Do It
input myNumber You would never want
set myAnswer = myNumber * 2 to write such a
output myAnswer repetitious list of
…and so on for 9,997 more instructions.
times
Figure 1-7 Inefficient pseudocode for program that doubles 10,000 numbers
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
start
input myNumber
20 Don’t Do It
set myAnswer = This logic saves
myNumber * 2 steps, but it has a
fatal flaw – it
never ends.
output myAnswer
represent
to a processing operation.
The false statement is #2. When you draw a flowchart, you use a rectangle
progress any furtfler wflile it is waiting for input; meanwflile, tfle pro-
gram is occupying computer memory and tying up operating system
resources. Refusing to enter any more numbers is not a practical
solu- tion. Anotfler way to end tfle program is simply to turn off tfle
com- puter. But again, tflat’s neitfler tfle best way nor an elegant
solution.
21
A superior way to end tfle program is to set a predetermined value
for myNumber tflat means “Stop tfle program!” For example, tfle
program- mer and tfle user could agree tflat tfle user will never need
to know tfle double of 0, so tfle user could enter a 0 to stop. The
program could tflen test any incoming value contained in myNumber
and, if it is a 0, stop tfle program. Testing a value is also called
making a decision.
myNumber Yes
stop
= 0?
No
set myAnswer =
myNumber times 2
output myAnswer
Yes
eof? stop
No
set myAnswer =
myNumber times 2
output myAnswer
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
for term
a file sentinel.
eof (for “end of file”) is the common term The false statement is #3. The
Understanding Programming
and User Environments
Many approacfles can be used to write and execute a computer pro-
gram. Wflen you plan a program’s logic, you can use a flowcflart or
pseudocode, or a combination of tfle two. Wflen you code tfle pro-
gram, you can type statements into a variety of text editors. Wflen
your program executes, it migflt accept input from a keyboard,
mouse, micropflone, or any otfler input device, and wflen you provide
a program’s output, you migflt use text, images, or sound. This
section describes tfle most common environments you will
encounter as a new programmer.
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
A text editor is a program tflat you use to create simple text files. It
is similar to a word processor, but witflout as many features. You can
use a text editor sucfl as Notepad tflat is included witfl Microsoft
Windows. Figure 1-11 sflows a C# program in Notepad tflat accepts
a number and doubles it. An advantage to using a simple text editor
to type and save a program is tflat tfle completed program does not
24
require mucfl disk space for storage. For example, tfle file sflown in
Figure 1-11 occupies only 314 bytes of storage.
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
1. You can type a program into an editor that is part of an integrated develop-
ment environment, but using a plain text editor provides you with more
pro- gramming help.
2. When a program runs from the command line, a user types text to provide
input.
3. Although GUI and command-line environments look different, the logic
processes of input, processing, and output apply to both program
editor.
types. programming
more help than a plain text
The false statement is #1. An integrated development environment provides
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
Understanding the
Evolution of Programming
Models
People flave been writing modern computer programs since tfle
1940s. The oldest programming languages required programmers to 27
work witfl memory addresses and to memorize awkward codes asso-
ciated witfl macfline languages. Newer programming languages look
mucfl more like natural language and are easier to use, partly Ada Byron
Lovelace pre-
because tfley allow programmers to name variables instead of using
dicted the
awkward memory addresses. Also, newer programming languages
development
allow pro- grammers to create self-contained modules or program of software
segments tflat can be pieced togetfler in a variety of ways. The oldest in
computer programs were written in one piece, from start to finisfl, 1843; she is often
but modern programs are rarely written tflat way—tfley are created regarded as the first pro-
by teams of programmers, eacfl developing reusable and grammer. The basis for
most modern software
connectable program procedures. Writing several small modules is
was proposed by Alan
easier tflan writing one large program, and most large tasks are Turing in 1935.
easier wflen you break tfle work into units and get otfler workers to
flelp witfl some of tfle units.
You will learn
Currently, two major models or paradigms are used by programmers to create pro-
to develop programs and tfleir procedures. One tecflnique, proce- gram modules
in Chapter 2.
dural programming, focuses on tfle procedures tflat programmers
create. That is, procedural programmers focus on tfle actions tflat
are carried out—for example, getting input data for an employee and
writing tfle calculations needed to produce a paycfleck from tfle data.
Procedural programmers would approacfl tfle job of producing a
pay- cfleck by breaking down tfle process into manageable subtasks.
The otfler popular programming model, object-oriented You can
programming, focuses on objects, or “tflings,” and describes tfleir write a
features (or attributes) and tfleir beflaviors. For example, object- procedural
program in
oriented programmers migflt design a payroll application by tflink-
any language
ing about employees and paycflecks, and describing tfleir attributes that
(e.g. employees flave names and Social Security numbers, and pay- supports
cflecks flave names and cfleck amounts). Then tfle programmers object orientation. The
would tflink about tfle beflaviors of employees and paycflecks, sucfl as opposite is not
employees getting raises and adding dependents and paycflecks always true.
being calculated and output. Object-oriented programmers would Object-
tflen build applications from tflese entities. oriented
programming
Witfl eitfler approacfl, procedural or object oriented, you can produce a employs a
correct paycfleck, and botfl models employ reusable program modules. large
The major difference lies in tfle focus tfle programmer takes during tfle vocabu-
earliest planning stages of a project. For now, tflis book focuses on pro- lary; you can learn this
cedural programming tecflniques. The skills you gain in programming terminology in Chapter
10 of the comprehensive
procedurally—declaring variables, accepting input, making decisions,
version of this book.
Copyright 2011 producing
Cengage Learning. Alloutput, and
Rights Reserved. sobeon—will
May not serve
copied, scanned, or duplicated,you well
in whole wfletfler
or in part. you
Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
Chapter Summary
• Togetfler, computer flardware (pflysical devices) and software
(instructions) accomplisfl tflree major operations: input, processing,
and output. You write computer instructions in a computer pro-
gramming language tflat requires specific syntax; tfle instructions
are translated into macfline language by a compiler or interpreter.
Wflen botfl tfle syntax and logic of a program are correct, you can
run, or execute, tfle program to produce tfle desired results.
• For a program to work properly, you must develop correct logic.
Logical errors are mucfl more difficult to locate tflan syntax
errors.
• A programmer’s job involves understanding tfle problem, plan-
ning tfle logic, coding tfle program, translating tfle program into
macfline language, testing tfle program, putting tfle program into
production, and maintaining it.
• Wflen programmers plan tfle logic for a solution to a program-
ming problem, tfley often use flowcflarts or pseudocode. Wflen
you draw a flowcflart, you use parallelograms to represent input
and output operations, and rectangles to represent processing.
Programmers also use decisions to control repetition of instruc-
tion sets.
• To avoid creating an infinite loop wflen you repeat instructions,
you can test for a sentinel value. You represent a decision in a
flowcflart by drawing a diamond-sflaped symbol tflat contains a
question, tfle answer to wflicfl is eitfler yes or no.
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
Key
Terms
• You can type a program into a plain text editor or one tflat is part
of an integrated development environment. Wflen a program’s
data values are entered from a keyboard, tfley can be entered at tfle
command line in a text environment or in a GUI. Eitfler way, tfle
logic is similar.
• Procedural and object-oriented programmers approacfl problems 29
differently. Procedural programmers concentrate on tfle actions
performed witfl data. Object-oriented programmers focus on
objects and tfleir beflaviors and attributes.
Key Terms
A computer system is a combination of all tfle components required
to process and store data using a computer.
Hardware is tfle collection of pflysical devices tflat comprise a com-
puter system.
Software consists of tfle programs tflat tell tfle computer wflat to
do.
Programs are sets of instructions for a computer.
Programming is tfle act of developing and writing programs.
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
Key
Terms
Users (or end users) are people wflo employ and benefit from
com- puter programs.
Documentation consists of all tfle supporting paperwork for a
program.
An algorithm is tfle sequence of steps necessary to solve any
31
problem.
An IPO chart is a program development tool tflat delineates input,
processing, and output tasks.
A TOE chart is a program development tool tflat lists tasks, objects,
and events.
Desk-checking is tfle process of walking tflrougfl a program
solution on paper.
A high-level programming language supports Englisfl-like syntax.
Macfline language is tfle low-level language made up of 1s and
0s tflat tfle computer understands.
A syntax error is an error in language or grammar.
Debugging is tfle process of finding and correcting program
errors.
Conversion is tfle entire set of actions an organization must take to
switcfl over to using a new program or set of programs.
Maintenance consists of all tfle improvements and corrections made
to a program after it is in production.
Pseudocode is an Englisfl-like representation of tfle logical steps it
takes to solve a problem.
A flowchart is a pictorial representation of tfle logical steps it takes
to solve a problem.
An input symbol
parallelogram in flowcflarts.
indicates an input operation and is represented by
a
A processing symbol indicates a processing operation and is
sented
repre- by a rectangle in flowcflarts.
An output symbol indicates an output operation and is represented
by a parallelogram in flowcflarts.
An input/output symbol or I/O symbol is represented by a
gram in flowcflarts.
parallelo-
Flowlines, or arrows, connect tfle steps in a
flowcflart.
A terminal symbol, or start/stop symbol, is used at eacfl end of
a flowcflart. Its sflape is a lozenge.
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
Review Questions
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
Review Questions
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
19. Wflen you write a program tflat will run in a GUI environ-
ment as opposed to a command-line environment,
.
a. tfle logic is very different
b. some syntax is different
c. you do not need to plan tfle logic
d. users are more confused
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
Exercises
Exercises
1. Matcfl tfle definition witfl tfle appropriate
term.
1. Computer system devices a. compiler
2. Anotfler word for programs b. syntax 37
3. Language rules c. logic
4. Order of instructions d. flardware
5. Language translator e. software
1. Input A.
2. Processing B.
3. Output C.
4. Decision D.
5. E.
Terminal
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
Licensed to: iChapters User
Exercises
Game Zone
9. In 1952, A. S. Douglas wrote flis University of Cambridge
Pfl.D. dissertation on fluman-computer interaction, and
created tfle first grapflical computer game—a version of
39
Tic-Tac-Toe. The game was programmed on an EDSAC
vacuum-tube mainframe computer. The first computer game
is generally assumed to be “Spacewar!”, developed in 1962 at
MIT; tfle first commercially available video game was “Pong,”
introduced by Atari in 1972. In 1980, Atari’s “Asteroids”
and “Lunar Lander” became tfle first video games to be
registered witfl tfle U. S. Copyrigflt Office. Througflout tfle
1980s, players spent flours witfl games tflat now seem very
simple and unglamorous; do you recall playing “Adventure,”
“Oregon Trail,” “Wflere in tfle World Is Carmen Sandiego?,”
or “Myst”?
Today, commercial computer games are mucfl more complex;
tfley require many programmers, grapflic artists, and tes-
ters to develop tflem, and large management and marketing
staffs are needed to promote tflem. A game migflt cost
many millions of dollars to develop and market, but a
successful game migflt earn flundreds of millions of dollars.
Obviously, witfl tfle brief introduction to programming you
flave flad
in tflis cflapter, you cannot create a very sopflisticated game.
However, you can get started.
Mad Libs© is a cflildren’s game in wflicfl players provide a
few words tflat are tflen incorporated into a silly story. The
game flelps cflildren understand different parts of speecfl
because tfley are asked to provide specific types of words.
For
adjective, and a past-tense verb. The cflild migflt reply witfl
example, you migflt
sucfl answers ask “book,”
as “table,” a cflild for a noun,
“silly,” anotfler noun,
and “studied.” The
an
created
newly Mad Lib migflt be:
Mary flad a little table
Its book was silly as snow
And everywflere tflat
Mary studied
The table was sure to go.
Create tfle logic for a Mad Lib program tflat accepts five
words from input, tflen creates and displays a sflort story or
nursery rflyme tflat uses tflose words.
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions
CHAPTER 1 An Overview of Computers and Programming
Up for Discussion
10. Wflicfl is tfle better tool for learning programming—
flowcflarts or pseudocode? Cite any educational researcfl you
can find.
40
11. Wflat is tfle image of tfle computer programmer in popular
culture? Is tfle image different in books tflan in TV sflows
and movies? Would you like tflat image for yourself?
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions