Chapter 2: Understanding Structure

Slides:



Advertisements
Similar presentations
COMPUTER PROGRAMMING I Understand Problem Solving Tools to Design Programming Solutions.
Advertisements

Programming Logic and Design Eighth Edition
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, Third Edition Comprehensive
Programming Logic and Design Seventh Edition
Programming Logic and Design Fourth Edition, Introductory
Chapter 2: Algorithm Discovery and Design
Chapter 5: Control Structures II (Repetition)
Writing a Complete Program
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
(C)opyright 2003 Scott/Jones Publishers Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Scott/Jones Publishers.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
Chapter 5: Control Structures II (Repetition)
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Chapter 12: How Long Can This Go On?
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.
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.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Programming Structure
`. 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,
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
(C)opyright 2000 Scott/Jones Publishers Introduction to Flowcharting.
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.
Chapter 13 Do It, Then Ask Permission (Posttest Loops) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Programming Logic and Design Fifth Edition, Comprehensive
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
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!
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.
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
Programming Logic and Design Fourth Edition, Introductory Chapter 2 Understanding Structure.
ALGORITHMS AND FLOWCHARTS
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
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
Writing a Complete Program
Flow of Control.
Topics Introduction to Repetition Structures
WRITING AN ALGORITHM, PSEUDOCODE, AND FLOWCHART LESSON 2.
Chapter 4: Writing and Designing a Complete Program
Presentation transcript:

Chapter 2: Understanding Structure Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Objectives After studying Chapter 2, you should be able to: Describe the features of unstructured spaghetti code Describe the three basic structures of sequence, selection, and loop Use a priming read Appreciate the need for structure Recognize structure Describe two special structures—case and do until Programming Logic and Design, Third Edition Comprehensive

Understanding Unstructured Spaghetti Code The popular name for snarled program statements is spaghetti code The reason for the name should be obvious—the code is as confusing to read as following one noodle through a plate of spaghetti Programming Logic and Design, Third Edition Comprehensive

Understanding the Three Basic Structures A structure is a basic unit of programming logic; each structure is a sequence, selection, or loop The first of these structures is a sequence, as shown in Figure 2-3 With a sequence structure, you perform an action or event, and then you perform the next action, in order Programming Logic and Design, Third Edition Comprehensive

Understanding the Three Basic Structures (continued) The second structure is called a selection structure or decision structure You ask a question depending on the answer, you take one of two courses of action no matter which path you follow, you continue with the next event Programming Logic and Design, Third Edition Comprehensive

Understanding the Three Basic Structures (continued) Programming Logic and Design, Third Edition Comprehensive

Understanding the Three Basic Structures (continued) The selection structure is sometimes called an if-then-else because it fits the following statement: if someCondition is true then do oneProcess else do theOtherProcess Programming Logic and Design, Third Edition Comprehensive

Understanding the Three Basic Structures (continued) Programming Logic and Design, Third Edition Comprehensive

Understanding the Three Basic Structures (continued) In a loop structure, you ask a question; if the answer requires an action, you perform the action and ask the original question again If the answer requires that the action be taken again, you take the action and then ask the original question again Continues until the answer to the question is such that the action is no longer required; then you exit the structure Programming Logic and Design, Third Edition Comprehensive

Understanding the Three Basic Structures (continued) You may hear programmers refer to looping as repetition or iteration Programming Logic and Design, Third Edition Comprehensive

Understanding the Three Basic Structures (continued) All logic problems can be solved using only these three structures—sequence, selection, and looping The three structures can be combined in an infinite number of ways Attaching structures end-to-end is called stacking structures Programming Logic and Design, Third Edition Comprehensive

Understanding the Three Basic Structures (continued) Programming Logic and Design, Third Edition Comprehensive

Understanding the Three Basic Structures (continued) Placing a structure within another structure is called nesting the structures Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Using the Priming Read A priming read or priming input is the first read or data input statement in a program If a program will read 100 data records, you read the first data record in a statement that is separate from the other 99 You must do this to keep the program structured With a selection structure, the logic goes in one of two directions after the question, and then the flow comes back together; the question is not asked a second time Programming Logic and Design, Third Edition Comprehensive

Using the Priming Read (continued) In a loop, if an answer results in the loop being entered and loop statements executing, then the logic returns to the question that started the loop when the body of a loop executes, the question that controls the loop is always asked again Programming Logic and Design, Third Edition Comprehensive

Using the Priming Read (continued) Programming Logic and Design, Third Edition Comprehensive

Using the Priming Read (continued) Programming Logic and Design, Third Edition Comprehensive

Using the Priming Read (continued) Programming Logic and Design, Third Edition Comprehensive

Using the Priming Read (continued) Programming Logic and Design, Third Edition Comprehensive

Using the Priming Read (continued) Programming Logic and Design, Third Edition Comprehensive

Using the Priming Read (continued) Programming Logic and Design, Third Edition Comprehensive

Using the Priming Read (continued) Programming Logic and Design, Third Edition Comprehensive

Understanding the Reasons for Structure Until you have some programming experience, it might be difficult to appreciate the reasons for using only the three structures—sequence, selection, and loop However, staying with these three structures is better for the following reasons: Clarity – Professionalism Efficiency – Maintenance Modularity Programming Logic and Design, Third Edition Comprehensive

Flowchart and Pseudocode of Structured College Admission Program Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Flowchart and Pseudocode of Structured College Admission Program (continued) Figure 2-19 (continued) Programming Logic and Design, Third Edition Comprehensive

Recognizing Structure Any set of instructions can be expressed in a structured format If you can teach someone how to perform any ordinary activity, then you can express it in a structured way Programming Logic and Design, Third Edition Comprehensive

Pseudocode for the Rock, Paper, Scissors Game Any task with applied rules can be expressed logically using only combinations of sequence, selection, and looping Programming Logic and Design, Third Edition Comprehensive

Two Special Structures—Case and Do Until Many programming languages allow two more structures: the case structure and the do until loop Never needed to solve any problem though sometimes are convenient Programmers consider them both to be acceptable, legal structures Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive The Case Structure Use the case structure when there are several distinct possible values for a single variable being tested and each value requires different actions Programming Logic and Design, Third Edition Comprehensive

The Case Structure (continued) Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive The Do Until Loop In a do while loop, you ask a question and, depending on the answer, you might or might not enter the loop to execute the loop’s procedure Conversely, in a do until loop, you ensure that the procedure executes at least once; then, depending on the answer to the controlling question, the loop may or may not execute additional times Programming Logic and Design, Third Edition Comprehensive

The Do Until Loop (continued) Because programmers understand that a do until can be expressed with a sequence followed by a do while, most languages allow the do until Again, you are never required to use a do until; you can always accomplish the same events with a sequence followed by a do while Programming Logic and Design, Third Edition Comprehensive

The Do Until Loop (continued) Programming Logic and Design, Third Edition Comprehensive

The Do Until Loop (continued) Programming Logic and Design, Third Edition Comprehensive

The Do Until Loop (continued) Programming Logic and Design, Third Edition Comprehensive

The Do Until Loop (continued) An unstructured loop is neither a do while loop (beginning with a decision and, after an action, returning to the decision), nor a do until loop (beginning with an action and ending with a decision that might repeat the action) Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Sequence and Structured Loop that Accomplish the Same Tasks as Figure 2-37 Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Summary The popular name for snarled program statements is spaghetti code A priming read or priming input is the first read or data input statement prior to beginning a structured loop The last step within the loop gets the next, and all subsequent, input values Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Summary (continued) You can use a case structure when there are several distinct possible values for a variable you are testing In a do while loop, you ask a question and, depending on the answer, you might never enter the loop to execute the loop’s procedure In a do until loop, you ensure that the procedure executes at least once Programming Logic and Design, Third Edition Comprehensive