Programming Logic and Design Fourth Edition, Introductory Chapter 2 Understanding Structure.

Slides:



Advertisements
Similar presentations
More on Algorithms and Problem Solving
Advertisements

Chapter 2: Understanding Structure
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
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
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 2 The Algorithmic Foundations of Computer Science
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
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
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
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 Fourth Edition, Comprehensive Chapter 6 Looping.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
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.
Invitation to Computer Science 5 th Edition Chapter 2 The Algorithmic Foundations of Computer Science.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Programming Logic and Design Fifth Edition, Comprehensive
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
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.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Programming Logic and Design Seventh Edition
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II (Repetition)
Programming Logic and Design Fourth Edition, Comprehensive
Programming Logic and Design Eighth Edition
Control Structures II (Repetition)
Chapter 5: Repetition Structures
( Iteration / Repetition / Looping )
Programming Fundamentals
Using the Priming Read Priming read (or priming input):
A Beginner’s Guide to Programming Logic, Introductory
Understanding the Reasons for Structure
Introduction to pseudocode
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Understanding the Three Basic Structures
Iteration Implemented through loop constructs (e.g., in C++)
Chapter 8: More on the Repetition Structure
Three Special Structures – Case, Do While, and Do Until
ICT Programming Lesson 3:
Boolean Expressions to Make Comparisons
Class 4: Repetition Pretest Posttest Counting Flowchart these!
The structure of programming
Thinking procedurally
Presentation transcript:

Programming Logic and Design Fourth Edition, Introductory Chapter 2 Understanding Structure

Programming Logic and Design, Introductory, Fourth Edition2 Objectives Describe the features of unstructured spaghetti code Describe the three basic structures – sequence, selection, and loop Use a priming read Appreciate the need for structure Recognize structure

Programming Logic and Design, Introductory, Fourth Edition3 Objectives (continued) Describe three special structures – case, do-while, and do-until

Programming Logic and Design, Introductory, Fourth Edition4 Understanding Unstructured Spaghetti Code Spaghetti code – logically snarled program statements Can be the result of poor program design Example: college admissions criteria

Programming Logic and Design, Introductory, Fourth Edition5 Understanding Unstructured Spaghetti Code (continued)

Programming Logic and Design, Introductory, Fourth Edition6 Understanding Unstructured Spaghetti Code (continued) Spaghetti code programs often work, but are difficult to read and maintain Convoluted logic usually requires more code

Programming Logic and Design, Introductory, Fourth Edition7 Understanding the Three Basic Structures Structure: a basic unit of programming logic Any program can be constructed from only three basic types of structures –Sequence –Selection –Loop

Programming Logic and Design, Introductory, Fourth Edition8 Understanding the Three Basic Structures (continued) Sequence structure –A set of instructions, performed sequentially with no branching

Programming Logic and Design, Introductory, Fourth Edition9 Understanding the Three Basic Structures (continued) Selection structure –Asks a question, then takes one of two possible courses of action based on the answer –Also called a decision structure or an if-then-else

Programming Logic and Design, Introductory, Fourth Edition10 Understanding the Three Basic Structures (continued) Dual-alternative if: contains two alternatives

Programming Logic and Design, Introductory, Fourth Edition11 Understanding the Three Basic Structures (continued) Single-alternative if: contains one alternative

Programming Logic and Design, Introductory, Fourth Edition12 Understanding the Three Basic Structures (continued) Single-alternative if Else clause is not required Null case: situation where nothing is done

Programming Logic and Design, Introductory, Fourth Edition13 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

Programming Logic and Design, Introductory, Fourth Edition14 Understanding the Three Basic Structures (continued) Loop structure

Programming Logic and Design, Introductory, Fourth Edition15 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 –endif : ends an if-then-else structure –endwhile : ends a loop structure

Programming Logic and Design, Introductory, Fourth Edition16 Understanding the Three Basic Structures (continued)

Programming Logic and Design, Introductory, Fourth Edition17 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, Introductory, Fourth Edition18 Understanding the Three Basic Structures (continued)

Programming Logic and Design, Introductory, Fourth Edition19 Understanding the Three Basic Structures (continued)

Programming Logic and Design, Introductory, Fourth Edition20 Understanding the Three Basic Structures (continued)

Programming Logic and Design, Introductory, Fourth Edition21 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

Programming Logic and Design, Introductory, Fourth Edition22 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: 1.First ask a question 2.Take action based on the answer 3.Return to ask the question again

Programming Logic and Design, Introductory, Fourth Edition23 Using the Priming Read (continued) Unstructured loop:

Programming Logic and Design, Introductory, Fourth Edition24 Using the Priming Read (continued) Structured but nonfunctional loop

Programming Logic and Design, Introductory, Fourth Edition25 Using the Priming Read (continued) Functional but non-structured loop

Programming Logic and Design, Introductory, Fourth Edition26 Using the Priming Read (continued) Functional and structured loop

Programming Logic and Design, Introductory, Fourth Edition27 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

Programming Logic and Design, Introductory, Fourth Edition28 Using the Priming Read (continued) What is wrong with this design?

Programming Logic and Design, Introductory, Fourth Edition29 Understanding the Reasons for Structure Advantages of structure: –Provides clarity –Professionalism –Efficiency –Ease of maintenance –Supports modularity

Programming Logic and Design, Introductory, Fourth Edition30 Understanding the Reasons for Structure (continued)

Programming Logic and Design, Introductory, Fourth Edition31 Understanding the Reasons for Structure (continued)

Programming Logic and Design, Introductory, Fourth Edition32 Recognizing Structure Any set of instructions can be expressed in structured format Is this flowchart structured?

Programming Logic and Design, Introductory, Fourth Edition33 Recognizing Structure (continued) Is this flowchart structured?

Programming Logic and Design, Introductory, Fourth Edition34 Recognizing Structure (continued) To make it structured, pull each symbol out and rework B begins a selection structure

Programming Logic and Design, Introductory, Fourth Edition35 Recognizing Structure (continued) Pull up on the flowline from the left side of B

Programming Logic and Design, Introductory, Fourth Edition36 Recognizing Structure (continued) Next, pull up the flowline on the right side of B

Programming Logic and Design, Introductory, Fourth Edition37 Recognizing Structure (continued) Pull up the flowline on the left side of D and untangle it from the B selection by repeating C

Programming Logic and Design, Introductory, Fourth Edition38 Recognizing Structure (continued) Now pull up the flowline on the right side of D

Programming Logic and Design, Introductory, Fourth Edition39 Recognizing Structure (continued) Bring together the loose ends of D and of B

Programming Logic and Design, Introductory, Fourth Edition40 Three Special Structures – Case, Do While, and Do Until Many languages allow three additional structures: –case structure –do-while structure –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, Introductory, Fourth Edition41 Three Special Structures – Case, Do While, and Do Until (continued) Using nested if-then-else for multiple alternatives

Programming Logic and Design, Introductory, Fourth Edition42 Three Special Structures – Case, Do While, and Do Until (continued) Using a case structure for multiple alternatives

Programming Logic and Design, Introductory, Fourth Edition43 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 –Ensures that the loop statements are always used at least once

Programming Logic and Design, Introductory, Fourth Edition44 Three Special Structures – Case, Do While, and Do Until (continued) do-while loop executes as long as the question’s answer is Yes or True do-until loop executes as long as the question’s answer is No or False (until it becomes Yes or True)

Programming Logic and Design, Introductory, Fourth Edition45 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 A posttest loop can be replaced with a sequence followed by a pretest while loop

Programming Logic and Design, Introductory, Fourth Edition46 Three Special Structures – Case, Do While, and Do Until (continued)

Programming Logic and Design, Introductory, Fourth Edition47 Three Special Structures – Case, Do While, and Do Until (continued)

Programming Logic and Design, Introductory, Fourth Edition48 Three Special Structures – Case, Do While, and Do Until (continued) How can this design be made structured?

Programming Logic and Design, Introductory, Fourth Edition49 Three Special Structures – Case, Do While, and Do Until (continued) Repeat the needed step to enforce structure

Programming Logic and Design, Introductory, Fourth Edition50 Summary Spaghetti code: snarled program logic Three basic structures: sequence, selection, loop These three can be combined by stacking and nesting Priming read: statement that reads the first input data record Structured techniques promote clarity, professionalism, efficiency, and modularity

Programming Logic and Design, Introductory, Fourth Edition51 Summary (continued) Flowchart can be made structured by untangling case structure: handles questions with multiple alternatives while loop: a pretest loop that asks the question first while loop statements may never be executed if the answer is No do-while and do-until loops: posttest loops that ask the question last

Programming Logic and Design, Introductory, Fourth Edition52 Summary (continued) 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