P. 1


|Views: 8|Likes:
Published by ALi

More info:

Published by: ALi on Jan 08, 2013
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





Read this assignment and then read pages 43–84 in your
textbook. When you’re comfortable with the material
presented in the text, take the self-check to gauge your
progress. If there are any areas you don’t feel comfortable
with, reread those sections in the textbook before returning
to the study guide. When you feel you’ve mastered the
material in Chapter 2, move on to the next assignment.

Program code can become quite complicated. When a collection
of program statements becomes difficult to read, it’s called
spaghetti code, because it’s like following a noodle in a bowl
of spaghetti. Therefore, it’s best to keep your code simple
by using structures. The three basic programming structures
are sequences(Figure 2), selections, and loops. Youcan stack
structures on each other at entrance or exit points. Putting
one structure within another is called nesting.

FIGURE 2—A Sequence

Introduction to Programming


To make your program workable and structured, you may

have to add an extra step, called a priming reador a priming

input.You would add a step for getting the input number

before beginning a structured loop. Looping involves asking

a question, and if you perform an action based on the

answer to that question, you then ask the question again.

In a structured loop
1.You ask a question.
2.You perform a procedure, if the answer dictates it.
3.If you performed a procedure, you go back and ask
the question again.
You may be wondering why you have to structure your
code. It’s important to follow the rules for structuring
your program for the following reasons:

■It keeps the code clear.

■It’s the way professional programmers write code.

■It’s more efficient.

■It’s easier to maintain.

■It can be broken down into modular pieces.

If you come across code that isn’t structured, you can
structure it. Again, think of a bowl of spaghetti. Your job
is to follow a noodle through the bowl and disentangle it
from the other noodles. Essentially, you’re separating the
noodles and rearranging them.
Three special structures we’ll discuss are case, do until, and

do while.You might use a case structure if there are several

possible values for a variable. For instance, a program fee
might be based on a sliding scale according to income. In
the do until loop,a procedure is performed at least once.
This differs from the do while loop,where a question is
asked and an action is performed only if a condition is

met. (In a do until loop, a question is asked and an action

is performed untila condition is met.)
Read the Chapter Summary on page 74 and then review
the Key Terms on pages 74–75. Answer the Review Questions
on pages 75–78 and work through “Find the Bugs” on page 78.
Then complete Exercises 1, 2, and 3 presented on pages 79–82,

Lesson 1


and find the answers to the “Detective Work” questions on
page 84. When you feel you’ve mastered the material in this
assignment, complete Self-Check 2. Answers to the self-check
questions are provided at the end of this study guide.
Answers to the Review Questions, “Find the Bugs,”
Exercises, and “Detective Work” are on the Web.

Self-Check 2

1–4: Indicate whether the following statements are True or False.

______1.Structured code is like a bowl of spaghetti.

______2.A priming read is an additional input step within a program to ensure that it
functions correctly.

______3.A do-while loop continues to execute as long as the answer to the controlling
question is no.

______4.In a structured program, structures can’t be nested within other structures.

5.What are the three basic types of structures?




6.Placing one structure within another one is called _______.

7.What type of structure requires that you ask a question, and if you perform an action,
ask the question again?



8.What would you consider using when there are several possible values for a variable?


Check your answers with those on page 73.

Introduction to Programming


You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->