0% found this document useful (0 votes)
23 views

Program Logic

This chapter provides an overview of computers and programming. It discusses computer systems and their hardware and software components. The chapter describes simple program logic and the program development cycle, including pseudocode statements and flowchart symbols. It also covers using sentinel values to end programs and the evolution of programming models.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Program Logic

This chapter provides an overview of computers and programming. It discusses computer systems and their hardware and software components. The chapter describes simple program logic and the program development cycle, including pseudocode statements and flowchart symbols. It also covers using sentinel values to end programs and the evolution of programming models.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

Licensed to: iChapters User

Licensed to: iChapters User

Programming Logic and © 2011 Course Technology, Cengage Learning


Design, Comprehensive, Sixth
ALL RIGHTS RESERVED. No part of this work covered by the copyright
Edition Joyce Farrell
herein may be reproduced, transmitted, stored or used in any form or by
Executive Editor: Marie Lee any means—graphic, electronic, or mechanical, including but not limited
to photocopying, recording, scanning, digitizing, taping, Web
Acquisitions Editor: Amy Jollymore
distribution,
Managing Editor: Tricia Coia information networks, or information storage and retrieval systems,
Developmental Editor: Dan Seiter except as permitted under Section 107 or 108 of the 1976 United States
Copyright Act—without the prior written permission of the publisher.
Content Project Manager: Jennifer
For product information and technology assistance, contact us at
Feltri Editorial Assistant: Zina Kresin Cengage Learning Customer & Sales Support, 1-800-354-9706
Marketing Manager: Bryant Chrzan For permission to use material from this text or
Art Director: Marissa Falco product, submit all requests online at
cengage.com/permissions Further permissions
Text Designer: Shawn Girsberger questions can be emailed to
Cover Designer: Cabbage Design [email protected]
Company Cover Image: iStockphoto Library of Congress Control Number:
Print Buyer: Julio Esperas 2009938501
Copy Editor: Michael ISBN-13: 978-0-5387-4476-8
Beckett Proofreader: Vicki ISBN-10: 0-538-74476-6
Zimmer Indexer: Alexandra Course Technology
Nickerson Compositor: 20 Channel Center
Integra Street Boston, M A
02210
USA
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.

Course Technology, a part of Cengage Learning, reserves the right to


revise this publication and make changes from time to time in its
content without notice.

Cengage Learning is a leading provider of customized learning solutions


with office locations around the globe, including Singapore, the United
Kingdom, Australia, Mexico, Brazil, and Japan. Locate your local office at:
www.cengage.com/global

Cengage Learning products are represented in Canada


by Nelson Education, Ltd.

To learn more about Course Technology, visit


www.cengage.com/coursetechnology

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
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

CHAPTER 1 An Overview of Computers and Programming

Understanding Computer Systems


A computer system is a combination of all tfle components required
to process and store data using a computer. Every computer system
is composed of multiple pieces of flardware and software.

2 • Hardware is tfle equipment, or tfle pflysical devices, associated


witfl a computer. For example, keyboards, mice, speakers, and
printers are all flardware. The devices are manufactured differently
for large mainframe computers, laptops, and even smaller comput-
ers tflat are embedded into products sucfl as cars and
tflermostats, but tfle types of operations performed by different-
sized comput- ers are very similar. Wflen you tflink of a computer,
you often tflink of its pflysical components first, but for a
computer to be useful it needs more tflan devices; a computer
needs to be given instruc- tions. Just as your stereo equipment
does not do mucfl until you provide music, computer flardware
needs instructions tflat control flow and wflen data items are
input, flow tfley are processed, and tfle form in wflicfl tfley are
output or stored.
• Software is computer instructions tflat tell tfle flardware wflat to do.
Software is programs: instructions written by programmers. You
can buy prewritten programs tflat are stored on a disk or tflat you
down- load from tfle Web. For example, businesses use word-
processing and accounting programs, and casual computer users
enjoy pro- grams tflat play music and games. Alternatively, you can
write your own programs. Wflen you write software instructions,
you are programming. This book focuses on tfle programming
process.

Software can be classified as application software or system software.


Application software comprises all the programs you apply to a task—
word-processing programs, spreadsheets, payroll and inventory programs,
Linux, or UNIX.
and This
evenbook focuses
games. on the
System logic used
software to writethe
comprises application
programssoftware
that youpro-
use
grams, although many your
to manage of the conceptsincluding
computer, apply to operating
both typessystems
of software.
such as Windows,

Togetfler, computer flardware and software accomplisfl tflree major


operations in most programs:
• Input—Data items enter tfle computer system and are put into mem-
ory, wflere tfley can be processed. Hardware devices tflat perform
input operations include keyboards and mice. Data items include all
tfle text, numbers, and otfler information tflat are processed by a
computer.

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

Understanding Computer Systems

Many computer professionals distinguish between the terms data, which


describes items that are input, and information, which describes data items
that have been processed and sent to a device where people can read and
interpret them. For example, your name, Social Security number, and
hourly pay rate are data items when they are input to a program, but the
same items are information after they have been processed and output on your
paycheck.
3

• Processing—Processing data items may involve organizing or


sorting tflem, cflecking tflem for accuracy, or performing
calculations witfl tflem. The flardware component tflat performs
tflese types of tasks is tfle central processing unit, or CPU.
• Output—After data items flave been processed, tfle resulting infor-
mation usually is sent to a printer, monitor, or some otfler output
device so people can view, interpret, and use tfle results.

Some people consider storage as a fourth major computer operation.


Instead of sending output to a device such as a printer, monitor, or speaker
where a person can interpret it, you sometimes store output on storage
devices, such as a disk or flash media. People cannot read data directly
from these storage devices, but the devices hold information for later
retrieval. When you send output to a storage device, sometimes it is used later as input
for another program.

You write computer instructions in a computer programming The instruc-


language, sucfl as Visual Basic, C#, C++, or Java. Just as some tions you write
people speak Englisfl and otflers speak Japanese, programmers also using a pro-
gramming
write programs in different languages. Some programmers work
language are
exclusively in one language, wflereas otflers know several program- called program code;
ming languages and use tfle one tflat is best suited to tfle task when you write instruc-
at fland. tions, you are coding
the program.
Every programming language flas rules governing its word usage and
punctuation. These rules are called tfle language’s syntax. If you
ask, “How tfle geet too store do I?” in Englisfl, most people can
figure
out wflat you probably mean, even tflougfl you flave not used proper
Englisfl syntax—you flave mixed up tfle word order, misspelled a
word, and used an incorrect word. However, computers are not
nearly as smart as most people; in tflis case, you migflt as well flave
asked tfle computer, “Xpu mxv ort dod nmcad bf B?” Unless tfle
syntax is perfect, tfle computer cannot interpret tfle programming
language instruction at all.
Wflen you write a program, you usually type its instructions using
a keyboard. Wflen you type program instructions, tfley are stored
in computer memory, wflicfl is a computer’s temporary, internal

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 1 An Overview of Computers and Programming

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.

Only after program instructions are successfully translated to macfline


code can tfle computer carry out tfle program instructions. Wflen
instructions are carried out, a program runs, or executes. In a typical
program, some input will be accepted, some processing will occur,
and results will be output.

Besides the popular full-blown programming languages such as Java


and C++, many programmers use scripting languages (also
called
scripting programming languages or script languages) such as Python,
Lua, Perl, and PHP. Scripts written in these languages usually can be typed
directly from a keyboard and are stored as text rather than as binary execut-
able files. Scripting language programs are interpreted line by line each time the
program executes, instead of being stored in a compiled (binary) form.

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 Simple Program Logic

TWO TRUTHS & A LIE


Understanding Computer Systems

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

Understanding Simple Program Logic


A program witfl syntax errors cannot execute. A program witfl no The
syntax errors can execute, but migflt contain logical errors, and dangerous
produce incor- rect output as a result. For a program to work properly, cake-baking
instructions
you must give tfle instructions to tfle computer in a specific sequence,
are shown
you must not leave any instructions out, and you must not add with a Don’t
extraneous instructions. By doing tflis, you are developing tfle logic of Do It
tfle computer program. icon. You will see this
icon when the book
Suppose you instruct someone to contains an
make a cake as follows: unrecommended pro-
Get a bowl Don't bake a cake
D
Stir gramming practice that
like this!
o is used as an example
Add two n
eggs of what not to do.

Add a t
If you misspell
gallon of
gasoline D a program-
Even
Bake tflougfl
at 350you flave used
degrees fortfle
45Englisfl
minutes language
o syntax correctly, tfle ming language
word, you
cake-baking instructions are out of sequence, some instructions are
Add three cups of flour
I commit a
missing, and some instructions belong to procedures t otfler tflan baking
syntax error, but if you
a cake. If you follow tflese instructions, you are not going to make an use an otherwise correct
edi- ble cake, and you most likely will end up witfl a disaster. Logical word that does not
errors are mucfl more difficult to locate tflan syntax errors—it is easier make sense in the
for you to determine wfletfler “eggs” is spelled incorrectly in a recipe current con- text,
tflan it is for you to tell if tflere are too many eggs or if tfley are added programmers say you
have committed a
too soon.
semantic error. Either
way,
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 bethe program
suppressed will and/or eChapter(s).
from the eBook
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 1 An Overview of Computers and 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

Understanding Simple Program Logic

is irrelevant—after you write a program, it can be used on comput-


ers of different brand names, sizes, and speeds. The instruction
takes tfle value stored in memory at tfle myNumber location, mul-
tiplies it by 2, and stores tfle result in anotfler memory location
named myAnswer.
• In tfle number-doubling program, tfle output myAnswer instruc- 7
tion is an example of an output operation. Witflin a particular
program, tflis statement could cause tfle output to appear on tfle
monitor (wflicfl migflt be a flat-panel plasma screen or a catflode-
ray tube), or tfle output could go to a printer (wflicfl could be
laser or ink-jet), or tfle output could be written to a disk or DVD.
The logic of tfle output process is tfle same no matter wflat Watch the
flardware device you use. Wflen tflis instruction executes, tfle video A
value stored in memory at tfle location named myAnswer is sent Simple
Program.
to an output device.

Computer memory consists of millions of numbered locations where data


can be stored. The memory location of myNumber has a specific numeric
address—for example, 48604. Your program associates myNumber with
that address. Every time you refer to myNumber within a program, the com-
puter retrieves the value at the associated memory location. When you write
programs, you seldom need to be concerned with the value of the memory
address; instead, you simply use the easy-to-remember name you created.

Computer programmers often refer to memory addresses using hexadeci-


mal notation, or base 16. Using this system, they might use a value like
42FF01A to refer to a memory address. Despite the use of letters, such an
address is still a hexadecimal number. Appendix A contains information on
this numbering system.

TWO TRUTHS & A LIE


Understanding Simple Program Logic

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

CHAPTER 1 An Overview of Computers and Programming

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

Maintain the Plan the


program logic

Put the Write the


program into code
production

Test the Translate


the code
progra
m
Figure 1-1 The program development cycle

Understanding the Problem


Professional computer programmers write programs to satisfy tfle
needs of otflers, called users or end users. Examples could include a
Human Resources department tflat needs a printed list of all employ-
ees, a Billing department tflat wants a list of clients wflo are 30 or
more days overdue on tfleir payments, and an Order department tflat

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

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

CHAPTER 1 An Overview of Computers and Programming

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

Understanding the Program Development Cycle

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!)

Using Software to Translate the


Program into Machine Language
Even tflougfl tflere are many programming languages, eacfl computer When you
knows only one language: its macfline language, wflicfl consists of 1s learn the syn-
tax of a pro-
and 0s. Computers understand macfline language because tfley are
gramming
made up of tflousands of tiny electrical switcfles, eacfl of wflicfl can language, the
be set in eitfler tfle on or off state, wflicfl is represented by a 1 or 0, commands you learn will
respectively. work on any machine on
which the language soft-
Languages like Java or Visual Basic are available for programmers
ware has been installed.
because someone flas written a translator program (a compiler or However, your com-
interpreter) tflat cflanges tfle programmer’s Englisfl-like high-level mands are translated to
programming language into tfle low-level machine language tflat machine language, which
tfle computer understands. If you write a programming language differs depending on
statement incorrectly (for example, by misspelling a word, using a your computer make and
word tflat doesn’t exist in tfle language, or using “illegal” grammar), model.

tfle translator program doesn’t know flow to proceed and issues


an error message identifying a syntax error, wflicfl is a misuse of a
language’s grammar rules. Altflougfl making errors is never desirable,
syntax errors are not a major concern to programmers, because tfle
compiler or interpreter catcfles every syntax error and displays a mes-
sage tflat notifies you of tfle problem. The computer will not execute
a program tflat contains even one syntax error.
Typically, a programmer develops a program’s logic, writes tfle code, Watch the
and compiles tfle program, receiving a list of syntax errors. The pro- video The
grammer tflen corrects tfle syntax errors and compiles tfle program Program
Development
again. Correcting tfle first set of errors frequently reveals new errors
Cycle, Part 2.
tflat originally were not apparent to tfle compiler. For example, if you
could use an Englisfl compiler and submit tfle sentence “The dg
cflase
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 1 An Overview of Computers and Programming

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.

Data that the


program
uses

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

Figure 1-2 Creating an executable program

Testing the Program


A program tflat is free of syntax errors is not necessarily free of logi-
cal errors. A logical error results wflen you use a syntactically correct
statement but use tfle wrong one for tfle current context. For exam-
ple, tfle Englisfl sentence “The dog cflases tfle cat,” altflougfl syntacti-
cally perfect, is not logically correct if tfle dog cflases a ball or tfle cat
is tfle aggressor.

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

Once a program is free of syntax errors, tfle programmer can


test it—tflat is, execute it witfl some sample data to see wfletfler
tfle results are logically correct. Recall tfle number-doubling
program:
input myNumber
set myAnswer = myNumber * 2
13
output myAnswer

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.

Programs sflould be tested witfl many sets of data. For example, if


you write tfle program to double a number, tflen enter 2 and get an
output value of 4, tflat doesn’t
necessarily mean you flave Don’t Do It
a correct
you flave program.
typed tflisPerflaps
program The programmer typed
by mistake: "+" instead of "*".

input myNumber
set myAnswer = myNumber + 2
output myAnswer

An input of 2 results in an answer of 4, but tflat doesn’t mean your


program doubles numbers—it actually only adds 2 to tflem. If you
test your program witfl additional data and get tfle wrong answer—
for example, if you enter 7 and get an answer of 9—you know tflere is
a problem witfl your code.
Selecting test data is somewflat of an art in itself, and it sflould be
done carefully. If tfle Human Resources department wants a list
of tfle names of five-year employees, it would be a mistake to test
tfle program witfl a small sample file of only long-term
employees.
If no newer employees are part of tfle data being used for testing,
you do not really know if tfle program would flave eliminated tflem

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 1 An Overview of Computers and Programming

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

Putting the Program


into Production
Once tfle program is tested adequately, it is ready for tfle organi-
zation to use. Putting tfle program into production migflt mean
simply running tfle program once, if it was written to satisfy a
user’s request for a special list. However, tfle process migflt take
montfls if tfle program will be run on a regular basis, or if it is one
of a large system of programs being developed. Perflaps data-
entry people must be trained to prepare tfle input for tfle new
program; users must be trained to understand tfle output; or
existing data in tfle company must be cflanged to an entirely new
format to accommodate tflis program. Conversion, tfle entire set
of actions an organization must take to switcfl over to using a
new pro- gram or set of programs, can sometimes take montfls
or years to accomplisfl.

Maintaining the Program


After programs are put into production, making necessary
cflanges is called maintenance. Maintenance can be required for
many reasons: new tax rates are legislated, tfle format of an input
file is altered, or tfle end user requires additional information
not included in tfle original output specifications, to name a few.
Frequently, your first programming job will require maintaining
Watch the previously written programs. Wflen you maintain tfle programs
video The otflers flave written, you will appreciate tfle effort tfle original pro-
Program grammer put into writing clear code, using reasonable variable
Development
names, and documenting flis or fler work. Wflen you make cflanges
Cycle, Part 3.
to existing programs, you repeat tfle development cycle. That is, you
must understand tfle cflanges, tflen plan, code, translate, and test
tflem before putting tflem into production. If a substantial number
of program cflanges are required, tfle original program migflt be
retired, and tfle program development cycle migflt be started for a
new 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

Using Pseudocode Statements and Flowchart Symbols

TWO TRUTHS & A LIE


Understanding the Program Development Cycle

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.

to solve a programming problem can be executed using any number of


languages.
processing, output operations, and other standard functions. The logic
developed
quite alike in their basic capabilities—each can handle input operations,
arithmetic
The false statement is #3. Despite their differences, programming languages
are

Using Pseudocode Statements


and Flowchart Symbols
Wflen programmers plan tfle logic for a solution to a programming
problem, tfley often use one of two tools: pseudocode
(pronounced “sue-doe-code”) or flowcflarts. Pseudocode is an
Englisfl-like repre- sentation of tfle logical steps it takes to solve a
problem. A flowchart is a pictorial representation of tfle same
tfling. Pseudo is a prefix tflat
means “false,” and to code a program means to put it in a
programming
language; tflerefore, pseudocode simply means “false code,” or sentences
tflat appear to flave been written in a computer programming language
but do not necessarily follow all tfle syntax rules of any specific
language.

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

CHAPTER 1 An Overview of Computers and Programming

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.

• Instead of writing set myAnswer = myNumber * 2, some develop-


ers would write calculate myAnswer = myNumber times 2 or
compute myAnswer as myNumber doubled.

• Instead of writing output myAnswer, many pseudocode devel-


opers would write display myAnswer, print myAnswer, or
write myAnswer.

The point is, tfle pseudocode statements are instructions to retrieve


an original number from an input device and store it in memory
wflere it can be used in a calculation, and tflen to get tfle calculated
answer from memory and send it to an output device so a person
can see it. Wflen you eventually convert your pseudocode to a
specific programming language, you do not flave sucfl flexibility
because spe- cific syntax will be required. For example, if you use
tfle C# program- ming language and write tfle statement to output
tfle answer, you will code tfle following:
Console.Write (myAnswer);

The exact use of words, capitalization, and punctuation are important


in tfle C# statement, but not in tfle pseudocode statement.

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 Statements and Flowchart Symbols

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.

Aritflmetic operation statements set myAnswer =


are examples of processing. In a myNumber * 2
flowcflart, you use a rectangle as tfle
processing symbol tflat contains a
Figure 1-4 Processing symbol Because the
processing statement, as sflown in parallelogram
Figure 1-4. is used for
both input
To represent an output state- output myAnswer and output, it
ment, you use tfle same symbol as is often
for input statements—tfle output called
symbol is a parallelogram, as Figure 1-5 Output symbol the input/output
sflown in Figure 1-5. symbol or I/O
symbol.

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

CHAPTER 1 An Overview of Computers and Programming

Programmers To be complete, a flowcflart sflould include two more elements:


seldom create terminal symbols, or start/stop symbols, at eacfl end. Often, you
both pseudo-
place a word like start or begin in tfle first terminal symbol and a
code and a
flowchart for
word like end or stop in tfle otfler. The standard terminal symbol is
the same problem. You sflaped like a racetrack; many programmers refer to tflis sflape as a
usually use one or the lozenge, because it resembles tfle sflape of tfle medication you migflt
18
other. In a large use to sootfle a sore tflroat. Figure 1-6 sflows a complete flowcflart
program, you might for tfle program tflat doubles a number, and tfle pseudocode for tfle
even prefer to use same problem. You can see from tfle figure tflat tfle flowcflart and
pseudocode for some
pseudocode statements are tfle same—only tfle presentation format
parts and draw a
flowchart for others.
differs.

Flowchart Pseudocode

start

input myNumber

start
input myNumber
set myAnswer =
set myAnswer = myNumber * 2
myNumber * 2
output myAnswer
stop

output myAnswer

stop

Figure 1-6 Flowchart and pseudocode of program that doubles a number

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

Using Pseudocode Statements and Flowchart Symbols

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

A better solution is to flave tfle computer execute tfle same set of


tflree instructions over and over again, as sflown in Figure 1-8. The
repetition of a series of steps is called a loop. Witfl tflis approacfl, tfle
computer gets a number, doubles it, displays tfle answer, and tflen
starts over again witfl tfle first instruction. The same spot in memory,
called myNumber, is reused for tfle second number and for any subse-
quent numbers. The spot in memory named myAnswer is reused eacfl
time to store tfle result of tfle multiplication operation. The logic
illus- trated in tfle flowcflart in Figure 1-8 contains a major problem
—tfle sequence of instructions never ends. This programming
situation is known as an infinite loop—a repeating flow of logic witfl
no end. You will learn one way to flandle tflis problem later in tflis
cflapter; you will learn a superior way in Cflapter 3.

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 1 An Overview of Computers and Programming

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

Figure 1-8 Flowchart of infinite number-doubling program

TWO TRUTHS & A LIE


Using Pseudocode Statements and Flowchart Symbols

1. When you draw a flowchart, you use a parallelogram to represent an


input operation.
2. When you draw a flowchart, you use a parallelogram to represent a
process- ing operation.
3. When you draw a flowchart, you use a parallelogram to represent an
output operation.

represent
to a processing operation.
The false statement is #2. When you draw a flowchart, you use a rectangle

Using a Sentinel Value to


End a Program
The logic in tfle flowcflart for doubling numbers, sflown in Figure 1-8,
flas a major flaw—tfle program contains an infinite loop. If, for exam-
ple, tfle input numbers are being entered at tfle keyboard, tfle program
will keep accepting numbers and outputting doubles forever. Of course,
tfle user could refuse to type any more numbers. But tfle computer is
very patient, and if you refuse to give it any more numbers, it will sit
and wait forever. Wflen you finally type a number, tfle program will
double it, output tfle result, and wait for anotfler. The program cannot
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 a Sentinel Value to End a Program

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.

You represent a decision in a flowcflart by drawing a decision symbol,


wflicfl is sflaped like a diamond. The diamond usually contains a A yes-or-no
decision is
question, tfle answer to wflicfl is one of two mutually exclusive
called a
options—often yes or no. All good computer questions flave only binary deci-
two mutually exclusive answers, sucfl as yes and no or true and false. sion, because
For example, “Wflat day of tfle year is your birtflday?” is not a good there are two possible
computer question because tflere are 366 possible answers. However, outcomes.
“Is your birtflday June 24?” is a good computer question because, for
everyone in tfle world, tfle answer is eitfler yes or no.
The question to stop tfle doubling program sflould be “Is tfle value
of myNumber just entered equal to 0?” or “myNumber = 0?” for sflort.
The complete flowcflart will now look like tfle one sflown 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

Figure 1-9 Flowchart of number-doubling program with sentinel value of 0


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 1 An Overview of Computers and Programming

One drawback to using 0 to stop a program, of course, is tflat it won’t


work if tfle user does need to find tfle double of 0. In tflat case, some
otfler data-entry value tflat tfle user will never need, sucfl as 999 or –
1, could be selected to signal tflat tfle program sflould end. A
preselected value tflat stops tfle execution of a program is often called
a dummy value because it does not represent real data, but just a
22 signal to stop. Sometimes, sucfl a value is called a sentinel value
because it repre- sents an entry or exit point, like a sentinel wflo
guards a fortress.
Not all programs rely on user data entry from a keyboard; many
read data from an input device, sucfl as a disk. Wflen organizations
store data on a disk or otfler storage device, tfley do not commonly
use
a dummy value to signal tfle end of tfle file. For one tfling, an input
record migflt flave flundreds of fields, and if you store a dummy
record in every file, you are wasting a large quantity of storage on
“nondata.”
Additionally, it is often difficult to cfloose sentinel values for fields
in a company’s data files. Any balanceDue, even a zero or a negative
number, can be a legitimate value, and any customerName, even “ZZ”,
could be someone’s name. Fortunately, programming languages can
recognize tfle end of data in a file automatically, tflrougfl a code tflat
is stored at tfle end of tfle data. Many programming languages use
tfle term eof (for “end of file”) to refer to tflis marker tflat
automatically acts as a sentinel. This book, tflerefore, uses eof to
indicate tfle end of
data wflenever start
using a dummy value is impractical
Don’t Do It or inconvenient. In
tfle flowcflart sflown in Figure 1-10, tfleThis
eof logic is not
question is sfladed.
structured; you will
learn about structure
input myNumber in Chapter 3.

Yes
eof? stop

No

set myAnswer =
myNumber times 2

output myAnswer

Figure 1-10 Flowchart using eof

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 Programming and User Environments

TWO TRUTHS & A LIE


Using a Sentinel Value to End a Program

1. A program that contains an infinite loop is one that never ends.


23
2. A preselected value that stops the execution of a program is often called
a dummy value or a sentinel value.
3. Many programming languages use the term fe (for “file end”) to refer to
a marker that automatically acts as a sentinel.

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.

Understanding Programming Environments


Wflen you plan tfle logic for a computer program, you can use
paper and pencil to create a flowcflart, or you migflt use software
tflat allows you to manipulate flowcflart sflapes. If you cfloose to
write pseudocode, you can do so by fland or by using a word-
processing program. To enter tfle program into a computer so you
can trans- late and execute it, you usually use a keyboard to type
program statements into an editor. You can type a program into one
of tfle following:
• A plain text editor
• A text editor tflat is part of an integrated development
environment

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 1 An Overview of Computers and Programming

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.

This line contains a


prompt that tells the user
what to enter. You will
learn more about
prompts in Chapter 2.

Figure 1-11 A C# number-doubling program in Notepad

You can use tfle editor of an integrated development environment


(IDE) to enter your program. An IDE is a software package tflat pro-
vides an editor, compiler, and otfler programming tools. For example,
Figure 1-12 sflows a C# program in tfle Microsoft Visual Studio IDE,
an environment tflat contains tools useful for creating programs in
Visual Basic, C++, and C#.

Figure 1-12 A C# number-doubling program in Visual Studio

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 Programming and User Environments

Using an IDE is flelpful to programmers because IDEs usually provide


features similar to tflose you find in many word processors. In
particu- lar, an IDE’s editor commonly includes sucfl features as tfle
following:
• It uses different colors to display various language
components, making elements like data types easier to 25
identify.
• It fligflligflts syntax errors visually for you.
• It employs automatic statement completion; wflen you start to type
a statement, tfle IDE suggests a likely completion, wflicfl you can
accept witfl a keystroke.
• It provides tools tflat allow you to step tflrougfl a program’s execu-
tion one statement at a time so you can more easily follow tfle pro-
gram’s logic and determine tfle source of any errors.
Wflen you use tfle IDE to create and save a program, you occupy
mucfl more disk space tflan wflen using a plain text editor. For exam-
ple, tfle program in Figure 1-12 occupies more tflan 49,000 bytes of
disk space.
Altflougfl various programming environments migflt look different
and offer different features, tfle process of using tflem is very
similar. Wflen you plan tfle logic for a program using pseudocode
or a flow- cflart, it does not matter wflicfl programming
environment you will use to write your code, and wflen you write
tfle code in a program- ming language, it does not matter wflicfl
environment you use to write it.
Understanding User Environments
A user migflt execute a program you flave written in any number
of environments. For example, a user migflt execute tfle
number- doubling program from a command line like tfle one
sflown in
Figure 1-13. A command line is a location on your computer screen
at wflicfl you type text entries to communicate witfl tfle computer’s
operating system. In tfle program in Figure 1-13, tfle user is asked for
a number, and tfle results are displayed.

Figure 1-13 Executing a number-doubling program in a


command-line environment

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 1 An Overview of Computers and Programming

Many programs are not run at tfle command line in a text


environ- ment, but are run using a graphical user interface, or
GUI (pro- nounced “gooey”), wflicfl allows users to interact witfl a
program in a grapflical environment. Wflen running a GUI
program, tfle user migflt type input into a text box or use a mouse
or otfler pointing device to select options on tfle screen. Figure 1-
26
14 sflows a number- doubling program tflat performs exactly tfle
same task as tfle one in Figure 1-13, but tflis program uses a GUI.

Figure 1-14 Executing a number-doubling program


in a GUI environment

A command-line program and a GUI program migflt be written in


tfle same programming language. (For example, tfle programs sflown
in Figures 1-13 and 1-14 were botfl written using C#.) However, no
matter wflicfl environment is used to write or execute a program, tfle
logical process is tfle same. The two programs in Figures 1-13 and
1-14 botfl accept input, perform multiplication, and perform output.
In tflis book, you will not concentrate on wflicfl environment is used
to type a program’s statements, nor will you care about tfle type of
environment tfle user will see. Instead, you will be concerned witfl tfle
logic tflat applies to all programming situations.

TWO TRUTHS & A LIE


Understanding Programming and User Environments

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

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 1 An Overview of Computers and Programming

TWO TRUTHS & A LIE


Understanding the Evolution of Programming Models

1. The oldest computer programs were written in many separate modules.


28
2. Procedural programmers focus on actions that are carried out by a program.
3. Object-oriented programmers focus on a program’s objects and their attri-
butes and behaviors.
modules.
piece; programs are divided into
newer
The false statement is #1. The oldest programs were written in a single

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.

Application software comprises all tfle programs you apply


to a task.
System software comprises tfle programs tflat you use to manage
your computer.
Input describes tfle entry of data items into computer memory
using flardware devices sucfl as keyboards and mice.
Data items include all tfle text, numbers, and otfler information pro-
cessed by a computer.
Processing data items may involve organizing tflem, cflecking tflem
for accuracy, or performing matflematical operations on tflem.
The central processing unit, or CPU, is tfle flardware component
tflat processes data.
Output describes tfle operation of retrieving information from mem-
ory and sending it to a device, sucfl as a monitor or printer, so
people can view, interpret, and work witfl tfle results.
Storage devices are types of flardware equipment, sucfl as disks, tflat
flold information for later retrieval.
Programming languages, sucfl 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
Licensed to: iChapters User

CHAPTER 1 An Overview of Computers and Programming

Program code is tfle set of instructions a programmer writes in


a programming language.
Coding the program is tfle act of writing programming
language instructions.
The syntax of a language is its grammar rules.
30
Computer memory is tfle temporary, internal storage witflin a
computer.
Volatile describes storage wflose contents are lost wflen power
is lost.
Nonvolatile describes storage wflose contents are retained wflen
power is lost.
Random access memory (RAM) is temporary, internal computer
storage.
Machine language is a computer’s on/off circuitry language.
A compiler or interpreter translates a fligfl-level language into
macfline language and tells you if you flave used a
programming language incorrectly.
Binary language is represented using a series of 0s and 1s.
Source code is tfle statements a programmer writes in a program-
ming language.
Object code is translated macfline language.
To run or execute a program is to carry out its instructions.
Scripting languages (also called scripting programming lan-
guages or script languages) sucfl as Pytflon, Lua, Perl, and PHP
are used to write programs tflat are typed directly from a keyboard.
Scripting languages are stored as text ratfler tflan as binary execut-
able files.
A logical error occurs wflen incorrect instructions are performed,
wflen instructions are performed in tfle wrong order.
or
You develop tfle logic of tfle computer program wflen you give
instructions to tfle computer in a specific sequence, witflout omitting
any instructions or adding extraneous instructions.
A semantic error occurs wflen a correct word is used in an incorrect
context.
A variable is a named memory location wflose value can vary.
The program development cycle consists of tfle steps tflat occur
during a program’s lifetime.

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

CHAPTER 1 An Overview of Computers and Programming

A loop is a repetition of a series of steps.


An infinite loop occurs wflen repeating logic cannot end.
Making a decision is tfle act of testing a value.
A decision symbol is sflaped like a diamond and used to represent
32 decisions in flowcflarts.
A binary decision is a yes-or-no decision witfl two possible
outcomes.
A dummy value is a preselected value tflat stops tfle execution of a
program.
A sentinel value is a preselected value tflat stops tfle execution of
a program.
The term eof means “end of file.”
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.
An integrated development environment (IDE) is a software
package tflat provides an editor, compiler, and otfler programming
tools.
Microsoft Visual Studio IDE is a software package tflat contains
useful tools for creating programs in Visual Basic, C++, and C#.
A command line is a location on your computer screen at wflicfl
you type text entries to communicate witfl tfle computer’s operating
system.
A graphical user interface, or GUI (pronounced “gooey”),
allows users to interact witfl a program in a grapflical
environment.
Procedural programming is a programming model tflat focuses on
tfle procedures tflat programmers create.
Object-oriented programming is a programming model tflat focuses
Review
on objects, orQuestions
“tflings,” and describes tfleir features (or attributes) and
tfleir beflaviors.
1. Computer programs are also known as
.
a. flardware
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
Licensed to: iChapters User

Review Questions

2. The major computer operations include


.
a. flardware and software
b. input, processing, and output
c. sequence and looping 33

d. spreadsfleets, word processing, and data


communications

3. Visual Basic, C++, and Java are all examples


of computer
.
a. operating systems
b. flardware
c. macfline languages
d. programming languages

4. A programming language’s rules are its


.
a. syntax
b. logic
c. format
d. options

5. The most important task of a compiler or interpreter


is to .
a. create tfle rules for a programming language
b. translate Englisfl statements into a language tfle computer
language
can understand, sucfl as Java
d. execute macfline language programs to perform useful
c. translate programming language statements into macfline
tasks

6. Wflicfl of tfle following is temporary, internal storage?


a. CPU
b. flard disk
c. keyboard
d. memory

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 1 An Overview of Computers and Programming

7. Wflicfl of tfle following pairs of steps in tfle programming


process is in tfle correct order?
a. code tfle program, plan tfle logic
b. test tfle program, translate it into macfline language
34 c. put tfle program into production, understand tfle
problem
d. code tfle program, translate it into macfline
language

8. The programmer’s most important task before planning tfle


logic of a program is to .
a. decide wflicfl programming language to use
b. code tfle problem
c. train tfle users of tfle program
d. understand tfle problem

9. The two most commonly used tools for planning a program’s


logic are .
a. flowcflarts and pseudocode
b. ASCII and EBCDIC
c. Java and Visual Basic
d. word processors and spreadsfleets

10. Writing a program in a language sucfl as C++ or Java is


known as tfle program.
a. translating
b. coding
c. interpreting
d. compiling

11. An Englisfl-like programming language sucfl as Java or Visual


Basic is a programming language.
a. macfline-level
b. low-level
c. fligfl-level
d. binary-level

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

12. Wflicfl of tfle following is an example of a syntax error?


a. producing output before accepting input
b. subtracting wflen you meant to add
c. misspelling a programming language word
35
d. all of tfle above

13. Wflicfl of tfle following is an example of a logical error?


a. performing aritflmetic witfl a value before
inputting it
b. accepting two input values wflen a program requires
only one
c. dividing by 3 wflen you meant to divide by 30
d. all of tfle above

14. The parallelogram is tfle flowcflart symbol representing


.
a. input
b. output
c. botfl a and b
d. none of tfle above

15. In a flowcflart, a rectangle represents


.
a. input
b. a sentinel
c. a question
d. processing
16. In flowcflarts, tfle decision symbol is a
.
a. parallelogram
b. rectangle
c. lozenge
d. diamond

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 1 An Overview of Computers and Programming

17. The term “eof” represents .


a. a standard input device
b. a generic sentinel value
c. a condition in wflicfl no more memory is available for
36 storage
d. tfle logical flow in a program

18. Wflen you use an IDE instead of a simple text editor to


develop a program, .
a. tfle logic is more complicated
b. tfle logic is simpler
c. tfle syntax is different
d. some flelp is provided

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

20. As compared to procedural programming, witfl object-


oriented programming .
a. tfle programmer’s focus differs
b. you cannot use some languages, sucfl as Java
c. you do not accept input
d. you do not code calculations; tfley are created
automatically

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

2. In your own words, describe tfle steps to writing a


computer program.

3. Matcfl tfle term witfl tfle appropriate sflape.

1. Input A.

2. Processing B.

3. Output C.

4. Decision D.

5. E.
Terminal

4. Draw a flowcflart or write pseudocode to represent tfle logic


of a program tflat allows tfle user to enter a value. The pro-
gram multiplies tfle value by 10 and outputs tfle result.

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 1 An Overview of Computers and Programming

5. Draw a flowcflart or write pseudocode to represent tfle logic


of a program tflat allows tfle user to enter a value for tfle
radius of a circle. The program calculates tfle diameter by
multiplying tfle radius by 2, and tflen calculates tfle circumfer-
ence by multiplying tfle diameter by 3.14. The program out-
puts botfl tfle diameter and tfle circumference.
38

6. Draw a flowcflart or write pseudocode to represent tfle logic


of a program tflat allows tfle user to enter two values. The
program outputs tfle sum of tfle two values.

7. Draw a flowcflart or write pseudocode to represent tfle logic


of a program tflat allows tfle user to enter tflree values. The
values represent flourly pay rate, tfle number of flours worked
tflis pay period, and percentage of gross salary tflat is witfl-
fleld. The program multiplies tfle flourly pay rate by tfle num-
ber of flours worked, giving tfle gross pay. Then, it multiplies
tfle gross pay by tfle witflflolding percentage, giving tfle witfl-
flolding amount. Finally, it subtracts tfle witflflolding amount
from tfle gross pay, giving tfle net pay after taxes. The pro-
gram outputs tfle net pay.

Find the Bugs

8. Since tfle early days of computer programming, program


errors flave been called “bugs.” The term is often said to
flave originated from an actual motfl tflat was discovered
trapped in tfle circuitry of a computer at Harvard
University in 1945. Actually, tfle term “bug” was in use prior
to 1945 to mean trouble witfl any electrical apparatus; even
during Thomas Edison’s life, it meant an “industrial defect.”
However, tfle term “debugging” is more closely associated
witfl correcting program syntax and logic errors tflan witfl
any otfler type of trouble.

Your student disk contains files named DEBUG01-01.txt,


DEBUG01-02.txt, and DEBUG01-03.txt. Eacfl file starts witfl
some comments tflat describe tfle problem. Comments are
lines tflat begin witfl two slasfles (//). Following tfle comments,
eacfl file contains pseudocode tflat flas one or more bugs you
must find and correct.

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

You might also like