Programming Logic and Design, Third Edition Comprehensive 2-1

Chapter 2

Understanding Structure

At a Glance
Instructor’s Notes

Chapter Overview

Chapter Objectives

Technical Notes

Lecture Notes

Quick Quizzes

Discussion Questions

Additional Activities

Instructor’s Notes
Chapter Overview

In this chapter, students will learn the features of unstructured spaghetti code. They will learn the three basic structures of sequence, selection, and loop. Also, students will learn the use of a priming read. They will learn to appreciate the need for structure and recognize structure. Finally, students will learn about two special structurescase and do until.

Chapter Objectives

After studying Chapter 2, students should be able to:

Describe the features of unstructured spaghetti code

Describe the three basic structures of sequence, selection, and loop

Use a priming read

Appreciate the need for structure

Recognize structure

Describe two special structurescase and do until

Technical Notes

In order to best present the material in this chapter to students, I recommend hands-on activities. Create one activity that can be broken off into sections and assign a section to each group. Then have them come together and put a final product together. By splitting your class into groups, each student can learn from the experience of their classmates. Ideally, each student would have individual access to a PC during lectures, so they may utilize different programming techniques. However, it has been my experience that when teaching any sort of subject matter, it is a good idea to demonstrate the concepts and then ask students to work individually in different lab settings.

This chapter should not be completed in one class session. I would recommend splitting it into at least two class sessions if possible. The amount of subject matter to be covered, can be covered in anywhere between a 3-6 hour period. Plus any at-home exercises you wish to assign. It is recommended that you have couple of programming languages at your disposal for comparison purposes. Also, Internet connectivity would be an advantage.

Lecture Notes
Understanding Unstructured Spaghetti Code

The popular name for snarled program statements is spaghetti code. The reason for the name should be obviousthe code is as confusing to read as following one noodle through a plate of spaghetti.

Understanding the Three Basic Structures

A structure is a basic unit of programming logic; each structure is a sequent, selection, or loop. The first of these structures is a sequence, as shown in Figure 2-3 on page 38 of the text. With a sequence structure, you perform an action or event, and then you perform the next action, in order. The second structure is called a selection structure or decision structure, as shown in Figure 2-4 on page 38 of the text. With this structure, you ask a question, and, depending on the answer, you take one of two courses of action. Then, no matter which path you follow, you continue with the next event. Some people call the selection structure an if-then-else because it fits the statement illustrated on page 38 at the bottom. The third structure, shown in Figure 2-6 on page 39 of the text, is a loop. In a loop structure, you ask a question; if the answer requires an action, you perform the action and ask the original question again. If the answer requires that the action be taken again, you take the action and then ask the original question again. This continues until the answer to the question is such that the action is no longer required; then you exit the structure. You may hear programmers refer to looping as repetition or iteration.

All logic problems can be solved using only these three structuressequence, selection, and looping. The three structures, of course, can be combined in an infinite number of ways. Attaching structures end-to-end is called stacking structures. Figure 2-7 on page 41 of the text shows a structured flowchart achieved by stacking structures, and also shows pseudocode that might follow that flowchart logic. You can have a sequence of three steps on one side of a selection, as shown in Figure 2-8 on page 42 of the text. Placing a structure within another structure is called nesting the structures.

Using the Priming Read

A priming read or priming input is the first read or data input statement in a program. If a program will read 100 data records, you read the first data record in a statement that is separate from the other 99. You must do this to keep the program structured.

With a selection structure, the logic goes in one of two directions after the question, and then the flow comes back together; the question is not asked a second time. However, in a loop, if the answer to the question results in the loop being entered and the loop statements executing, then the logic returns to the question that started the loop; when the body of a loop executes, the question that controls the loop is always asked again.

In the doubling problem in the original Figure 2-12 on page 45 of the text, if it is not eof (that is, if the end-of-file condition is not met), some math is done, an answer is printed, a new number is obtained, and the eof question is asked again, in other words, while the answer to the eof question continues to be no, eventually the logic will return to the eof question.

Quick Reference / Discuss alternative programming solutions for the number doubling program.

Quick Quiz

  1. A(n) ______is a basic unit of programming logic. ANSWER: structure
  1. Some people call the selection structure a(n) ______. ANSWER: if-then-else
  1. Attaching structures end-to-end is called ______structures. ANSWER: stacking
  1. A priming read or priming input is the ______read or data input statement in a program. ANSWER: first
  1. The last step executed within the loop alters the condition tested in the question that begins the loop, which in this case is the ______question. ANSWER: end-of-file or eof

Understanding the Reasons for Structure

Until you have some programming experience, it is difficult to appreciate the reasons for using only the three structuressequence, selection, and loop. However, staying with these three structures is better for the following reasons:

Clarity  Professionalism

Efficiency  Maintenance Modularity

Quick Reference / Discuss the importance of program structure and refer to Figure 2-19 on page 51 of the text.

Recognizing Structure

Any set of instructions can be expressed in a structured format. If you can teach someone how to perform any ordinary activity, then you can express it in a structured way. Figure 2-20 on page 53 of the text shows a fairly complicated set of statements. Its purpose is not to teach you how to play a game, but rather to convince you that any task to which you can apply rules can be expressed logically using only combinations of sequence, selection, and looping.

Two Special StructuresCase and Do Until

Many programming languages allow two more structures: the case structure and the do until loop. These structures are never needed to solve any problem. However, sometimes these two additional structuresthe case and do untilare convenient. Programmers consider them both to be acceptable, legal structures.

The Case Structure

You can use the case structure when there are several distinct possible values for a single variable you are testing, and each value requires a different course of action.

Quick Reference / Discuss the use of case structures as illustrated in Figures 2-31 and 2-32 on pages 59 and 60 of the text.

Even though the program segments in Figure 2-31 on page 59 of the text are correct and structured, many programming languages permit using a case structure, as shown in Figure 2-32 on page 60 of the text.

The Do Until Loop

Recall that a structured loop (often called a do while) looks like Figure 2-33 on page 61 of the text. A special case loop called a do until loop looks like Figure 2-34 on page 61 of the text. In a do while loop, you ask a question and, depending on the answer, you might or might not enter the loop to execute the loop’s procedure. Conversely, in a do until loop, you ensure that the procedure executes at least once, then, depending on the answer to the controlling question, the loop may or may not execute additional times.

Because programmers understand that a do until can be expressed with a sequence followed by a do while, most languages allow the do until. Again, you are never required to use a do until; you can always accomplish the same events with a sequence followed by a do while. Figure 2-37 on page 63 of the text shows an unstructured loop. It is neither a do while loop (that begins with a decision, and after an action, returns to the decision), nor a do until loop (that begins with an action and ends with a decision that might repeat the action). Figure 2-38 on page 63 of the text shows the same logic as Figure 2-37, but now it is structured logic with a sequence of two actions occurring within the loop.

Quick Quiz

  1. Name three reasons for staying with the three structures: selection, sequence, and loop. ANSWER: Clarity, Professionalism, Efficiency, Maintenance, and Modularity
  1. The ______nature of structured programs means that work can be divided among many programmers. ANSWER: modular
  1. Any set of instructions can be expressed in a(n) ______format. ANSWER: structured
  1. You can use the ______structure when there are several distinct possible values for a single variable you are testing, and each value requires a different course of action. ANSWER: case
  1. In a(n) ______loop, you ensure that the procedure executes at least once. ANSWER: do until

Discussion Questions

  1. Discuss the significance of the priming read in a program.
  1. Discuss the importance of looping structures in program development.

Additional Activities

1.Provide students with some examples of spaghetti code and have them correct the programming structure.

2.Provide students with some sample programs that do not contain any looping structure, and have them redesign the program to include a looping structure.

Solutions to Exercises can be found within the Instructor’s Resource Kit (CD-ROM) that accompanies this text or at the following link:

1