Programming Logic and Design, Introductory, Fourth Edition1 Understanding the Three Basic Structures Structure: a basic unit of programming logic Any program.

Slides:



Advertisements
Similar presentations
More on Algorithms and Problem Solving
Advertisements

Chapter 2: Understanding Structure
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
Selection (decision) control structure Learning objective
Understanding the Three Basic Structures
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture two Dr. Hamdy M. Mousa.
Lesson 5 - Decision Structure By: Dan Lunney
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 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.
(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
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
Programming Logic and Design Sixth Edition
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Chapter 3 Making Decisions
Problem Solving with Decisions
Programming Logic and Design Fifth Edition, Comprehensive
S2008Final_part1.ppt CS11 Introduction to Programming Final Exam Part 1 S A computer is a mechanical or electrical device which stores, retrieves,
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
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.
The world of Constructs Control Structures. The three Structures Sequence Selection Loop Entry Exit.
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.
2 Chapter 21 Understanding Structure Programming Logic and Design, Second Edition, Comprehensive 2.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Programming Logic and Design Fifth Edition, Comprehensive
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.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
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.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 Making Decisions.
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
Programming Fundamentals
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
Three Special Structures – Case, Do While, and Do Until
ICT Programming Lesson 3:
REPETITION Why Repetition?
Presentation transcript:

Programming Logic and Design, Introductory, Fourth Edition1 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 Edition2 Understanding the Three Basic Structures (continued) Sequence structure –A set of instructions, performed sequentially with no branching

Programming Logic and Design, Introductory, Fourth Edition3 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 Edition4 Understanding the Three Basic Structures (continued) Dual-alternative if: contains two alternatives IF the hours worked is more than 40 THEN (question) total pay will be = regular pay amount plus overtime hours multiplied by 1 ½ times regular pay amount (action if true) ELSE total pay is regular hours times regular pay amount (action if false) END IF If the hours an employee has worked is greater than 40 hours then calculate their pay as regular hours multiplied by their regular time pay mount added to the overtime pay amount which is overtime hours multiplied by 1 ½ time the regular pay amount. The Problem Pauedocode Flowchart

Programming Logic and Design, Introductory, Fourth Edition5 Understanding the Three Basic Structures (continued) Single-alternative if: contains one alternative If the hours an employee has worked is greater than 40 hours then calculate their pay as regular hours multiplied by their regular time pay mount added to the overtime pay amount which is overtime hours multiplied by 1 ½ time the regular pay amount. Total pay = regular hours multiplied by regular pay IF the hours worked is more than 40 THEN (question) total pay will be = total pay amount plus overtime hours multiplied by 1 ½ times regular pay amount (action if true) END IF Print to printer the total pay amount (action if true or false) The Problem Pauedocode Flowchart Question TRUE path TRUE or FALSE path FALSE path

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

Programming Logic and Design, Introductory, Fourth Edition7 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 Edition8 Understanding the Three Basic Structures (continued) Loop structure WHILE testcondition (check if testcondition is true) do however many instructions are required (testcondition is true) END LOOP (end of loop – go back to beginning and check condition) Continue with whatever processing is necessary Question TRUE (repeat) FALSE Check Condition here or here DO WHILE or DO UNTIL

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

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

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

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

Programming Logic and Design, Introductory, Fourth Edition15 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 Edition16 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 Edition17 Using the Priming Read (continued) Unstructured loop:

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

Programming Logic and Design, Introductory, Fourth Edition19 Using the Priming Read (continued) Corrrect

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

Programming Logic and Design, Introductory, Fourth Edition21 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 Edition22 Using the Priming Read (continued) What is wrong with this design?

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

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

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

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

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

Programming Logic and Design, Introductory, Fourth Edition28 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 Edition29 Three Special Structures – Case, Do While, and Do Until (continued) Using nested if-then-else for multiple alternatives

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

Programming Logic and Design, Introductory, Fourth Edition31 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 Edition32 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 Test checked at beginning May not be executed do-until loop executes as long as the question’s answer is No or False (until it becomes Yes or True) Test checked at end of loop Will always execute loop at least once

Programming Logic and Design, Introductory, Fourth Edition33 Three Special Structures – Case, Do While, and Do Until (continued) while loop with question at beginning is called a pretest loop do-until with question at end are called posttest loops

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

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