Farrell23936 1111823936 02 01 Chapter01
Farrell23936 1111823936 02 01 Chapter01
Some of the product names and company names used in this book have
been used for identification purposes only and may be trademarks or
registered trademarks of their respective manufacturers and sellers.
Purchase any of our products at your local college store or at our preferred
online store www.ichapters.com
Printed in Canada
1 2 3 4 5 6 7 14 13 12 11 10
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 require it.
Licensed to: iChapters User
An Overview of
CHAPTER 1
Computers and
Programming
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 require it.
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 require it.
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 require it.
Licensed to: iChapters User
Random storage. Internal storage is volatile—its contents are lost when the
access computer is turned off or loses power. Usually, you want to be able to
memory, or
retrieve and perhaps modify the stored instructions later, so you also
RAM, is a
form of inter-
store them on a permanent storage device, such as a disk. Permanent
nal, volatile memory. It is storage devices are nonvolatile—that is, their contents are persistent
hardware on which the and are retained even when power is lost.
4
programs that are cur-
After a computer program is stored in memory, it must be trans-
rently running and the
data items that are lated from your programming language statements to machine
currently being used language that represents the millions of on/off circuits within the
are stored for quick computer. Each programming language uses a piece of software,
access. called a compiler or an interpreter, to translate your program
code into machine language. Machine language is also called
binary language, and is represented as a series of 0s and 1s. The
The program compiler or interpreter that translates your code tells you if any
statements programming language component has been used incorrectly.
you write in a Syntax errors are relatively easy to locate and correct because
programming
the compiler or interpreter you use highlights every syntax error.
language are
known as source code.
If you write a computer program using a language such as C++
The translated machine but spell one of its words incorrectly or reverse the proper order
language statements are of two words, the software lets you know that it found a mistake
known as object code. by displaying an error message as soon as you try to translate the
program.
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 require it.
Licensed to: iChapters User
In each Two Truths and a Lie section, two of the numbered statements are true,
5
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.
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 require it.
Licensed to: iChapters User
After you learn Just as baking directions can be given correctly in Mandarin, Urdu,
French, you or Spanish, the same program logic can be expressed in any number
automatically
of programming languages. Because this book is not concerned with
know, or can
easily figure
any specific language, the programming examples could have been
out, many Spanish words. written in Visual Basic, C++, or Java. For convenience, this book uses
Similarly, after you learn instructions written in English!
6
one programming lan-
Most simple computer programs include steps that perform input,
guage, it is much easier
to understand several processing, and output. Suppose you want to write a computer
other languages. program to double any number you provide. You can write such a
program in a programming language such as Visual Basic or Java,
but if you were to write it using English-like statements, it would
You will learn look like this:
about the odd
input myNumber
elimination of
set myAnswer = myNumber * 2
the space
output myAnswer
between
words like my and The number-doubling process includes three instructions:
Number in Chapter 2.
• The instruction to input myNumber is an example of an input
operation. When the computer interprets this instruction, it
knows to look to an input device to obtain a number. When you
work in a specific programming language, you write instructions
that tell the computer which device to access for input. For exam-
ple, when a user enters a number as data for a program, the user
might click on the number with a mouse, type it from a keyboard,
or speak it into a microphone. Logically, however, it doesn’t really
matter which hardware device is used, as long as the computer
knows to look for a number. When the number is retrieved from
an input device, it is placed in the computer’s memory at the
location named myNumber. The location myNumber is a variable. A
variable is a named memory location whose value can vary—for
example, the value of myNumber might be 3 when the program is
used for the first time and 45 when it is used the 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.
For now, this book is only concerned with the logic of the operation, not the minor
Programmers details.
use an aster-
isk to indicate
multiplication.
• The instruction set myAnswer = myNumber * 2 is an example
You will learn of a processing operation. Mathematical operations are not the
more about arithmetic only kind of processing operations, but they are very typical. As
statements in Chapter 2. with input operations, the type of hardware used for 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 require it.
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.
program with no syntax errors can execute, but might produce incorrect results.
The false statement is #1. A program with syntax errors cannot execute; a
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 require it.
Licensed to: iChapters User
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 require it.
Licensed to: iChapters User
needs a Web site to provide buyers with an online shopping cart in The term end
which to gather their orders. Because programmers are providing a user distin-
guishes those
service to these users, programmers must first understand what the
who actually
users want. Although when a program runs, you usually think of the use and ben-
logic as a cycle of input-processing-output operations; when you efit from a software prod-
plan a program, you think of the output first. After you understand uct from others in an
9
what the desired result is, you can plan what to input and process to organization who might
achieve it. purchase, install, or have
other contact with the
Suppose the director of Human Resources says to a programmer, software.
“Our department needs a list of all employees who have been here
over five years, because we want to invite them to a special thank-you
dinner.” On the surface, this seems like a simple request. An experi-
enced programmer, however, will know that the request is incom-
plete. For example, you might not know the answers to the following
questions about which employees to include:
• Does the director want a list of full-time employees only, or a list
of full- and part-time employees together?
• Does she want people who have worked for the company on a
month-to-month contractual basis over the past five years, or only
regular, permanent employees?
• Do the listed employees need to have worked for the organization
for five years as of today, as of the date of the dinner, or as of some
other cutoff date?
• What about an employee who, for example, worked three years,
took a two-year leave of absence, and has been back for three years?
The programmer cannot make any of these decisions; the user (in this
case, the Human Resources director) must address these questions.
More decisions still might be required. For example:
• What data should be included for each listed employee? Should
the list contain both first and last names? Social Security numbers?
Phone numbers? Addresses?
• Should the list be in alphabetical order? Employee ID number
order? Length-of-service order? Some other order?
• Should the employees be grouped by any criteria, such as depart-
ment number or years of service?
Several pieces of documentation are often provided to help the pro-
grammer understand the problem. Documentation consists of all the
supporting paperwork for a program; it might include items such
as original requests for the program from users, sample output, and
descriptions of the 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 require it.
Licensed to: iChapters User
Watch the video Really understanding the problem may be one of the most difficult
The Program aspects of programming. On any job, the description of what the user
Development
needs may be vague—worse yet, users may not really know what they
Cycle, Part 1.
want, and users who think they know frequently change their minds
after seeing sample output. A good programmer is often part coun-
selor, part detective!
10
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 require it.
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, however, needs planning before writing—and
so do most programs.
Which step is harder: planning the logic or coding the program?
Right now, it may seem to you that writing in a programming lan- 11
guage is a very difficult task, considering all the spelling and syntax
rules you must learn. However, the planning step is actually more
difficult. Which is more difficult: thinking up the twists and turns to
the plot of a best-selling mystery novel, or writing a translation of an
existing novel from English to Spanish? And who do you think gets
paid more, the writer who creates the plot or the translator? (Try
asking friends to name any famous translator!)
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 require it.
Licensed to: iChapters User
the cat,” the compiler at first might point out only one syntax error.
The second word, “dg,” is illegal because it is not part of the English
After a pro- language. Only after you corrected the word to “dog” would the com-
gram has piler find another syntax error on the third word, “chase,” because it is
been trans- the wrong verb form for the subject “dog.” This doesn’t mean “chase”
lated into is necessarily the wrong word. Maybe “dog” is wrong; perhaps the
12 machine lan- subject should be “dogs,” in which case “chase” is right. Compilers
guage, the machine lan-
don’t always know exactly what you mean, nor do they know what the
guage program is saved
and can be run any num-
proper correction should be, but they do know when something is
ber of times without wrong with your syntax.
repeating the translation
When writing a program, a programmer might need to recompile the
step. You only need to
retranslate your code if
code several times. An executable program is created only when the
you make changes to code is free of syntax errors. When you run an executable program, it
your source code typically also might require input data. Figure 1-2 shows a diagram of
statements. this entire process.
If there are no
Write and correct Compile the syntax errors Executable
the program code program program
If there are
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 require it.
Licensed to: iChapters User
If you execute the program, provide the value 2 as input to the pro-
gram, and the answer 4 is displayed, you have executed one successful
test run of the program.
However, if the answer 40 is displayed, maybe the program contains
a logical error. Maybe the second line of code was mistyped with an
extra zero, so that the program reads:
input myNumber Don’t Do It
set myAnswer = myNumber * 20 The programmer typed
output myAnswer "20" instead of "2".
The process
Placing 20 instead of 2 in the
of finding and
multiplication statement caused a logical error. Notice that nothing is correcting
syntactically wrong with this second program—it is just as reasonable program
to multiply a number by 20 as by 2—but if the programmer intends errors is
only to double myNumber, then a logical error has occurred. called debugging.
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 require it.
Licensed to: iChapters User
from the five-year list. Many companies do not know that their
software has a problem until an unusual circumstance occurs—for
Chapter 4
contains more
example, the first time an employee has more than nine depen-
information on dents, the first time a customer orders more than 999 items at a
testing time, or when (as well-documented in the 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 require it.
Licensed to: iChapters User
1. Understanding the problem that must be solved can be one of the most dif-
15
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 have already seen examples of statements that represent pseudo-
code earlier in this chapter, and there is nothing mysterious about
them. 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 require it.
Licensed to: iChapters User
Using pseudocode involves writing down all the steps you will use in
a program. Usually, programmers preface their pseudocode with a
beginning statement like start and end it with a terminating state-
ment like stop. The statements between start and stop look like
English and are indented slightly so that start and stop stand out.
Most programmers do not bother with punctuation such as periods
16
at the end of pseudocode statements, although it would not be wrong
to use them if you prefer that style. Similarly, there is no need to capi-
talize the first word in a sentence, although you might choose to do
so. This book follows the conventions of using lowercase letters for
verbs that begin pseudocode statements and omitting periods at the
end of statements.
Pseudocode is fairly flexible because it is a planning tool, and not the
final product. Therefore, for example, you might prefer any of the
following:
• Instead of start and stop, some pseudocode developers would
use the 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 require it.
Licensed to: iChapters User
Drawing Flowcharts
Some professional programmers prefer writing pseudocode to
drawing flowcharts, because using pseudocode is more similar to
writing the final statements in the programming language. Others
prefer drawing flowcharts to represent the logical flow, because flow-
charts allow programmers to visualize more easily how the program You can draw 17
statements will connect. Especially for beginning programmers, flow- a flowchart by
charts are an excellent tool to help them visualize how the statements hand or use
software,
in a program are interrelated.
such as
When you create a flowchart, you draw geometric shapes that contain Microsoft Word and
the individual statements and that are connected with arrows. You Microsoft PowerPoint,
that contains flowcharting
use a parallelogram to represent
tools. You can use sev-
an input symbol, which indicates input myNumber eral other software pro-
an input operation. You write an grams, such as Visio and
input statement in English inside the Visual Logic, specifically
parallelogram, as shown in Figure 1-3. Figure 1-3 Input symbol to 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 show the correct sequence of these statements, you use arrows, or
contains a
flowlines, to connect the steps. Whenever possible, most of a flow- summary of
chart should read from top to bottom or from left to right on a page. all the flow-
That’s the way we read English, so when flowcharts follow this con- chart symbols
vention, they 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 require it.
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 the flowchart or pseudocode has been developed, the
programmer only needs to: (1) buy a computer, (2) buy a language
compiler, (3) learn a programming language, (4) code the pro-
gram, (5) attempt to compile it, (6) fix the syntax errors, (7) com-
pile it again, (8) test it with several sets of data, and (9) put it into
production.
“Whoa!” you are probably saying to yourself. “This is simply not worth
it! All that work to create a flowchart or pseudocode, and then all those
other steps? For five dollars, I can buy a pocket calculator that 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 require it.
Licensed to: iChapters User
double any number for me instantly!” You are absolutely right. If this When you tell
were a real computer program, and all it did was double the value of a a friend how
to get to your
number, it would not be worth the effort. Writing a computer program
house, you
would be worthwhile only if you had many—let’s say 10,000—numbers might write a
to double in a limited amount of time—let’s say the next two minutes. series of instructions or
you might draw a map.
Unfortunately, the number-doubling program represented in 19
Pseudocode is similar to
Figure 1-6 does not double 10,000 numbers; it doubles only one. You
written, step-by-step
could execute the program 10,000 times, of course, but that would instructions; a flowchart,
require you to sit at the computer and tell it to run the program over like a map, is a visual
and over again. You would be better off with a program that could representation of the
process 10,000 numbers, one after the other. same thing.
One solution is to write the program shown in Figure 1-7 and execute
the same steps 10,000 times. Of course, writing this program would
be very time consuming; you might as well buy the 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 to
set myAnswer = myNumber * 2 write such a repetitious
output myAnswer list of instructions.
…and so on for 9,997 more 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 require it.
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
progress any further while it is waiting for input; meanwhile, the pro-
gram is occupying computer memory and tying up operating system
resources. Refusing to enter any more numbers is not a practical solu-
tion. Another way to end the program is simply to turn off the com-
puter. But again, that’s neither the best way nor an elegant solution.
A superior way to end the program is to set a predetermined value for 21
myNumber that means “Stop the program!” For example, the program-
mer and the user could agree that the user will never need to know
the double of 0, so the user could enter a 0 to stop. The program could
then test any incoming value contained in myNumber and, if it is a 0,
stop the program. Testing a value is also called making a decision.
You represent a decision in a flowchart by drawing a decision symbol,
which is shaped like a diamond. The diamond usually contains a
question, the answer to which is one of two mutually exclusive
options—often yes or no. All good computer questions have only A yes-or-no
decision is
two mutually exclusive answers, such as yes and no or true and false.
called a
For example, “What day of the year is your birthday?” is not a good binary deci-
computer question because there are 366 possible answers. However, sion, because
“Is your birthday June 24?” is a good computer question because, for there are two possible
everyone in the world, the answer is either yes or no. outcomes.
The question to stop the doubling program should be “Is the value
of myNumber just entered equal to 0?” or “myNumber = 0?” for short. The
complete flowchart will now look like the one shown in Figure 1-9.
start Don’t Do It
This logic is not
structured; you will
learn about structure
input myNumber in Chapter 3.
myNumber Yes
stop
= 0?
No
set myAnswer =
myNumber times 2
output myAnswer
start Don’t Do It
This logic is not
structured; you will
learn about structure
input myNumber in Chapter 3.
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 require it.
Licensed to: iChapters User
a file sentinel.
The false statement is #3. The term eof (for “end of file”) is the common term for
Understanding Programming
and User Environments
Many approaches can be used to write and execute a computer pro-
gram. When you plan a program’s logic, you can use a flowchart or
pseudocode, or a combination of the two. When you code the pro-
gram, you can type statements into a variety of text editors. When
your program executes, it might accept input from a keyboard,
mouse, microphone, or any other input device, and when you provide
a program’s output, you might use text, images, or sound. This section
describes the 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 require it.
Licensed to: iChapters User
A text editor is a program that you use to create simple text files. It
is similar to a word processor, but without as many features. You can
use a text editor such as Notepad that is included with Microsoft
Windows. Figure 1-11 shows a C# program in Notepad that accepts
a number and doubles it. An advantage to using a simple text editor
to type and save a program is that the completed program does not
24
require much disk space for storage. For example, the file shown 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 require it.
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 require it.
Licensed to: iChapters User
Many programs are not run at the command line in a text environ-
ment, but are run using a graphical user interface, or GUI (pro-
nounced “gooey”), which allows users to interact with a program in
a graphical environment. When running a GUI program, the user
might type input into a text box or use a mouse or other pointing
device to select options on the screen. Figure 1-14 shows a number-
26
doubling program that performs exactly the same task as the one in
Figure 1-13, but this program uses a GUI.
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 types.
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 require it.
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 require it.
Licensed to: iChapters User
Chapter Summary
• Together, computer hardware (physical devices) and software
(instructions) accomplish three major operations: input, processing,
and output. You write computer instructions in a computer pro-
gramming language that requires specific syntax; the instructions
are translated into machine language by a compiler or interpreter.
When both the syntax and logic of a program are correct, you can
run, or execute, the program to produce the desired results.
• For a program to work properly, you must develop correct logic.
Logical errors are much more difficult to locate than syntax errors.
• A programmer’s job involves understanding the problem, plan-
ning the logic, coding the program, translating the program into
machine language, testing the program, putting the program into
production, and maintaining it.
• When programmers plan the logic for a solution to a program-
ming problem, they often use flowcharts or pseudocode. When
you draw a flowchart, 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 when you repeat instructions,
you can test for a sentinel value. You represent a decision in a
flowchart by drawing a diamond-shaped symbol that contains a
question, the answer to which is either 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 require it.
Licensed to: iChapters User
Key Terms
• You can type a program into a plain text editor or one that is part
of an integrated development environment. When a program’s
data values are entered from a keyboard, they can be entered at the
command line in a text environment or in a GUI. Either way, the
logic is similar.
• Procedural and object-oriented programmers approach problems 29
differently. Procedural programmers concentrate on the actions
performed with data. Object-oriented programmers focus on
objects and their behaviors and attributes.
Key Terms
A computer system is a combination of all the components required
to process and store data using a computer.
Hardware is the collection of physical devices that comprise a com-
puter system.
Software consists of the programs that tell the computer what to do.
Programs are sets of instructions for a computer.
Programming is the act of developing and writing programs.
Application software comprises all the programs you apply
to a task.
System software comprises the programs that you use to manage
your computer.
Input describes the entry of data items into computer memory using
hardware devices such as keyboards and mice.
Data items include all the text, numbers, and other information pro-
cessed by a computer.
Processing data items may involve organizing them, checking them
for accuracy, or performing mathematical operations on them.
The central processing unit, or CPU, is the hardware component that
processes data.
Output describes the operation of retrieving information from mem-
ory and sending it to a device, such as a monitor or printer, so people
can view, interpret, and work with the results.
Storage devices are types of hardware equipment, such as disks, that
hold information for later retrieval.
Programming languages, such as Visual Basic, C#, C++, Java,
or COBOL, are used to write 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 require it.
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 require it.
Licensed to: iChapters User
Key Terms
Users (or end users) are people who employ and benefit from com-
puter programs.
Documentation consists of all the supporting paperwork for a
program.
An algorithm is the sequence of steps necessary to solve any
31
problem.
An IPO chart is a program development tool that delineates input,
processing, and output tasks.
A TOE chart is a program development tool that lists tasks, objects,
and events.
Desk-checking is the process of walking through a program solution
on paper.
A high-level programming language supports English-like syntax.
Machine language is the low-level language made up of 1s and 0s
that the computer understands.
A syntax error is an error in language or grammar.
Debugging is the process of finding and correcting program errors.
Conversion is the entire set of actions an organization must take to
switch over to using a new program or set of programs.
Maintenance consists of all the improvements and corrections made
to a program after it is in production.
Pseudocode is an English-like representation of the logical steps it
takes to solve a problem.
A flowchart is a pictorial representation of the logical steps it takes to
solve a problem.
An input symbol indicates an input operation and is represented by a
parallelogram in flowcharts.
A processing symbol indicates a processing operation and is repre-
sented by a rectangle in flowcharts.
An output symbol indicates an output operation and is represented
by a parallelogram in flowcharts.
An input/output symbol or I/O symbol is represented by a parallelo-
gram in flowcharts.
Flowlines, or arrows, connect the steps in a flowchart.
A terminal symbol, or start/stop symbol, is used at each end of a
flowchart. Its shape 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 require it.
Licensed to: iChapters User
Review Questions
1. Computer programs are also known as .
a. hardware
b. software
c. data
d. information
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 require it.
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 require it.
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 require it.
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 require it.
Licensed to: iChapters User
19. When you write a program that will run in a GUI environ-
ment as opposed to a command-line environment,
.
a. the logic is very different
b. some syntax is different
c. you do not need to plan the 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 require it.
Licensed to: iChapters User
Exercises
Exercises
1. Match the definition with the appropriate term.
1. Computer system devices a. compiler
2. Another word for programs b. syntax 37
3. Language rules c. logic
4. Order of instructions d. hardware
5. Language translator e. software
1. Input A.
2. Processing B.
3. Output C.
4. Decision D.
5. Terminal E.
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 require it.
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 require it.
Licensed to: iChapters User
Exercises
Game Zone
9. In 1952, A. S. Douglas wrote his University of Cambridge
Ph.D. dissertation on human-computer interaction, and
created the first graphical computer game—a version of
Tic-Tac-Toe. The game was programmed on an EDSAC 39
vacuum-tube mainframe computer. The first computer game
is generally assumed to be “Spacewar!”, developed in 1962 at
MIT; the first commercially available video game was “Pong,”
introduced by Atari in 1972. In 1980, Atari’s “Asteroids”
and “Lunar Lander” became the first video games to be
registered with the U. S. Copyright Office. Throughout the
1980s, players spent hours with games that now seem very
simple and unglamorous; do you recall playing “Adventure,”
“Oregon Trail,” “Where in the World Is Carmen Sandiego?,”
or “Myst”?
Today, commercial computer games are much more complex;
they require many programmers, graphic artists, and tes-
ters to develop them, and large management and marketing
staffs are needed to promote them. A game might cost many
millions of dollars to develop and market, but a successful
game might earn hundreds of millions of dollars. Obviously,
with the brief introduction to programming you have had
in this chapter, you cannot create a very sophisticated game.
However, you can get started.
Mad Libs© is a children’s game in which players provide a
few words that are then incorporated into a silly story. The
game helps children understand different parts of speech
because they are asked to provide specific types of words. For
example, you might ask a child for a noun, another noun, an
adjective, and a past-tense verb. The child might reply with
such answers as “table,” “book,” “silly,” and “studied.” The newly
created Mad Lib might be:
Mary had a little table
Its book was silly as snow
And everywhere that Mary studied
The table was sure to go.
Create the logic for a Mad Lib program that accepts five
words from input, then creates and displays a short story or
nursery rhyme that uses those 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 require it.
CHAPTER 1 An Overview of Computers and Programming
Up for Discussion
10. Which is the better tool for learning programming—
flowcharts or pseudocode? Cite any educational research you
can find.
40
11. What is the image of the computer programmer in popular
culture? Is the image different in books than in TV shows and
movies? Would you like that 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 require it.