Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure.

Similar presentations


Presentation on theme: "Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure."— Presentation transcript:

1 Programming Logic and Design Seventh 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 2

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

4 RAPTOR Flowchart Symbols 4 Sequence Control Structures Selection and Repetition Control Structures

5 5 Understanding the Three Basic Control Structures (continued) Figure 3-2 Sequence control structure Sequential execution of control structures 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.

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

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 7 if employee belongs to dentalPlan then deduct $40 from employeeGrossPay endif

8 8 Understanding the Three Basic Control Structures (continued) Figure 3-3 Dual-alternative if selection structure noyes structure

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

10 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 elif keyword 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 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 12 Understanding the Three Basic Control Structures (continued) Figure 3-5 Loop structure (pre-test form – while loop) Connector When using drawing software such as Visio Entry Point

13 13 Understanding the Three Basic Control Structures (continued) Loop structure [ pre-test loop ] while testCondition is true do someStatement 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 – enddo ends a post-test loop structure 14

15 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) 16

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

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

19 19 Understanding the Three Basic Structures (continued) Figure 3-9 Flowchart and pseudocode for selection structure with nested sequence and loop structures 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? selection loop 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

20 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 nested structures

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 21

22 Using a Priming Input (continued) Programming Logic & Design, Sixth Edition 22 Figure 3-16 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.

23 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 get inputNumber //priming input while not eof calculatedAnswer = inputNumber * 2 print calculatedAnswer get inputNumber endwhile stop

24 24 Figure 3-17 number-doubling problem 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.

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

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 26

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

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

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

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

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 31


Download ppt "Programming Logic and Design Seventh Edition Chapter 3 Understanding Structure."

Similar presentations


Ads by Google