1 / 31

Programming Logic and Design Seventh Edition

Programming Logic and Design Seventh Edition. Chapter 3 Understanding Structure. Objectives. In this chapter, you will learn about: The three basic control structures : Sequence Selection Repetition ( aka looping and iteration ) The need for structure Using a priming input.

andie
Télécharger la présentation

Programming Logic and Design Seventh Edition

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Logic and DesignSeventh Edition Chapter 3 Understanding Structure

  2. Objectives In this chapter, you will learn about: • The three basic control structures: • Sequence • Selection • Repetition( aka looping and iteration ) • The need for structure • Using a priming input

  3. Three Basic Control StructuresSequence, Selection, Repetition/Iteration/Looping Connector Use where flowlines come together in a flowchart

  4. RAPTOR Flowchart Symbols SequenceControl Structures Selection and Repetition Control Structures

  5. Understanding the Three Basic Control Structures (continued) There is a distinction between sequence and a sequence structure. The book doesn't make this distinction which is unfortunate. On the right, you see 3 sequence structures that will execute in sequence (or sequentially). To the right of that, you see 3 selection structures that will execute in a sequence(sequentiall). You enter a control structure through an entry point and exit from an exit point. Control structures can be stacked and nested. Stacked control structures are always executed in sequence. Figure 3-2 Sequence control structure Sequential execution of control structures

  6. Understanding the Three Basic Control Structures (continued) expression usually referred to as the condition null case No structure to be executed Figure 3-4Single-alternative if selection structure

  7. Understanding the Three Basic Control Structures (continued) • Single-alternative if • Else clause is not required • If the condition does not evaluate to true, the structure(s) are not executed and control passes to the next structure • null case • Situation where nothing is done • This would be the "empty" else branch of a selection structure ifemployee belongs to dentalPlanthen deduct $40 from employeeGrossPay endif

  8. Understanding the Three Basic Control Structures (continued) no yes structure structure Figure 3-3Dual-alternative ifselection structure

  9. Understanding the Three Basic Control Structures (continued) • Dual-alternative if • Contains two alternatives • If-then-else structure Pseudocode keywords: if, then, else, endif ifsomeCondition [is true] then statement_1 else statement_2 endif structures

  10. Understanding the Three Basic Control Structures (continued) • Some languages have an "else if" keyword • The spelling of the keyword in languages varies (elseif / elsif / elif) • Python uses the elifkeyword • Java and C++ do not have an "else-if" keyword • Usage: if x == 1 then print 1 elif x == 2 then print 2 ... (additional elif statements) else print "no match for x" endif

  11. Understanding the Three Basic Structures (continued) • Loop structure • Repeats a set of actions based on the answer to a question • Loop body • Also called repetition or iteration • Question is asked first in the most common form of a loop • while(pre-test)or do … while(post-test) • RAPTOR lets you put the test anywhere you like!

  12. Understanding the Three Basic Control Structures (continued) Entry Point Connector When using drawing software such as Visio Figure 3-5Loop structure (pre-test form – while loop)

  13. Understanding the Three Basic Control Structures (continued) • Loop structure [ pre-test loop ] whiletestConditionis true dosomeStatement endwhile while count < 11 printCount() //module call count += 1 endwhile

  14. Understanding the Three Basic Control Structures (continued) • All logic problems can be solved using only these three control structures • Structures can be combined in an infinite number of ways • Stacking • Attaching structures end-to-end at their entry/exit points • Nesting • discussed a few slides later… • End-structure statements • Indicate the end of a structure • endif ends an if-then-else structure • endwhile ends a pre-test loop structure • enddoends a post-test loop structure

  15. Understanding the Three Basic Control Structures (continued) Figure 3-6 Structured flowchart and pseudocode with three stacked structures

  16. Understanding the Three Basic Control Structures (continued) • Any individual task or step can be inserted by creating a new structure or an existing structure can be replaced by a different structure • Nesting • Placing one structure within another • Indent the nested structure’s statements • Block [ aka compound statement ] • Group of statements that execute as a single unit • Java and C++ use { } to enlcose these • Python relies on indentation of the block (suite)

  17. Understanding the Three Basic Control Structures (continued) block or compound statement Figure 3-7 Flowchart and pseudocode showing nested structures [ sequence nested within a selection ]

  18. Understanding the Three Basic Control Structures (continued) entry point and exit point for a structure structures have ONE of each Figure 3-8 Flowchart and pseudocode showing nested structures a loop nested within a sequence, nested within a selection

  19. Understanding the Three Basic Structures (continued) selection Note: The diamond flowchart symbol is used for both selection and loop structures. How do you know if you are looking at a selection or loop structure? A selection structure only has flowlines merging at the exit point for the structure A loop structure will always have flowlines merging above the test expression and the body of the loop loop Figure 3-9 Flowchart and pseudocode for selection structure with nested sequence and loop structures

  20. Understanding the Three Basic Control Structures (continued) • Rules for creating a "structured" flowchart • Include only combinations of the three basic control structures (sequence, selection, loop) • Each of the structures has a single entry point and a single exit point • Structures can be stacked(connected) to one another only at their entry and exit points • Ideally whenever flowchart lines come together a connector is used (if drawn manually) • Selection and Loop structures can have nestedstructures

  21. Using a Priming Input • Priming input (or priming read) • Does the first input operation outside of the loop that inputs the rest of the data • Inside the body of the loop the input operation is usually the last operation performed in the body of the loop • Note: it is not always necessary to do this…Some languages can “look ahead” to see if there is any data that can be input

  22. Using a Priming Input (continued) RAPTOR would let you do this because there are languages that support a “mid-test” condition. Our book only considers pre-test and post-test loops as structured. Figure 3-16 Programming Logic & Design, Sixth Edition

  23. Using a Priming Input (continued) • Priming input sets up the process so the loop can be structured • To analyze a flowchart’s structure, try writing pseudocode for it start getinputNumber//priming input while not eof calculatedAnswer = inputNumber * 2 printcalculatedAnswer getinputNumber endwhile stop

  24. A priming input is OUTSIDE the loop structure. It precedes the structure. The input structure INSIDE the loop is usually the last structure nested in the loop. Figure 3-17number-doubling problem

  25. Figure 3-18Structured but incorrect solution to the number-doubling problem Programming Logic & Design, Sixth Edition

  26. 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, and loop control structures

  27. Flowchart Status • Flowcharts fall into one of the 2 following categories: • structured • unstructured

  28. Recognizing Structure (continued) This is a no no! Understandable but not Structured Figure 3-21 Example 3

  29. Recognizing Structure (continued) What are the structures? Are they: stacked? nested? Are they: structured? Figure 3-20 Example 2 Programming Logic & Design, Sixth Edition

  30. Recognizing Structure (continued) Nested selection structures in RAPTOR Programming Logic and Design, Seventh Edition

  31. Summary • Spaghetti code • Snarled program logic • Three basic control structures • Sequence, selection, and looping • Combined by stacking and/or nesting • Priming input • Statement that reads the first input data record

More Related