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

Slides:



Advertisements
Similar presentations
Chapter 2: Understanding Structure
Advertisements

Programming Logic and Design Eighth Edition
Introduction to Flowcharting
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
Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Published by Addison-Wesley.
Programming Logic and Design Seventh Edition
CS0004: Introduction to Programming Repetition – Do Loops.
Programming Logic and Design, Third Edition Comprehensive
Programming Logic and Design Seventh Edition
Programming Logic and Design Fourth Edition, Introductory
ITEC113 Algorithms and Programming Techniques
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Chapter 2: Algorithm Discovery and Design
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding Computer Components and Operations (continued) A program must be free of syntax.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Chapter 3 Planning Your Solution
Programming Logic and Design Fourth Edition, Introductory
The Program Design Phases
(C)opyright 2003 Scott/Jones Publishers Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Scott/Jones Publishers.
CSC103: Introduction to Computer and Programming
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Design the program Create a detailed description of program –Use charts or ordinary language (pseudocode) Identify algorithms needed –Algorithm: a step-by-step.
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.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
ITEC113 Algorithms and Programming Techniques
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
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.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding the Three Basic Structures Structure: a basic unit of programming logic Any program.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
2 Chapter 21 Understanding Structure Programming Logic and Design, Second Edition, Comprehensive 2.
Programming Logic and Design Fifth Edition, Comprehensive
P ROGRAMMING L OGIC GWDA123 Sharon Kaitner, M.Ed. Winter 2015: Week 2.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
Programming Logic and Design Fifth Edition, Comprehensive Chapter 7 Using Methods.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Programming Logic and Design Seventh Edition Chapter 1 An Overview of Computers and Programming.
Programming Logic and Design Fourth Edition, Introductory Chapter 2 Understanding Structure.
Programming Logic and Design Seventh Edition
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
Programming Logic and Design Eighth Edition
Using the Priming Read Priming read (or priming input):
A Beginner’s Guide to Programming Logic, Introductory
Structured Program
A First Book of ANSI C Fourth Edition
Understanding the Three Basic Structures
Writing a Complete Program
Topics Introduction to Repetition Structures
Chapter 4: Writing and Designing a Complete Program
Programming Logic and Design Eighth Edition
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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!

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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