A Beginner’s Guide to Programming Logic, Introductory

Slides:



Advertisements
Similar presentations
Chapter 2: Understanding Structure
Advertisements

Repetition Control Structures School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 9, Friday 3/07/2003)
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
Programming Logic and Design, Third Edition Comprehensive
Programming Logic and Design Seventh Edition
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
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Chapter 2: Algorithm Discovery and Design
Program Design and Development
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Design the program Create a detailed description of program –Use charts or ordinary language (pseudocode) Identify algorithms needed –Algorithm: a step-by-step.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
Selection Control Structures Simple Program Design Third Edition A Step-by-Step Approach 4.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Programming Logic and Design Fifth Edition, Comprehensive
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
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.
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.
Programming Structure
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
CHAPTER 2: Understanding Structure. Objectives 2  Learn about the features of unstructured spaghetti code  Understand the three basic structures: sequence,
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
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.
Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will.
Programming Logic and Design Fifth Edition, Comprehensive
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.
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.
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 Fourth Edition, Introductory Chapter 2 Understanding Structure.
An Introduction to Programming with C++ Sixth Edition Chapter 8 More on the Repetition Structure.
Structured Programming The Basics
Programming Logic and Design Seventh Edition
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
Programming Logic and Design Fourth Edition, Comprehensive
Programming Logic and Design Eighth Edition
Chapter 5: Repetition Structures
( Iteration / Repetition / Looping )
Programming Fundamentals
Using the Priming Read Priming read (or priming input):
Understanding the Reasons for Structure
Introduction to pseudocode
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Understanding the Three Basic Structures
Chapter 2- Visual Basic Schneider
Chapter 8: More on the Repetition Structure
Chapter 2- Visual Basic Schneider
Boolean Expressions to Make Comparisons
Lecture 7 Algorithm Design & Implementation. All problems can be solved by employing any one of the following building blocks or their combinations 1.
Chapter 4 - Program Control
Presentation transcript:

A Beginner’s Guide to Programming Logic, Introductory Chapter 3 Understanding Structure

Objectives In this chapter, you will learn about: The features of unstructured spaghetti code The three basic structures—sequence, selection, and loop Using a priming input to structure a program The need for structure Recognizing structure Structuring and modularizing unstructured logic A Beginner's Guide to Programming Logic, Introductory

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 A Beginner's Guide to Programming Logic, Introductory

Figure 3-1 Spaghetti code logic for washing a dog A Beginner's Guide to Programming Logic, Introductory

Understanding the Three Basic Structures Basic units of programming logic 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 ifs Loop Repeat actions based on answer to a question A Beginner's Guide to Programming Logic, Introductory

Understanding the Three Basic Structures (continued) Figure 3-2 Sequence structure A Beginner's Guide to Programming Logic, Introductory

Understanding the Three Basic Structures (continued) Figure 3-3 Selection structure A Beginner's Guide to Programming Logic, Introductory

Understanding the Three Basic Structures (continued) Dual-alternative if Contains two alternatives If-then-else structure if someCondition is true then do oneProcess else do theOtherProcess A Beginner's Guide to Programming Logic, Introductory

Understanding the Three Basic Structures (continued) Single-alternative if An else clause is not required null case Situation where nothing is done if employee belongs to dentalPlan then deduct $40 from employeeGrossPay A Beginner's Guide to Programming Logic, Introductory

Understanding the Three Basic Structures (continued) Figure 3-4 Single-alternative selection structure A Beginner's Guide to Programming Logic, Introductory

Understanding the Three Basic Structures (continued) Loop structure Repeats a set of actions based on the answer to a question (the condition of the loop) Loop body Also called repetition or iteration Question is asked first (the condition is tested first) in the most common form of loop while … do or while loop A Beginner's Guide to Programming Logic, Introductory

Understanding the Three Basic Structures (continued) Figure 3-5 Loop structure A Beginner's Guide to Programming Logic, Introductory

Understanding the Three Basic Structures (continued) Loop structure while testCondition continues to be true do someProcess while quantityInInventory remains low continue to orderItems A Beginner's Guide to Programming Logic, Introductory

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 Pseudocode uses end-structure statements Indicate the end of a structure The endif statement ends an if-then-else structure The endwhile ends a loop structure A Beginner's Guide to Programming Logic, Introductory

Understanding the Three Basic Structures (continued) Figure 3-6 Structured flowchart and pseudocode with three stacked structures A Beginner's Guide to Programming Logic, Introductory

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 A Beginner's Guide to Programming Logic, Introductory

Understanding the Three Basic Structures (continued) Figure 3-7 Flowchart and pseudocode showing nested structures—a sequence nested within a selection A Beginner's Guide to Programming Logic, Introductory

Understanding the Three Basic Structures (continued) Figure 3-8 Flowchart and pseudocode showing nested structures—a loop nested within a sequence, nested within a selection A Beginner's Guide to Programming Logic, Introductory

Understanding the Three Basic Structures (continued) Figure 3-9 Flowchart and pseudocode for selection within loop within sequence within selection A Beginner's Guide to Programming Logic, Introductory

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 Any structure can be nested within another structure A Beginner's Guide to Programming Logic, Introductory

Using a Priming Input to Structure a Program 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 A Beginner's Guide to Programming Logic, Introductory

Using a Priming Input to Structure a Program (continued) Figure 3-15 Structured, but nonfunctional, flowchart of number-doubling problem A Beginner's Guide to Programming Logic, Introductory

Using a Priming Input to Structure a Program (continued) Figure 3-16 Functional but unstructured flowchart A Beginner's Guide to Programming Logic, Introductory

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 while not eof calculatedAnswer = inputNumber * 2 print calculatedAnswer endwhile stop A Beginner's Guide to Programming Logic, Introductory

Figure 3-17 Functional, structured flowchart and pseudocode for the number-doubling problem A Beginner's Guide to Programming Logic, Introductory

Figure 3-18 Structured but incorrect solution to the number-doubling problem A Beginner's Guide to Programming Logic, Introductory

Understanding the Reasons for Structure Clarity – unstructured programs are confusing Professionalism – other programmers expect it Efficiency – most languages support it Ease of maintenance – other programmers find it easier to read Supports modularity – easily broken down into modules A Beginner's Guide to Programming Logic, Introductory

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 A Beginner's Guide to Programming Logic, Introductory

Recognizing Structure (continued) Figure 3-20 Example 2 A Beginner's Guide to Programming Logic, Introductory

Recognizing Structure (continued) Figure 3-21 Example 3 A Beginner's Guide to Programming Logic, Introductory

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 A Beginner's Guide to Programming Logic, Introductory

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 A Beginner's Guide to Programming Logic, Introductory

Recognizing Structure (continued) Pull up on the flowline from the left side of H Figure 3-24 Untangling Example 3, third step A Beginner's Guide to Programming Logic, Introductory

Recognizing Structure (continued) Next, pull up the flowline on the right side of H Figure 3-25 Untangling Example 3, fourth step A Beginner's Guide to Programming Logic, Introductory

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 A Beginner's Guide to Programming Logic, Introductory

Recognizing Structure (continued) Now pull up the flowline on the right side of I Figure 3-27 Untangling Example 3, sixth step A Beginner's Guide to Programming Logic, Introductory

Recognizing Structure (continued) Bring together the loose ends of I and of H Figure 3-28 Finished flowchart and pseudocode for untangling Example 3 A Beginner's Guide to Programming Logic, Introductory

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 A Beginner's Guide to Programming Logic, Introductory

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 A Beginner's Guide to Programming Logic, Introductory

Figure 3-31 Washing the dog, part 3 A Beginner's Guide to Programming Logic, Introductory

Figure 3-33 Structured dog-washing flowchart and pseudocode A Beginner's Guide to Programming Logic, Introductory

Figure 3-34 Modularized version of the dog-washing program A Beginner's Guide to Programming Logic, Introductory

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 A Beginner's Guide to Programming Logic, Introductory

Summary (continued) Structured techniques promote: Clarity Professionalism Efficiency Modularity Flowchart can be made structured by untangling A Beginner's Guide to Programming Logic, Introductory