Programming Logic and Design Seventh Edition

Slides:



Advertisements
Similar presentations
Chapter 2: Understanding Structure
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Programming Logic and Design Eighth Edition
Repetition Control Structures School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 9, Friday 3/07/2003)
Introduction to Flowcharting
Understanding the Three Basic Structures
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture two Dr. Hamdy M. Mousa.
CHAPTER 1: AN OVERVIEW OF COMPUTERS AND LOGIC. Objectives 2  Understand computer components and operations  Describe the steps involved in the programming.
An Object-Oriented Approach to Programming Logic and Design
Programming Logic and Design Seventh Edition
CS0004: Introduction to Programming Repetition – Do Loops.
Programming Logic and Design, Third Edition Comprehensive
Objectives In this chapter, you will learn about:
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Program Design and Development
Chapter 2 The Algorithmic Foundations of Computer Science
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
Programming Logic and Design Sixth Edition
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science, Java Version, Second Edition.
Chapter 3 Making Decisions
Programming Logic and Design Fifth Edition, Comprehensive
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
`. Lecture Overview Structure Programming Basic Control of Structure Programming Selection Logical Operations Iteration Flowchart.
CHAPTER 2: Understanding Structure. Objectives 2  Learn about the features of unstructured spaghetti code  Understand the three basic structures: sequence,
(C)opyright 2000 Scott/Jones Publishers Introduction to Flowcharting.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding the Three Basic Structures Structure: a basic unit of programming logic Any program.
2 Chapter 21 Understanding Structure Programming Logic and Design, Second Edition, Comprehensive 2.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Programming Logic and Design Fifth Edition, Comprehensive
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
An Object-Oriented Approach to Programming Logic and Design Second Edition Chapter 2 Understanding Structure.
P ROGRAMMING L OGIC GWDA123 Sharon Kaitner, M.Ed. Winter 2015: Week 2.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Pseudocode. Algorithm A procedure for solving a problem in terms of the actions to be executed and the order in which those actions are to be executed.
Programming Logic and Design Seventh Edition Chapter 1 An Overview of Computers and Programming.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 Making Decisions.
Programming Logic and Design Fourth Edition, Introductory Chapter 2 Understanding Structure.
An Introduction to Programming with C++ Sixth Edition Chapter 8 More on the Repetition Structure.
Programming Logic and Design Seventh Edition
More on the Selection Structure
REPETITION CONTROL STRUCTURE
Programming Logic and Design Fourth Edition, Comprehensive
Programming Logic and Design Eighth Edition
Introduction To Flowcharting
Chapter 5: Repetition Structures
Using the Priming Read Priming read (or priming input):
A Beginner’s Guide to Programming Logic, Introductory
Understanding the Reasons for Structure
MSIS 655 Advanced Business Applications Programming
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Understanding the Three Basic Structures
Chapter 8: More on the Repetition Structure
Boolean Expressions to Make Comparisons
Presentation transcript:

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 The features of unstructured spaghetti code Recognizing structure Structuring and modularizing unstructured logic Using a priming input to structure a program

Three Basic Control Structures Sequence, Selection, Repetition/Iteration/Looping

RAPTOR Symbols Sequence Control Structures Selection and Repetition

Understanding Unstructured Spaghetti Code Logically snarled program statements Can be the result of poor program design Programs often work but are difficult to read and maintain Convoluted logic usually requires more code Unstructured programs Do not follow the rules of structured logic Structured programs Do follow rules of structured logic RAPTOR does not permit you to draw an unstructured flowchart

Figure 3-1 Spaghetti code logic for washing a dog

Understanding the Three Basic Structures A Control Structure is a basic unit of programming logic Sequence Perform a single action ( input, output, assign, … ) Selection ( aka decision ) Ask a question, take one of two actions Dual-alternative or single-alternative if's Loop ( aka repetition/iteration ) Repeat actions based on answer to a question

Understanding the Three Basic 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 being executed in sequence ( or sequentially ). To the right of that, you see 3 selection structures in a sequence. 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

Understanding the Three Basic Structures (continued) no yes question usually referred to as the condition statement statement Figure 3-3 Selection structure

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

Understanding the Three Basic Structures (continued) Single-alternative if Else clause is not required If the condition does not evaluate to true, the statement(s) are not executed and control passes to the next structure null case Situation where nothing is done if employee belongs to dentalPlan then do deduct $40 from employeeGrossPay endif

Understanding the Three Basic Structures (continued) Figure 3-4 Single-alternative selection structure

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 … do (pre-test) or do … while (post-test) RAPTOR lets you put the test anywhere you like!

Understanding the Three Basic Structures (continued) Connector When using drawing software such as Visio Figure 3-5 Loop structure (pre-test form – while … do)

Understanding the Three Basic Structures (continued) Loop structure [ pre-test loop ] while testCondition continues to be true do someStatement endwhile while count less than 11 do printCount() //module call do add 1 to count

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 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 loop structure

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

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 structure’s statements Block [ aka compound statement ] Group of statements that execute as a single unit Java uses { } to enlcose these

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

Understanding the Three Basic 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

Understanding the Three Basic Structures (continued) Figure 3-9 Flowchart and pseudocode for selection structure with nested sequence and loop structures

Understanding the Three Basic Structures (continued) Structured programs have the following characteristics: Include only combinations of the three basic structures Each of the structures has a single entry point and a single exit point Structures can be stacked or connected to one another only at their entry or exit points Selection and Loop structures can have nested structures

Using a Priming Input to Structure a Program Priming read (or priming input) Reads the first input data record outside of the loop that reads the rest of the records Helps keep the program structured Note: it is not always necessary to do this… 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

Using a Priming Input to Structure a Program (continued) Figure 3-15 Structured, but nonfunctional, flowchart of number-doubling problem

Using a Priming Input to Structure a Program (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 Functional but unstructured flowchart Programming Logic & Design, Sixth Edition

Using a Priming Input to Structure a Program (continued) Priming read 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 read while not eof calculatedAnswer = inputNumber * 2 print calculatedAnswer get inputNumber endwhile stop

Figure 3-17 Functional, structured flowchart for the number-doubling problem

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

Understanding the Reasons for Structure Clarity Professionalism Efficiency Ease of maintenance Supports modularity

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, loop It can be difficult to detect whether a flowchart is structured

Flowchart Status Flowcharts fall in to one of the 3 following categories: structured AND functional structured but NOT functional functional but NOT structured These categories will be presented over the next several slides.

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

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

Recognizing Structure (continued) Single process like G is part of an acceptable structure At least the beginning of a sequence structure Figure 3-22 Untangling Example 3, first step

Recognizing Structure (continued) H begins a selection structure Sequences never have decisions in them Logic never returns to G Figure 3-23 Untangling Example 3, second step

Recognizing Structure (continued) Pull up on the flowline from the left side of H Figure 3-24 Untangling Example 3, third step

Recognizing Structure (continued) Next, pull up the flowline on the right side of H Figure 3-25 Untangling Example 3, fourth step

Recognizing Structure (continued) Pull up the flowline on the left side of I and untangle it from the B selection by repeating J Figure 3-26 Untangling Example 3, fifth step

Recognizing Structure (continued) Now pull up the flowline on the right side of I Figure 3-27 Untangling Example 3, sixth step

Recognizing Structure (continued) Bring together the loose ends of I and of H Figure 3-28 Finished flowchart and pseudocode for untangling Example 3

Structuring and Modularizing Unstructured Logic Dog-washing process Unstructured Can be reconfigured to be structured First step simple sequence Figure 3-29 Washing the dog, part 1

Structuring and Modularizing Unstructured Logic (continued) After the dog runs away Catch the dog and determine whether he runs away again A loop begins Figure 3-30 Washing the dog, part 2

Programming Logic & Design, Sixth Edition Figure 3-31 Washing the dog, part 3 Programming Logic & Design, Sixth Edition

Programming Logic & Design, Sixth Edition Figure 3-33 Structured dog-washing flowchart and pseudocode Programming Logic & Design, Sixth Edition

Programming Logic & Design, Sixth Edition Figure 3-34 Modularized version of the dog-washing program Programming Logic & Design, Sixth Edition

Summary Spaghetti code Three basic structures Priming read Snarled program logic Three basic structures Sequence, selection, and loop Combined by stacking and nesting Priming read Statement that reads the first input data record

Summary (continued) Structured techniques promote: Clarity Professionalism Efficiency Modularity Flowchart can be made structured by untangling