You are on page 1of 26

An Introduction to Programming Structure

Chapter 4

Sprankle/Hubbard Problem Solving and Programming Concepts, 8e

Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

Objectives
1. Explain the need for structured programming. 2. Explain how to design modules and functions in terms of cohesion and coupling. 3. Explain the difference between local and global variables. 4. Explain the use of parameters. 5. List and describe the four logic structures: sequential, decision, loops, and case.

Sprankle/Hubbard Problem Solving and Programming Concepts, 8e

Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

Points for Structuring a Solution


1. Use modules break the whole into parts, with each part having a particular function. 2. Use the four logic structures to ensure that the solution flows smoothly from one instruction to the next, rather than jumping from one point in the solution to another
1. 2. 3. 4. The sequential structure executes instructions one after another in a sequence The decision structure branches to execute one of two possible sets of instructions The loop structure executes a set of instructions many times The case structure executes one set of instructions out of several sets
Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

Sprankle/Hubbard Problem Solving and Programming Concepts, 8e

Points for Structuring a Solution


3. Eliminate the rewriting of identical processes by using modules 4. Use techniques to improve readability, including the four logic structures, proper naming of variables, internal documentation, and proper documentation

Sprankle/Hubbard Problem Solving and Programming Concepts, 8e

Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

Figure 4.1 Sequential Logic Structure

Sprankle/Hubbard Problem Solving and Programming Concepts, 8e

Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

Figure 4.2 Decision Logic Structure

Sprankle/Hubbard Problem Solving and Programming Concepts, 8e

Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

Figure 4.3 Loop Logic Structure

Sprankle/Hubbard Problem Solving and Programming Concepts, 8e

Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

Figure 4.4 Case Logic Structure

Sprankle/Hubbard Problem Solving and Programming Concepts, 8e

Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

Rules for designing modules


1. Each module is an entity in itself. There is one entrance and one exit. Processing does not jump out of the middle of a module to another module, nor does it jump into the middle of a module. 2. Each module has a single function, such as printing, calculating, or entering data 3. Each module is short enough to be easily read and modified 4. The length of a module is governed by its function and the number of instructions to be executed to perform that function 5. A module is developed to control the order of processing
Sprankle/Hubbard Problem Solving and Programming Concepts, 8e Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

Modules Needed for Solutions to Most Problems 1. Control module 2. Initialization module 3. Process module
1. Calculation module 2. Print module 3. Read module

4. Data validation module 5. Wrap-up module 6. Event modules in object-oriented programming


Sprankle/Hubbard Problem Solving and Programming Concepts, 8e Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

Cohesion and Coupling


1. Cohesion is the ability for a module to work independently from all other modules. 2. To work independently, each module should have a single entry and single exit. 3. However, modules need to share data from other modules in order to complete the modular tasks. 4. This sharing of data is called coupling. 5. Coupling is accomplished by some type of interface between modules that enables data to be passed from one module to another with the minimum interruption of modular independence. 6. Coupling allows for the communication between modules 7. There are 3 ways to couple modules: Global variables, parameters and return values
Sprankle/Hubbard Problem Solving and Programming Concepts, 8e Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

Figure 4.5 Cohesion and Coupling

Sprankle/Hubbard Problem Solving and Programming Concepts, 8e

Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

Figure 4.6 Interactivity Chart

Sprankle/Hubbard Problem Solving and Programming Concepts, 8e

Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

Figure 4.7 Scope of Local and Global Variables

Sprankle/Hubbard Problem Solving and Programming Concepts, 8e

Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

Figure 4.8 Parameter Terminology

Sprankle/Hubbard Problem Solving and Programming Concepts, 8e

Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

Figure 4.9 Parameters: Call-by-Value and Call-by-Reference

Sprankle/Hubbard Problem Solving and Programming Concepts, 8e

Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

Figure 4.9 Parameters: Call-by-Value and Call-by-Reference

Sprankle/Hubbard Problem Solving and Programming Concepts, 8e

Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

Figure 4.9 Parameters: Call-by-Value and Call-by-Reference

Sprankle/Hubbard Problem Solving and Programming Concepts, 8e

Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

Figure 4.10 Parameter Example

Sprankle/Hubbard Problem Solving and Programming Concepts, 8e

Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

Figure 4.10 Parameter Example

Sprankle/Hubbard Problem Solving and Programming Concepts, 8e

Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

Figure 4.10 Parameter Example

Sprankle/Hubbard Problem Solving and Programming Concepts, 8e

Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

Figure 4.10 Parameter Example

Sprankle/Hubbard Problem Solving and Programming Concepts, 8e

Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

Figure 4.11 Coupling Diagram for Figures 4.8 and 4.9

Sprankle/Hubbard Problem Solving and Programming Concepts, 8e

Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

Figure 4.12 Coupling Diagram for Figure 4.10

Sprankle/Hubbard Problem Solving and Programming Concepts, 8e

Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

Figure 4.13 Return Value

Sprankle/Hubbard Problem Solving and Programming Concepts, 8e

Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

Figure 4.14 Example of a Data Dictionary

Sprankle/Hubbard Problem Solving and Programming Concepts, 8e

Copyright 2009 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.

You might also like