Programming Logic and Design Fifth Edition, Comprehensive

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)
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
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.
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 Seventh Edition
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Program Design and Development
Chapter 5: Control Structures II (Repetition)
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
(C)opyright 2003 Scott/Jones Publishers Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Scott/Jones Publishers.
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.
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
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.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
CHAPTER 2: Understanding Structure. Objectives 2  Learn about the features of unstructured spaghetti code  Understand the three basic structures: sequence,
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
(C)opyright 2000 Scott/Jones Publishers Introduction to Flowcharting.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
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 2: Algorithm Discovery and Design Invitation to Computer Science.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 Making Decisions.
Programming Logic and Design Fourth Edition, Introductory Chapter 2 Understanding Structure.
Programming Logic and Design Seventh Edition
REPETITION CONTROL STRUCTURE
Programming Logic and Design Fourth Edition, Comprehensive
Programming Logic and Design Eighth Edition
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
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Understanding the Three Basic Structures
Chapter 8: More on the Repetition Structure
Three Special Structures – Case, Do While, and Do Until
Presentation transcript:

Programming Logic and Design Fifth Edition, Comprehensive Chapter 2 Understanding Structure

Objectives Learn about the features of unstructured spaghetti code Understand the three basic structures: sequence, selection, and loop Use a priming read Appreciate the need for structure Recognize structure Learn about three special structures: case, do-while, and do-until Programming Logic and Design, Fifth Edition, Comprehensive

Understanding Unstructured Spaghetti Code Spaghetti code: logically snarled program statements Can be the result of poor program design Spaghetti code programs often work, but are difficult to read and maintain Convoluted logic usually requires more code Programming Logic and Design, Fifth Edition, Comprehensive

Understanding Unstructured Spaghetti Code (continued) Example: College Admissions Admit students who score >= 90 on admissions test if upper 75 percent of high-school graduating class Admit students who score >= 80 on test if upper 50 percent of high-school graduating class Admit students who score >= 70 on admission test if upper 25 percent of high-school graduating class Programming Logic and Design, Fifth Edition, Comprehensive

Understanding Unstructured Spaghetti Code (continued) Figure 2-2 Spaghetti code example Programming Logic and Design, Fifth Edition, Comprehensive

Understanding the Three Basic Structures Structure: basic unit of programming logic Any program can be constructed from only three basic types of structures Sequence Perform actions in order No branching or skipping any task Selection (decision) Ask a question, take one of two actions Dual-alternative or single-alternative Loop Repeat actions based on answer to a question Programming Logic and Design, Fifth Edition, Comprehensive

Understanding the Three Basic Structures (continued) Sequence structure Figure 2-3 Sequence structure Programming Logic and Design, Fifth Edition, Comprehensive

Understanding the Three Basic Structures (continued) Selection structure Figure 2-4 Selection structure Programming Logic and Design, Fifth Edition, Comprehensive

Understanding the Three Basic Structures (continued) Dual-alternative if: contains two alternatives if someCondition is true then do oneProcess else do theOtherProcess Programming Logic and Design, Fifth Edition, Comprehensive

Understanding the Three Basic Structures (continued) Single-alternative if: contains one alternative Figure 2-5 Single-alternative selection structure Programming Logic and Design, Fifth Edition, Comprehensive

Understanding the Three Basic Structures (continued) Single-alternative if Else clause is not required Null case: situation where nothing is done if employee belongs to dentalPlan then deduct $40 from employeeGrossPay Programming Logic and Design, Fifth Edition, Comprehensive

Understanding the Three Basic Structures (continued) Loop structure Repeats a set of actions based on the answer to a question Also called repetition or iteration Question is asked first in the most common form of loop Figure 2-6 Loop structure Programming Logic and Design, Fifth Edition, Comprehensive

Understanding the Three Basic Structures (continued) Loop structure while testCondition continues to be true do someProcess while quantityInInventory remains low continue to orderItems Programming Logic and Design, Fifth Edition, Comprehensive

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 End-structure statements: indicate the end of a structure The endif statement ends an if-then-else structure The endwhile ends a loop structure Programming Logic and Design, Fifth Edition, Comprehensive

Understanding the Three Basic Structures (continued) Figure 2-7 Structured flowchart and pseudocode Programming Logic and Design, Fifth Edition, Comprehensive

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: group of statements that execute as a single unit Programming Logic and Design, Fifth Edition, Comprehensive

Understanding the Three Basic Structures (continued) Figure 2-8 Flowchart and pseudocode showing a sequence nested within a selection Programming Logic and Design, Fifth Edition, Comprehensive

Understanding the Three Basic Structures (continued) Figure 2-9 Selection in a sequence within a selection Programming Logic and Design, Fifth Edition, Comprehensive

Understanding the Three Basic Structures (continued) Figure 2-10 Flowchart and pseudocode for loop within selection within sequence within selection Programming Logic and Design, Fifth Edition, Comprehensive

Understanding the Three Basic Structures (continued) Each structure has one entry and one exit point Structures attach to others only at entry or exit points Figure 2-11 The three structures Programming Logic and Design, Fifth Edition, Comprehensive

Using the Priming Read Priming read (or priming input): Reads the first input data record Outside the loop that reads the rest of the records Helps keep the program structured 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 Programming Logic and Design, Fifth Edition, Comprehensive

Using the Priming Read (continued) Unstructured loop Figure 2-12 Unstructured flowchart of a number-doubling program Programming Logic and Design, Fifth Edition, Comprehensive

Using the Priming Read (continued) Structured but nonfunctional loop Figure 2-15 Structured, but nonfunctional, flowchart of number-doubling problem Programming Logic and Design, Fifth Edition, Comprehensive

Using the Priming Read (continued) Functional but nonstructured loop Figure 2-16 Functional, but nonstructured, flowchart Programming Logic and Design, Fifth Edition, Comprehensive

Using the Priming Read (continued) Functional and structured loop Figure 2-17 Functional, structured flowchart and pseudocode for the number-doubling problem Programming Logic and Design, Fifth Edition, Comprehensive

Using the Priming Read (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 while not eof calculatedAnswer = inputNumber * 2 print calculatedAnswer endwhile stop Programming Logic and Design, Fifth Edition, Comprehensive

Using the Priming Read (continued) What is wrong with this design? Figure 2-18 Structured but incorrect solution to the number-doubling problem Programming Logic and Design, Fifth Edition, Comprehensive

Understanding the Reasons for Structure Provides clarity Professionalism Efficiency Ease of maintenance Supports modularity Programming Logic and Design, Fifth Edition, Comprehensive

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 Programming Logic and Design, Fifth Edition, Comprehensive

Recognizing Structure (continued) Is this flowchart structured? Figure 2-22 Example 2 Programming Logic and Design, Fifth Edition, Comprehensive

Recognizing Structure (continued) Is this flowchart structured? Figure 2-23 Example 3 Programming Logic and Design, Fifth Edition, Comprehensive

Recognizing Structure (continued) Single process like A is part of an unacceptable structure At least the beginning of a sequence structure Figure 2-24 Untangling Example 3, first step Programming Logic and Design, Fifth Edition, Comprehensive

Recognizing Structure (continued) B begins a selection structure Sequences never have decisions in them Logic never returns to B Figure 2-25 Untangling Example 3, second step Programming Logic and Design, Fifth Edition, Comprehensive

Recognizing Structure (continued) Pull up on the flowline from the left side of B Figure 2-26 Untangling Example 3, third step Programming Logic and Design, Fifth Edition, Comprehensive

Recognizing Structure (continued) Next, pull up the flowline on the right side of B Figure 2-27 Untangling Example 3, fourth step Programming Logic and Design, Fifth Edition, Comprehensive

Recognizing Structure (continued) Pull up the flowline on the left side of D and untangle it from the B selection by repeating C Figure 2-28 Untangling Example 3, fifth step Programming Logic and Design, Fifth Edition, Comprehensive

Recognizing Structure (continued) Now pull up the flowline on the right side of D Figure 2-29 Untangling Example 3, sixth step Programming Logic and Design, Fifth Edition, Comprehensive

Recognizing Structure (continued) Bring together the loose ends of D and of B Figure 2-30 Finished flowchart and pseudocode for untangling Example 3 Programming Logic and Design, Fifth Edition, Comprehensive

Three Special Structures – CASE, DO-WHILE, and DO-UNTIL Many languages allow three additional structures: The case structure The do-while structure The do-until structure CASE Structure: Decisions with more than two alternatives Tests a variable against a series of values and takes action based on a match Nested if-then-else statements will do what a case structure does Programming Logic and Design, Fifth Edition, Comprehensive

Three Special Structures – Case, Do While, and Do Until (continued) Using nested if-then-else for multiple alternatives Figure 2-31 Flowchart and pseudocode of tuition decisions Programming Logic and Design, Fifth Edition, Comprehensive

Three Special Structures – Case, Do While, and Do Until (continued) Using a case structure for multiple alternatives Figure 2-32 Flowchart and pseudocode of case structure Programming Logic and Design, Fifth Edition, Comprehensive

Three Special Structures – Case, Do While, and Do Until (continued) do-while and do-until loops Question is asked at the end of the loop structure Loop statements always used at least once Figure 2-33 The while loop, which is a pretest loop Figure 2-34 Structure of a do-while or do-until loop, which are posttest loops Programming Logic and Design, Fifth Edition, Comprehensive

Three Special Structures – Case, Do While, and Do Until (continued) do-while: executes as long as the question’s answer is Yes or True do-until: executes as long as the question’s answer is No or False do wash a dish until all dishes are washed do wash a dish while more dishes remain to be washed Programming Logic and Design, Fifth Edition, Comprehensive

Three Special Structures – Case, Do While, and Do Until (continued) while loop with question at beginning is called a pretest loop do-while and do-until with question at end are called posttest loops Posttest loop can be replaced with a sequence followed by a pretest while loop pay a bill while there are more bills to pay endwhile Programming Logic and Design, Fifth Edition, Comprehensive

Three Special Structures – Case, Do While, and Do Until (continued) Figure 2-35 Flowchart and pseudocode for do-while loop Programming Logic and Design, Fifth Edition, Comprehensive

Three Special Structures – Case, Do While, and Do Until (continued) Figure 2-36 Flowchart and pseudocode for sequence followed by while loop Programming Logic and Design, Fifth Edition, Comprehensive

Three Special Structures – Case, Do While, and Do Until (continued) How can this design be made structured? Figure 2-37 Unstructured loop Programming Logic and Design, Fifth Edition, Comprehensive

Three Special Structures – Case, Do While, and Do Until (continued) Repeat the needed step to enforce structure Figure 2-38 Sequence and structured loop that accomplish the same tasks as Figure 2-37 Programming Logic and Design, Fifth Edition, Comprehensive

Summary Spaghetti code: snarled program logic Three basic structures: sequence, selection, loop Combined by stacking and nesting Priming read: statement that reads the first input data record Structured techniques promote clarity, professionalism, efficiency, and modularity Flowchart can be made structured by untangling Programming Logic and Design, Fifth Edition, Comprehensive

Summary (continued) case structure: questions with multiple alternatives while loop: a pretest loop asks the question first while loop statements never execute if the answer is No do-while and do-until loops: posttest loops that ask the question last do-while and do-until loop statements are always executed at least once Posttest loop can be replaced by a sequence followed by a while loop Programming Logic and Design, Fifth Edition, Comprehensive