# Programming Logic and Design Fifth Edition, Comprehensive - PowerPoint PPT Presentation

1 / 50

Programming Logic and Design Fifth Edition, Comprehensive Chapter 2 Understanding Structure Objectives Learn about the features of unstructured spaghetti code Understand the three basic structures: sequence, selection, and loop Use a priming read Appreciate the need for structure

## Related searches for Programming Logic and Design Fifth Edition, Comprehensive

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Programming Logic and Design Fifth Edition, Comprehensive

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

## Programming Logic and DesignFifth Edition, Comprehensive

Chapter 2

Understanding Structure

### Objectives

• Learn about the features of unstructured spaghetti code

• Understand the three basic structures: sequence, selection, and loop

• Appreciate the need for structure

• Recognize structure

• Learn about three special structures: case, do-while, and do-until

Programming Logic and Design, Fifth Edition, Comprehensive

### Understanding Unstructured Spaghetti Code

• Spaghetticode: logically snarled program statements

• Can be the result of poor program design

• Spaghetti code programs often work, but are difficult to read and maintain

• Convoluted logic usually requires more code

Programming Logic and Design, Fifth Edition, Comprehensive

### Understanding Unstructured Spaghetti Code (continued)

• Admit students who score >= 90 on admissions test if upper 75 percent of high-school graduating class

• Admit students who score >= 80 on test if upper 50 percent of high-school graduating class

• Admit students who score >= 70 on admission test if upper 25 percent of high-school graduating class

Programming Logic and Design, Fifth Edition, Comprehensive

### Understanding Unstructured Spaghetti Code (continued)

Figure 2-2 Spaghetti code example

Programming Logic and Design, Fifth Edition, Comprehensive

### Understanding the Three Basic Structures

• Structure: basic unit of programming logic

• Any program can be constructed from only three basic types of structures

• Sequence

• Perform actions in order

• No branching or skipping any task

• Selection (decision)

• Ask a question, take one of two actions

• Dual-alternative or single-alternative

• Loop

• Repeat actions based on answer to a question

Programming Logic and Design, Fifth Edition, Comprehensive

### Understanding the Three Basic Structures (continued)

• Sequence structure

Figure 2-3 Sequence structure

Programming Logic and Design, Fifth Edition, Comprehensive

### Understanding the Three Basic Structures (continued)

• Selection structure

Figure 2-4 Selection structure

Programming Logic and Design, Fifth Edition, Comprehensive

### Understanding the Three Basic Structures (continued)

• Dual-alternative if: contains two alternatives

if someCondition is true then

do oneProcess

else

do theOtherProcess

Programming Logic and Design, Fifth Edition, Comprehensive

### Understanding the Three Basic Structures (continued)

• Single-alternative if: contains one alternative

Figure 2-5 Single-alternative selection structure

Programming Logic and Design, Fifth Edition, Comprehensive

### Understanding the Three Basic Structures (continued)

• Single-alternative if

• Else clause is not required

• Null case: situation where nothing is done

if employee belongs to dentalPlan then

deduct \$40 from employeeGrossPay

Programming Logic and Design, Fifth Edition, Comprehensive

### Understanding the Three Basic Structures (continued)

• Loop structure

• Repeats a set of actions based on the answer to a question

• Also called repetition or iteration

• Question is asked first in the most common form of loop

Figure 2-6 Loop structure

Programming Logic and Design, Fifth Edition, Comprehensive

### Understanding the Three Basic Structures (continued)

• Loop structure

while testCondition continues to be true

do someProcess

while quantityInInventory remains low

continue to orderItems

Programming Logic and Design, Fifth Edition, Comprehensive

### Understanding the Three Basic Structures (continued)

• All logic problems can be solved using only these three structures

• Structures can be combined in an infinite number of ways

• Stacking: attaching structures end-to-end

• End-structure statements: indicate the end of a structure

• Theendifstatement ends an if-then-else structure

• Theendwhile ends a loop structure

Programming Logic and Design, Fifth Edition, Comprehensive

### Understanding the Three Basic Structures (continued)

Figure 2-7 Structured flowchart and pseudocode

Programming Logic and Design, Fifth Edition, Comprehensive

### Understanding the Three Basic Structures (continued)

• Any individual task or step in a structure can be replaced by a structure

• Nesting: placing one structure within another

• Indent the nested structure’s statements

• Block: group of statements that execute as a single unit

Programming Logic and Design, Fifth Edition, Comprehensive

### Understanding the Three Basic Structures (continued)

Figure 2-8 Flowchart and pseudocode showing a sequence nested within a selection

Programming Logic and Design, Fifth Edition, Comprehensive

### Understanding the Three Basic Structures (continued)

Figure 2-9 Selection in a sequence within a selection

Programming Logic and Design, Fifth Edition, Comprehensive

### Understanding the Three Basic Structures (continued)

Figure 2-10 Flowchart and pseudocode for loop within selection within sequence within selection

Programming Logic and Design, Fifth Edition, Comprehensive

### Understanding the Three Basic Structures (continued)

• Each structure has one entry and one exit point

• Structures attach to others only at entry or exit points

Figure 2-11 The three structures

Programming Logic and Design, Fifth Edition, Comprehensive

• Priming read (or priming input):

• Reads the first input data record

• Outside the loop that reads the rest of the records

• Helps keep the program structured

• Analyze a flowchart for structure one step at a time

• Watch for unstructured loops that do not follow this order:

• Take action based on the answer

Programming Logic and Design, Fifth Edition, Comprehensive

### Using the Priming Read (continued)

• Unstructured loop

Figure 2-12 Unstructured flowchart of a number-doubling program

Programming Logic and Design, Fifth Edition, Comprehensive

### Using the Priming Read (continued)

• Structured but nonfunctional loop

Figure 2-15 Structured, but nonfunctional, flowchart of number-doubling problem

Programming Logic and Design, Fifth Edition, Comprehensive

### Using the Priming Read (continued)

• Functional but nonstructured loop

Figure 2-16 Functional, but nonstructured, flowchart

Programming Logic and Design, Fifth Edition, Comprehensive

### Using the Priming Read (continued)

• Functional and structured loop

Figure 2-17 Functional, structured flowchart and pseudocode for the number-doubling problem

Programming Logic and Design, Fifth Edition, Comprehensive

### Using the Priming Read (continued)

• Priming read sets up the process so the loop can be structured

• To analyze a flowchart’s structure, try writing pseudocode for it

start

get inputNumber

while not eof

get inputNumber

endwhile

stop

Programming Logic and Design, Fifth Edition, Comprehensive

### Using the Priming Read (continued)

• What is wrong with this design?

Figure 2-18 Structured but incorrect solution to the number-doubling problem

Programming Logic and Design, Fifth Edition, Comprehensive

### Understanding the Reasons for Structure

• Provides clarity

• Professionalism

• Efficiency

• Ease of maintenance

• Supports modularity

Programming Logic and Design, Fifth Edition, Comprehensive

### Recognizing Structure

• Any set of instructions can be expressed in structured format

• Any task to which you can apply rules can be expressed logically using sequence, selection, loop

• It can be difficult to detect whether a flowchart is structured

Programming Logic and Design, Fifth Edition, Comprehensive

Recognizing Structure (continued)

• Is this flowchart structured?

Figure 2-22 Example 2

Programming Logic and Design, Fifth Edition, Comprehensive

### Recognizing Structure (continued)

• Is this flowchart structured?

Figure 2-23 Example 3

Programming Logic and Design, Fifth Edition, Comprehensive

### Recognizing Structure (continued)

• Single process like A is part of an unacceptable structure

• At least the beginning of a sequence structure

Figure 2-24 Untangling Example 3, first step

Programming Logic and Design, Fifth Edition, Comprehensive

### Recognizing Structure (continued)

• B begins a selection structure

• Sequences never have decisions in them

• Logic never returns to B

Figure 2-25 Untangling Example 3, second step

Programming Logic and Design, Fifth Edition, Comprehensive

### Recognizing Structure (continued)

• Pull up on the flowline from the left side of B

Figure 2-26 Untangling Example 3, third step

Programming Logic and Design, Fifth Edition, Comprehensive

### Recognizing Structure (continued)

• Next, pull up the flowline on the right side of B

Figure 2-27 Untangling Example 3, fourth step

Programming Logic and Design, Fifth Edition, Comprehensive

### Recognizing Structure (continued)

• Pull up the flowline on the left side of D and untangle it from the B selection by repeating C

Figure 2-28 Untangling Example 3, fifth step

Programming Logic and Design, Fifth Edition, Comprehensive

### Recognizing Structure (continued)

• Now pull up the flowline on the right side of D

Figure 2-29 Untangling Example 3, sixth step

Programming Logic and Design, Fifth Edition, Comprehensive

### Recognizing Structure (continued)

• Bring together the loose ends of D and of B

Figure 2-30 Finished flowchart and pseudocode for untangling Example 3

Programming Logic and Design, Fifth Edition, Comprehensive

### Three Special Structures – CASE, DO-WHILE, and DO-UNTIL

• Many languages allow three additional structures:

• Thecase structure

• The do-while structure

• The do-until structure

• CASE Structure:

• Decisions with more than two alternatives

• Tests a variable against a series of values and takes action based on a match

• Nested if-then-else statements will do what a case structure does

Programming Logic and Design, Fifth Edition, Comprehensive

### Three Special Structures – Case, DoWhile, and DoUntil (continued)

• Using nested if-then-else for multiple alternatives

Figure 2-31 Flowchart and pseudocode of tuition decisions

Programming Logic and Design, Fifth Edition, Comprehensive

### Three Special Structures – Case, DoWhile, and DoUntil (continued)

• Using a case structure for multiple alternatives

Figure 2-32 Flowchart and pseudocode of case structure

Programming Logic and Design, Fifth Edition, Comprehensive

### Three Special Structures – Case, DoWhile, and DoUntil (continued)

• do-while and do-until loops

• Question is asked at the end of the loop structure

• Loop statements always used at least once

Figure 2-33 The while loop, which is a pretest loop

Figure 2-34 Structure of a do-while or do-until loop, which are posttest loops

Programming Logic and Design, Fifth Edition, Comprehensive

### Three Special Structures – Case, DoWhile, and DoUntil (continued)

• do-while: executes as long as the question’s answer is Yes or True

• do-until: executes as long as the question’s answer is No or False

do

wash a dish

until all dishes are washed

do

wash a dish

while more dishes remain to be washed

Programming Logic and Design, Fifth Edition, Comprehensive

### Three Special Structures – Case, DoWhile, and DoUntil (continued)

• while loop with question at beginning is called a pretest loop

• do-while and do-until with question at end are called posttest loops

• Posttest loop can be replaced with a sequence followed by a pretest while loop

pay a bill

while there are more bills to pay

pay a bill

endwhile

Programming Logic and Design, Fifth Edition, Comprehensive

### Three Special Structures – Case, DoWhile, and DoUntil (continued)

Figure 2-35 Flowchart and pseudocode for do-while loop

Programming Logic and Design, Fifth Edition, Comprehensive

### Three Special Structures – Case, DoWhile, and DoUntil (continued)

Figure 2-36 Flowchart and pseudocode for sequence followed by while loop

Programming Logic and Design, Fifth Edition, Comprehensive

### Three Special Structures – Case, DoWhile, and DoUntil (continued)

• How can this design be made structured?

Figure 2-37 Unstructured loop

Programming Logic and Design, Fifth Edition, Comprehensive

### Three Special Structures – Case, DoWhile, and DoUntil (continued)

• Repeat the needed step to enforce structure

Figure 2-38 Sequence and structured loop that accomplish the same tasks as Figure 2-37

Programming Logic and Design, Fifth Edition, Comprehensive

### Summary

• Spaghetti code: snarled program logic

• Three basic structures: sequence, selection, loop

• Combined by stacking and nesting

• Structured techniques promote clarity, professionalism, efficiency, and modularity

• Flowchart can be made structured by untangling

Programming Logic and Design, Fifth Edition, Comprehensive

### Summary (continued)

• case structure: questions with multiple alternatives

• while loop: a pretest loop asks the question first

• while loop statements never execute if the answer is No

• do-while and do-until loops: posttest loops that ask the question last

• do-while and do-until loop statements are always executed at least once

• Posttest loop can be replaced by a sequence followed by a while loop

Programming Logic and Design, Fifth Edition, Comprehensive