Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Object-Oriented Approach to Programming Logic and Design Second Edition Chapter 2 Understanding Structure.

Similar presentations


Presentation on theme: "An Object-Oriented Approach to Programming Logic and Design Second Edition Chapter 2 Understanding Structure."— Presentation transcript:

1 An Object-Oriented Approach to Programming Logic and Design Second Edition Chapter 2 Understanding Structure

2 An Object-Oriented Approach to Programming Logic and Design2 Objectives Understand why programs need to repeat instructions Learn how to stop a program from executing infinitely Learn about unstructured spaghetti code Learn the three basic structures: sequence, selection, and loop Build structured methods

3 An Object-Oriented Approach to Programming Logic and Design3 Objectives (cont’d.) Use a priming read Appreciate the reasons to use structure Recognize structure Learn about three special structures - case, do- while, and do-until Learn about methods

4 An Object-Oriented Approach to Programming Logic and Design4 Understanding the Need to Repeat Program Instructions Methods –More complicated than number-doubling application Figure 2-1 Flowchart and pseudocode for the DoubleNumber class

5 An Object-Oriented Approach to Programming Logic and Design5 Understanding the Need to Repeat Program Instructions (cont’d.) Tasks after flowchart or pseudocode is developed –Buy computer –Buy language compiler –Learn programming language –Code the program –Attempt to compile it –Fix syntax errors –Compile it again –Test with several data sets –Put into production

6 An Object-Oriented Approach to Programming Logic and Design6 Understanding the Need to Repeat Program Instructions (cont’d.) Effort to write computer program –Worth it if many (10,000) numbers to process in limited amount of time (two minutes) Figure 2-1: number-doubling program –Does not double 10,000 numbers: doubles one –Could execute program 10,000 times Requires manual keyboard input 10,000 times –Better off processing 10,000 numbers One after the other

7 An Object-Oriented Approach to Programming Logic and Design7 Understanding the Need to Repeat Program Instructions (cont’d.) One solution (Figure 2-2) –Execute same steps 10,000 times –Disadvantage: long program, time-consuming Better off with calculator Better solution (Figure 2-3) –Execute same set of three instructions over and over –Advantage: memory reused InputNumber and CalculatedAnswer –Major problem Instruction sequence never ends

8 An Object-Oriented Approach to Programming Logic and Design8 Understanding the Need to Repeat Program Instructions (cont’d.) Figure 2-2 Inefficient flowchart and pseudocode for program that doubles 10,000 numbers

9 An Object-Oriented Approach to Programming Logic and Design9 Understanding the Need to Repeat Program Instructions (cont’d.) Figure 2-3 Flowchart and pseudocode of infinite number- doubling program

10 An Object-Oriented Approach to Programming Logic and Design10 Stopping a Program from Executing Infinitely Indefinite loop – Repeating flow of logic with no end Problem –Computer is patient: waits for input forever Prevents program progress Program occupies computer memory Program ties up operating system resources Non-practical solutions –Refuse to enter more numbers –Turn off computer

11 An Object-Oriented Approach to Programming Logic and Design11 Stopping a Program from Executing Infinitely (cont’d.) Superior solution –Set predetermined value meaning “stop the program!” –Programmer and user must agree on value –Example Use zero (user will never double zero) –Testing a value Comparing to another value to make decision Flowchart decision –Decision symbol shaped like diamond –Answer: one of two mutually exclusive options Yes or no

12 An Object-Oriented Approach to Programming Logic and Design12 Stopping a Program from Executing Infinitely (cont’d.) Figure 2-4 Flowchart of number-doubling program with sentinel value of 0

13 An Object-Oriented Approach to Programming Logic and Design13 Stopping a Program from Executing Infinitely (cont’d.) Drawback (stopping program with zero) –Will not work if user calculates with zero –Alternate stopping values 999 or negative one Dummy value –Preselected value stopping program execution Does not represent real data Just a stop signal –Sentinel value Alternative name for dummy variable Represents entry or exit point

14 An Object-Oriented Approach to Programming Logic and Design14 Stopping a Program from Executing Infinitely (cont’d.) Not all programs rely on keyboard data entry –Options: disk or flash memory Data stored on disk (other storage device) –No use of dummy value signaling end of file –Reasons Wastes large storage quantity on “non-data” Difficult to choose sentinel values –Solution Languages automatically recognize end of file Code stored at end of data –End of file: eof

15 An Object-Oriented Approach to Programming Logic and Design15 Stopping a Program from Executing Infinitely (cont’d.) Figure 2-5 Flowchart of number-doubling program using eof

16 An Object-Oriented Approach to Programming Logic and Design16 Understanding Unstructured Spaghetti Code Spaghetti code –Popular name for snarled program statements –Code confusing to read Like following one noodle through plate of spaghetti –Example Table 2-1 Admission requirements

17 An Object-Oriented Approach to Programming Logic and Design17 Figure 2-6 Unstructured spaghetti code example

18 An Object-Oriented Approach to Programming Logic and Design18 Understanding the Three Basic Structures: Sequence, Selection, and Loop Any program or method within program or class –Constructed using only three structures Proven in mid-1960s Structure –Basic unit of programming logic –Sequence, selection, loop –With these three structures alone Can diagram any event

19 An Object-Oriented Approach to Programming Logic and Design19 The Sequence Structure Perform an action or event –Perform next action in order No limit of events No chance to branch off No chance to skip events “Straight-through” aspect –No branches Figure 2-7 Sequence structure

20 An Object-Oriented Approach to Programming Logic and Design20 The Selection Structure Figure 2-8 Selection structure Also known as a decision structure Ask question Answer dependency –Take one of two courses of action –No matter which path followed Continue with next event

21 An Object-Oriented Approach to Programming Logic and Design21 The Selection Structure (cont’d.) Dual-alternative if s –Contains two alternatives Action taken when tested condition true Action taken when tested condition false if someCondition is true then do oneProcess else do anotherProcess

22 An Object-Oriented Approach to Programming Logic and Design22 The Selection Structure (cont’d.) Single-alternative if s –No special action if first condition false –Null case Do nothing Figure 2-9 Single- alternative selection structure

23 An Object-Oriented Approach to Programming Logic and Design23 The Loop Structure Loop structure –Ask question –If answer requires action Perform action and ask original question again If answer requires action be taken again Take the action and ask original question again –Continues until answer requires no further action Exit structure Looping –Also know as repetition or iteration

24 An Object-Oriented Approach to Programming Logic and Design24 The Loop Structure (cont’d.) While loop ( while-do loop) Figure 2-10 Loop structure while testCondition continues to be true do someProcess

25 An Object-Oriented Approach to Programming Logic and Design25 Building Structured Methods All logic problems solvable using structures –Sequence, selection, looping –Combined in infinite number of ways Stacking structure (Figure 2-11) –Attaching structures end-to-end Nesting the structure (Figure 2-12) –Placing structure within another structure Any sequence, selection, loop can contain other sequences, selections, loops

26 An Object-Oriented Approach to Programming Logic and Design26 Building Structured Methods (cont’d.) Figure 2-11 Stacked, structured flowchart and pseudocode

27 An Object-Oriented Approach to Programming Logic and Design27 Building Structured Methods (cont’d.) Figure 2-12 Flowchart and pseudocode showing a sequence nested within a selection

28 An Object-Oriented Approach to Programming Logic and Design28 Building Structured Methods (cont’d.) Pseudocode conventions –Indent statements depending on one decision branch Known as a block –Condition statements align vertically with each other “On the same level” Important for readability in nested structures See Figures 2-13 and 2-14

29 An Object-Oriented Approach to Programming Logic and Design29 Building Structured Methods (cont’d.) Figure 2-13 Selection within a sequence within a selection

30 An Object-Oriented Approach to Programming Logic and Design30 Building Structured Methods (cont’d.) Figure 2-14 Flowchart and pseudocode for a loop within a selection within a sequence within a selection

31 An Object-Oriented Approach to Programming Logic and Design31 Building Structured Methods (cont’d.) Structured method characteristics summary –Includes combinations of three structures –Structures stacked at entrance (exit) points –Structure can be nested within another structure Figure 2-15 The three structures

32 An Object-Oriented Approach to Programming Logic and Design32 Using a Priming Read Determining if program is structured –Difficult because of stacked and nested structures –Must analyze flowchart one step at a time Example –Figures 2-16 through 2-20 Breaks down Figure 2-5

33 An Object-Oriented Approach to Programming Logic and Design33 Using a Priming Read (cont’d.) Figure 2-16 analysis –Beginning of flowchart –First two steps Declaration of variables Getting first input value –Structured? Yes (a sequence) Figure 2-16 Beginning of number-doubling flowchart

34 An Object-Oriented Approach to Programming Logic and Design34 Using a Priming Read (cont’d.) Figure 2-17 Continuation of number-doubling flowchart Figure 2-17 analysis –Sequence is finished –Selection or loop starting? –Selection: logic goes in one of two directions and flows back together –Loop: logic returns to question starting the loop –Figure 2-5 logic contains loop Not structured!

35 An Object-Oriented Approach to Programming Logic and Design35 Using a Priming Read (cont’d.) Figure 2-18 analysis –Shows flow of logic returning to question immediately After the sequence –Shows a structured flowchart segment –Flowchart major flaw Does not continuously double numbers

36 An Object-Oriented Approach to Programming Logic and Design36 Using a Priming Read (cont’d.) Figure 2-18 Structured, but nonfunctional, flowchart segment of number-doubling problem

37 An Object-Oriented Approach to Programming Logic and Design37 Using a Priming Read (cont’d.) Figure 2-19 analysis: not structured –Logic does not return to loop-controlling question Goes “too high” outside loop repeating the get inputNumber step Need a structured and correctly working solution –Add something extra In this case: extra get inputNumber step

38 An Object-Oriented Approach to Programming Logic and Design38 Using a Priming Read (cont’d.) Figure 2-20 analysis –Structured and does what supposed to do Logic illustrated contains sequence and a loop Loop contains another sequence Priming input (priming read) –First of the two input steps –Purpose Control upcoming loop that begins with eof? question

39 An Object-Oriented Approach to Programming Logic and Design39 Using a Priming Read (cont’d.) Figure 2-19 Functional, but unstructured flowchart segment

40 An Object-Oriented Approach to Programming Logic and Design40 Using a Priming Read (cont’d.) Figure 2-20 Functional, structured flowchart and pseudocode for number- doubling problem

41 An Object-Oriented Approach to Programming Logic and Design41 Using a Priming Read (cont’d.) Typical structured method features –Additional get inputNumber step (Figure 2-20) –Last element within structured loop gets next input values and all subsequent input values –Last step executed within loop alters question condition tested beginning the loop Correct structure alone –Not always correctly working program Program working correctly –Not always structured

42 An Object-Oriented Approach to Programming Logic and Design42 Using a Priming Read (cont’d.) Goal –Achieve both structured design and correct functionality Determining if flowchart segment structured –Another way Write the psuedocode See example on page 50

43 An Object-Oriented Approach to Programming Logic and Design43 Understanding the Reasons for Structure Reason to use three structures –Clarity: small doubling method –Professionalism: way things are done –Efficiency: modern computer languages are structured languages –Maintenance: easier –Modularity: routines assigned to number of programmers Modern program development –Huge and contain thousands or millions of statements

44 An Object-Oriented Approach to Programming Logic and Design44 Understanding the Reasons for Structure (cont’d.) Advantages –Objects created communicate with each other –Structured method code easily understood and revised –Large application developed much more quickly Motivating factor –Money

45 An Object-Oriented Approach to Programming Logic and Design45 Figure 2-21 Flowchart and pseudocode of structured college admission program (continued)

46 An Object-Oriented Approach to Programming Logic and Design46 Figure 2-21 Flowchart and pseudocode of structured college admission program

47 An Object-Oriented Approach to Programming Logic and Design47 Recognizing Structure Any instruction set –Expressible in structured format Task with rules –Express logically using any combinations of sequence, selection, looping Students and programmers early in careers –Find it difficult to detect if flowchart is structured or not

48 An Object-Oriented Approach to Programming Logic and Design48 Recognizing Structure (cont’d.) Figure 2-23 structured? –Yes –Has sequence and selection structure Figure 2-24 structured? –Yes –Has a loop Within loop is a selection Figure 2-23 Example 1 Figure 2-24 Example 2

49 An Object-Oriented Approach to Programming Logic and Design49 Recognizing Structure (cont’d.) Figure 2-25 structured? –No –Not constructed from the three basic structures Figure 2-25 Example 3

50 An Object-Oriented Approach to Programming Logic and Design50 Recognizing Structure (cont’d.) Untangle Figure 2-25 Spaghetti bowl method –Start pulling at Figure 2-25 top –Encounter procedure box labeled A (Figure 2-26) Figure 2-26 Untangling Example 3, first step

51 An Object-Oriented Approach to Programming Logic and Design51 Recognizing Structure (cont’d.) Spaghetti bowl method (cont’d.) –Next item is a question Testing condition labeled B (Figure 2-27) Now know sequence starting with A ended Figure 2-27 Untangling Example 3, second step

52 An Object-Oriented Approach to Programming Logic and Design52 Recognizing Structure (cont’d.) Spaghetti bowl method (cont’d.) –Pull flowline from “No” side of Question B Encounter C (Figure 2-28) Figure 2-28 Untangling Example 3, third step

53 An Object-Oriented Approach to Programming Logic and Design53 Recognizing Structure (cont’d.) Spaghetti bowl method (cont’d.) –Pull flowline from “Yes” side of Question B Encounter D (Figure 2-29) Figure 2-29 Untangling Example 3, fourth step

54 An Object-Oriented Approach to Programming Logic and Design54 Recognizing Structure (cont’d.) Spaghetti bowl method (cont’d.) – Follow line on left side of Question D If line attached somewhere, untangle by repeating tangled step –Continue pulling flowline emerging from Step C Reach end of program segment (Figure 2-30) Figure 2-30 Untangling Example 3, fifth step

55 An Object-Oriented Approach to Programming Logic and Design55 Recognizing Structure (cont’d.) Spaghetti bowl method (cont’d.) –Pull right side of Question D Process E pops up (Figure 2-31) Reached the end Figure 2-31 Untangling Example 3, sixth step

56 An Object-Oriented Approach to Programming Logic and Design56 Recognizing Structure (cont’d.) Three loose ends –Question D brought together: selection structure –Question B loose ends brought together: selection structure Figure 2-32 Finished flowchart and pseudocode for untangling Example 3

57 An Object-Oriented Approach to Programming Logic and Design57 Describing Three Special Structures – Case, Do-While, and Do-Until Three more forms of basic structures –Case Alternative decision-making structure –Do-while Alternative to while loop –Do-until loops Alternative to while loop Never needed to solve a problem –Sometimes convenient All acceptable, legal structures

58 An Object-Oriented Approach to Programming Logic and Design58 The Case Structure Several possible values exists for single variable being tested –Each value requires different course of action Flow passes through only one alternative Flowchart, pseudocode, program code convenience –Easier to understand at first glance Examples –Figure 2-23: series of decisions –Figure 2-24: case structure implementing decisions

59 An Object-Oriented Approach to Programming Logic and Design59 The Case Structure (cont’d.) Figure 2-33 Flowchart and pseudocode of tuition decisions

60 An Object-Oriented Approach to Programming Logic and Design60 The Case Structure (cont’d.) Figure 2-34 Flowchart and pseudocode of case structure

61 An Object-Oriented Approach to Programming Logic and Design61 The Do-While and Do-Until Loops while loop –Condition tested at structure beginning –Condition not met at first test Code in while structure body never executed –Also called a pretest loop Figure 2-35 The while loop

62 An Object-Oriented Approach to Programming Logic and Design62 The Do-While and Do-Until Loops (cont’d.) do-while loop –Condition tested at structure end Body has executed at least once –Expressed as a sequence followed by a loop –Also called a posttest loop Figure 2-36 The do-while or do-until loop

63 An Object-Oriented Approach to Programming Logic and Design63 Introduction to Methods Self-contained program module –Containing statements to carry out task Method execution –Invoke it or call it from another method –Calling method invokes called method Class can contain unlimited number of methods Method called any number of times Simplest methods –Do not require data items sent to them –Do not send data items back

64 An Object-Oriented Approach to Programming Logic and Design64 Introduction To Methods (cont’d.) Figure 2-41 Sample logic

65 An Object-Oriented Approach to Programming Logic and Design65 Introduction To Methods (cont’d.) Figure 2-42 Logic from Figure 2-41 using a method

66 An Object-Oriented Approach to Programming Logic and Design66 Introduction To Methods (cont’d.) Figure 2-43 Logic from Figure 2-41 using two methods

67 An Object-Oriented Approach to Programming Logic and Design67 Summary Programs execute instructions over and over –Use same variables to hold series of values End program by testing input for predetermined sentinel value Spaghetti code: unstructured, snarled statements Programs constructed using basic structures –Three structures combine infinitely (stacking,nesting) –Each structure has one entry and one exit point –Structure attaches to another only at entry or exit point

68 An Object-Oriented Approach to Programming Logic and Design68 Summary (cont’d.) Priming read or priming input –First read (data input) before beginning loop Structured techniques promote clarity, professionalism, efficiency, and modularity Straighten unstructured flowchart segment by thinking of a bowl of spaghetti that you must untangle Three other generally recognized structures Method –Self-contained program module containing series of statements that carry out task


Download ppt "An Object-Oriented Approach to Programming Logic and Design Second Edition Chapter 2 Understanding Structure."

Similar presentations


Ads by Google