MIS410 2
Computing with Business Applications
1: Programming Logic and Design (comprehensive); by Joyce Farrell, Ninth Edition, Cengage Learning.
2: VBA for Modelers: Developing Decision Support Systems with Microsoft Office Excel; by Albright 5th edition, South Western Cengage Learning.
Chapter 3
Structured Programming
1: Programming Logic and Design (comprehensive); by Joyce Farrell, Ninth Edition, Cengage Learning.
Pages: 87-123
Objectives
In this chapter, you will learn about:
• The disadvantages of unstructured spaghetti code
• The three basic structures—sequence, selection, and loop
• Using a priming input to structure a program
• The need for structure
• Recognizing structure
• Structuring and modularizing unstructured logic
3© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
The Disadvantages of Unstructured Spaghetti Code • Spaghetti code
• Logically snarled program statements
• Often a complicated mess
• Programs often work but are difficult to read and maintain
• Confusing and prone to error
• Unstructured programs • Do not follow the rules of structured logic
• Structured programs • Follow the rules of structured logic
4© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
5
Figure 3-1 Spaghetti code logic for washing a dog
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding the Three Basic Structures
• Structure • Basic unit of programming logic
• Sequence structure • Perform actions in order
• No branching or skipping any task
• Selection structure (decision structure) • Ask a question, take one of two actions
• Dual-alternative ifs or single-alternative ifs
• Loop structure • Repeat actions while a condition remains true
6© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding the Three Basic Structures (continued)
7
Figure 3-2 Sequence structure
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding the Three Basic Structures (continued)
8
Figure 3-3 Selection structure
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding the Three Basic Structures (continued) • Dual-alternative ifs
• Contain two alternatives
• The if-then-else structure
9
if someCondition is true then
do oneProcess
else
do theOtherProcess
endif
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding the Three Basic Structures (continued) • Single-alternative ifs
• An else clause is not required
• null case • Situation where nothing is done
10
if employee belongs to dentalPlan then
deduct $40 from employeeGrossPay
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding the Three Basic Structures (continued)
11
Figure 3-4 Single-alternative selection structure
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding the Three Basic Structures (continued) • Loop structure
• Repeats a set of actions while a condition remains true • Loop body
• Also called repetition or iteration
• Condition is tested first in the most common form of loop
• The while…do or while loop
12© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding the Three Basic Structures (continued)
13
Figure 3-5 Loop structure
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding the Three Basic Structures (continued) • Loop structure
14
while testCondition continues to be true
do someProcess
while you continue to be hungry
take another bite of food
determine if you still feel hungry
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding the Three Basic Structures (continued) • All logic problems can be solved using only sequence, selection, and
loop
• Structures can be combined in an infinite number of ways
• Stacking structures – Attaching structures end-to-end
• End-structure statement – Indicates the end of a structure
– The endif statement ends an if-then-else structure
– The endwhile statement ends a loop structure
15© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding the Three Basic Structures (continued)
16
Figure 3-6 Structured flowchart and pseudocode with three stacked structures
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding the Three Basic Structures (continued) • Any individual task or step in a structure can be replaced by a
structure
• Nesting structures • Placing one structure within another
• Indent the nested structure’s statements
• Block • A group of statements that execute as a single unit
17© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding the Three Basic Structures (continued)
18
Figure 3-7 Flowchart and pseudocode showing nested structures— a sequence nested within a selection
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding the Three Basic Structures (continued)
19
Figure 3-8 Flowchart and pseudocode showing nested structures— a loop nested within a sequence, nested within a selection
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding the Three Basic Structures (continued)
20
Figure 3-9 Flowchart and pseudocode for a selection within a loop within a sequence within a selection
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding the Three Basic Structures (continued) • Structured programs have the following characteristics:
• Include only combinations of the three basic structures
• Each structure has a single entry point and a single exit point
• Structures can be stacked or connected to one another only at their entry or exit points
• Any structure can be nested within another structure
21© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Using a Priming Input to Structure a Program • Priming input (or priming read)
• Reads the first input data record
• Is 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 • First ask a question
• Take action based on the answer
• Return to ask the question again
22© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Using a Priming Input to Structure a Program (continued)
23
Figure 3-15 Structured, but nonfunctional, flowchart of number-doubling problem
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Using a Priming Input to Structure a Program (continued)
24
Figure 3-16 Functional but unstructured flowchart
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Slide 21: loop should start by asking a question. Input should be primed before loop.
25
Figure 3-17 Functional, structured flowchart for the number-doubling problem
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
26
Figure 3-18 Structured but incorrect solution to the number-doubling problem
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding the Reasons for Structure
• Clarity—unstructured programs are confusing
• Professionalism—other programmers expect it
• Efficiency—most languages support it
• Ease of maintenance—other programmers find it easier to read
• Supports modularity—easily broken down into modules
• It can be difficult to detect whether a flowchart is structured
27© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
28
Figure 3-20 Example 2
A Structured Flowchart
Recognizing Structure
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Recognizing Structure (continued)
29
Figure 3-21 Example 3
An Unstructured Flowchart
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
30
Figure 3-23 Structured dog-washing flowchart and pseudocode
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
31
Figure 3-24 Modularized version of the dog-washing program
© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from
the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Summary
• Spaghetti code • Statements that do not follow rules of structured logic
• Three basic structures • Sequence, selection, and loop
• Combined by stacking and nesting
• Priming input • Statement that reads the first input value prior to starting a structured loop
32© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Summary (continued)
• Structured techniques promote: • Clarity
• Professionalism
• Efficiency
• Modularity
• Flowcharts can be made structured by untangling
• Logical steps can be rewritten to conform to the three structures
33© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.