Transcript pld7e_ch03

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 ) 2 • • The need for structure Using a priming input

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

4

RAPTOR Flowchart Symbols

Sequence Control Structures Selection and Repetition Control Structures

Understanding the Three Basic Control Structures (continued) 5 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

Understanding the Three Basic Control Structures (continued) expression usually referred to as the condition null case

No structure to be executed Figure 3-4

Single-alternative if selection structure 6

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

8 Understanding the Three Basic Control Structures (continued) structure no yes structure

Figure 3-3

Dual-alternative if selection structure

Understanding the Three Basic Control Structures (continued) 9 •

Dual-alternative if

– – Contains two alternatives

If-then-else structure

if someCondition [is true] then statement_1 else endif statement_2 Pseudocode keywords: if, then, else, 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

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

Understanding the Three Basic Structures (continued)

11 •

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!

Understanding the Three Basic Control Structures (continued) Entry Point Connector When using drawing software such as Visio 12

Figure 3-5

Loop structure (pre-test form – while loop)

Understanding the Three Basic Control Structures (continued) 13 •

Loop structure

[ pre-test loop ]

while testCondition is true do someStatement endwhile while count < 11 printCount() //module call count += 1 endwhile

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

Understanding the Three Basic Control Structures (continued)

Figure 3-6

Structured flowchart and pseudocode with three stacked structures 15

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

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

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 18 entry point and exit point for a structure structures have ONE of each

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?

loop A selection structure only has flowlines merging at the exit point for the structure 19 Figure 3-9 Flowchart and pseudocode for selection structure with nested sequence and loop structures A loop structure will always have flowlines merging above the test expression and the body of the loop

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 exit point single 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 20

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

Using a Priming Input (continued) RAPTOR condition.

would let you do this because there are languages that support a “mid-test” Our book only considers pre-test and post-test loops as structured.

Figure 3-16

Programming Logic & Design, Sixth Edition 22

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 get not eof calculatedAnswer = inputNumber * 2 print calculatedAnswer inputNumber endwhile stop

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.

24

Figure 3-18

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

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: 1. structured 2. unstructured

Recognizing Structure (continued)

This is a no no!

Understandable

but not

Structured Figure 3-21

Example 3 28

Recognizing Structure (continued)

What are the structures?

Are they: stacked? nested?

Are they: structured?

Figure 3-20

Example 2 Programming Logic & Design, Sixth Edition 29

Recognizing Structure (continued)

Nested selection structures in RAPTOR Programming Logic and Design, Seventh Edition 30

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