Programming Logic and Design Fourth Edition, Comprehensive - PowerPoint PPT Presentation

1 / 54
About This Presentation
Title:

Programming Logic and Design Fourth Edition, Comprehensive

Description:

Example: college admissions criteria. Programming Logic and Design, Introductory, Fourth Edition ... Priming read sets up the process so the loop can be structured ... – PowerPoint PPT presentation

Number of Views:98
Avg rating:3.0/5.0
Slides: 55
Provided by: Cerr4
Category:

less

Transcript and Presenter's Notes

Title: Programming Logic and Design Fourth Edition, Comprehensive


1
Programming Logic and Design Fourth Edition,
Comprehensive
  • Chapter 2
  • Understanding Structure

2
Objectives
  • Describe the features of unstructured spaghetti
    code
  • Describe the three basic structures sequence,
    selection, and loop
  • Use a priming read
  • Appreciate the need for structure
  • Recognize structure

3
Objectives (continued)
  • Describe three special structures
  • case special form of selection structure
  • do-while post-test loop
  • do-until post-test loop

4
Understanding Unstructured Spaghetti Code
  • Spaghetti code logically snarled program
    statements
  • Can be the result of poor program design
  • Example college admissions criteria

5
Understanding Unstructured Spaghetti Code
(continued)
6
Understanding Unstructured Spaghetti Code
(continued)
  • Spaghetti code programs often work, but are
    difficult to read and maintain
  • Convoluted logic usually requires more code

7
Understanding the Three Basic Structures
  • Structure a basic unit of programming logic
  • Any program can be constructed from only three
    basic types of structures
  • Sequence
  • Selection
  • Loop ( aka repetition or iteration )

8
Understanding the Three Basic Structures
(continued)
  • Sequence structure
  • A set of instructions, performed sequentially
    with no branching

9
Understanding the Three Basic Structures
(continued)
  • Selection structure
  • Asks a question, then takes one of two possible
    courses of action based on the answer
  • Also called a decision structure or an
    if-then-else

10
Understanding the Three Basic Structures
(continued)
  • Dual-alternative if contains two
    alternativesif hoursWorked is more than 40
    then
  • calculate regularPay and overtimePay
  • else
  • calculate regularPay

11
Understanding the Three Basic Structures
(continued)
  • Single-alternative if contains one alternative

12
Understanding the Three Basic Structures
(continued)
  • Single-alternative if
  • if employee in dental plan then
  • subtract 40 from employee gross pay
  • Else clause is not required
  • Null case situation where nothing is done

13
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

14
Understanding the Three Basic Structures
(continued)
  • Loop structure

15
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
  • endif ends an if-then-else structure
  • endwhile ends a loop structure

16
Understanding the Three Basic Structures
(continued)
17
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 structures statements
  • Block group of statements that execute as a
    single unit ( compound statement )

18
Understanding the Three Basic Structures
(continued)
19
Understanding the Three Basic Structures
(continued)
20
Understanding the Three Basic Structures
(continued)
21
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

22
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

Sequence
Selection
23
Using the Priming Read
  • 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
  • First ask a question
  • Take action based on the answer
  • Return to ask the question again

24
Using the Priming Read (continued)
  • Unstructured loop

25
Using the Priming Read (continued)
  • Structured but nonfunctional loop

26
Using the Priming Read (continued)
  • Functional but non-structured loop

27
Using the Priming Read (continued)
  • Functional and structured loop

28
Using the Priming Read (continued)
  • Priming read sets up the process so the loop can
    be structured
  • To analyze a flowcharts structure, try writing
    pseudocode for it

29
Using the Priming Read (continued)
  • What is wrong with this design?
  • this would be okay in some languages!

30
Understanding the Reasons for Structure
  • Advantages of structure
  • Provides clarity
  • Professionalism
  • Efficiency
  • Ease of maintenance
  • Supports modularity
  • Also,very importantly, you cannot write code that
    is not using "structured" logic without the use
    of a goto statement!

31
Understanding the Reasons for Structure
(continued)
32
Understanding the Reasons for Structure
(continued)
33
Recognizing Structure
  • Any set of instructions can be expressed in
    structured format
  • Is this flowchart structured?

34
Recognizing Structure (continued)
  • Is this flowchart structured?

35
Recognizing Structure (continued)
  • To make it structured, pull each symbol out and
    rework
  • B begins a selection structure

36
Recognizing Structure (continued)
  • Pull up on the flowline from the left side of B

37
Recognizing Structure (continued)
  • Next, pull up the flowline on the right side of B

38
Recognizing Structure (continued)
  • Pull up the flowline on the left side of D and
    untangle it from the B selection by repeating C

39
Recognizing Structure (continued)
  • Now pull up the flowline on the right side of D

40
Recognizing Structure (continued)
  • Bring together the loose ends of D and of B

41
Three Special Structures
  • Many languages allow three additional structures
  • case structure
  • do-while structure
  • 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

42
Three Special Structures Case, Do While, and Do
Until (continued)
  • Using nested if-then-else for multiple
    alternatives

43
Three Special Structures Case, Do While, and Do
Until (continued)
  • Using a case structure for multiple alternatives

44
Three Special Structures Case, Do While, and Do
Until (continued)
  • do-while and do-until loops
  • Question is asked at the end of the loop
    structure
  • Ensures that the loop statements are always used
    at least once

45
post-test versus pre-test loop
46
Three Special Structures Case, Do While, and Do
Until (continued)
  • do-while loop executes as long as the questions
    answer is Yes or True
  • do-until loop executes as long as the questions
    answer is No or False (until it becomes Yes or
    True)

47
Three Special Structures Case, Do While, and Do
Until (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
  • A posttest loop can be replaced with a sequence
    followed by a pretest while loop

48
Three Special Structures Case, Do While, and Do
Until (continued)
49
Three Special Structures Case, Do While, and Do
Until (continued)
50
Three Special Structures Case, Do While, and Do
Until (continued)
  • How can this design be made structured?

51
Three Special Structures Case, Do While, and Do
Until (continued)
  • Repeat the needed step to enforce structure

52
Summary
  • Spaghetti code snarled program logic
  • Three basic structures sequence, selection,
    loop
  • These three can be combined by stacking and
    nesting
  • Priming read statement that reads the first
    input data record
  • Structured techniques promote clarity,
    professionalism, efficiency, and modularity

53
Summary (continued)
  • Flowchart can be made structured by untangling
  • case structure handles questions with multiple
    alternatives
  • while loop a pretest loop that asks the
    question first
  • while loop statements may never be executed if
    the answer is No
  • do-while and do-until loops posttest loops that
    ask the question last

54
Summary (continued)
  • 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
Write a Comment
User Comments (0)
About PowerShow.com