INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

48
INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1

Transcript of INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Page 1: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

INTRODUCTION TO PROGRAMMING

STRUCTURE

Chapter 4

1

Page 2: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Overview2

Pointers Modules & Functions Cohesion & Coupling Local & Global Variables Parameters Variable Names & Data Dictionaries Three Logic Structures

Page 3: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Pointers for Structuring a program

3

Use Modules Each part should have a particular function

Use the four logic structures Sequential , Decision , loop & case

Eliminate the rewriting of identical processes by using modules .

Use techniques to improve readability.

Page 4: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

4

- The computer should be a tool to help people find

solutions to their problems and to increase their

productivity.

- You can develop efficient computer solutions to

problems if you heed the following pointers:

1- Use modules. Break the whole into parts, each part

having a particular function.

Pointers for Structuring a program

Page 5: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

5

2- Use the four logic structures to ensure that solution flows smoothly from one instruction to the next, rather than jumping from one point in the solution to another.

a- The sequential structure executes instructions one

after another in a sequence.

b- The decision structure branches to execute one of

two possible sets of instructions.

c- The loop structure executes a set of instructions

many times.

d- The case structure executes one set of instructions

out of several sets.

Pointers for Structuring a program

Page 6: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

6

Sequential Logic Structure

Page 7: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

7

Decision Logic Structure

Page 8: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

8

Loop Logic Structure

Page 9: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

9

Case Logic Structure

Page 10: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

10

Page 11: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

rules for designing modules Each module

is an entity by itself . There is one entrance and one exit , the processing does not jump out of the middle of a module to another module.

has a single function ( printing , calculating , entering data)

should be easily read, modified ( short enough).

Length is governed by function and number of instructions contained within

Controls the order of processing.

11

Page 12: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Types of modules 12

Control modules . Initialization module Process module ( process only once or

loop): calculation modules . print modules . read and data validation modules.

Wrapup modules

Page 13: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Control Modules

Most often called “Main” shows the overall flow of the data

through the program All other modules are subordinate to the

control module.

13

Page 14: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Initialization Module

that are executed only once during the program and only at the beginning.

Examples Opening files Initializing variables ( beginning values )

14

Page 15: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Process Modules May be processed only once , or the may

be part of a loop , there are several kind : Calculation modules:

arithmetic operations Accumulations Counting Manipulate string data.

Read and Data Validation modules: Reads and validates input data

Usually separate modules Print modules :

print output line( the result of the processing)

15

Page 16: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Wrap Up Module

process all instructions that are executed once during the program and only at the end Examples

Closing files Printing totals .

16

Page 17: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Modules17

Modules are often combined in one solution .

A module can be broken into several smaller modules to make the parts of a program more manageable.

One module performing a specific function can be used by one or more other modules .

Page 18: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

COHESION & COUPLING

chapter 4

18

Page 19: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Cohesion & Coupling19

Each module should Be functionally independent. Perform single task . Modules will need to connected ,primarily

through the data needed to complete the specified task within the modules ( cohesion and coupling ) .

Page 20: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

cohesion20

The ability for a module to work independently from all other modules ( single entry and single exit).

Each module should have a function . Each module should have single entry

and single exit.

Page 21: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

coupling21

modules need to share data from other modules in order to complete the modular tasks.

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 the modular independence .

Allows for the communication between modules. There are three ways to couple modules : global

variable , parameters , and return value .

Page 22: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.
Page 23: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

LOCAL AND GLOBAL VARIABLES

Programmer use the concept of local and global variables to allow cohesion and coupling to occur.

Global variable( define outside individual module)

Local variables ( define within module) The difference between the two is in the

scope of the variables .

Page 24: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Local variables24

May be used only by the module itself. Other modules have no knowledge of

these variables. Allow cohesion to take place . The programmer does not have to worry

about variable name duplication in modules created by other programmers.

The modules can be coupled through the use of parameters or return values.

Page 25: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

global variables25

thy can be seen by all modules. Allow data coupling through the entire

program . The programmer must be careful not to

use the same variable name for local variable and a global variable .

The use of parameter and return value is not necessary since global variable couple the data from module to module .

Page 26: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

global variables

Can be referenced anywhere in the program Is visible and accessible

everywhere

X, Y, Z

A

CB

X, Y & Z are Global to modules

A, B & C

Page 27: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Interactivity chart27

Page 28: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Scope of Local and Global Variables28

Page 29: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Parameters29

Local variables that are passed from one module to another .

Another way of coupling that allow the communication of data between modules.

Thy are placed after module name between parentheses.

Read ( A , B , C)

Page 30: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Parameters30

Make modules more versatile. Different data can be manipulated each

time the module is called. The calling module (module that process

another module) The called module ( the module being

processed).

Page 31: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Parameter Terminology31

Page 32: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Parameters32

Parameters Comes in two types: Actual (is the list of parameter that follows the

module name being processed in the calling module )

Formal (is the list of parameter that follows the module name at the beginning of the module ).

The variable name in the actual may or may not be the same as those in the formal parameter .

The value are sent and received according to the position in the listings .

Page 33: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Actual & Formal Parameters

33

Actual Parameters Parameters used in the call statement Data types must be assignment compatible

with its corresponding formal parameter Can be a variable, constant or an

expression Can be call-by-value or call-by-reference

Formal Parameters Must be a variable Can be call-by-value or call-by-reference

Page 34: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Calling data through the modules There are two ways to send data from

one module to another: Send the value of the variable ( call-by-

value) Send the address of the variable (call-by-

reference) Specified by the use of an asterisk (*) in front of

the variable name ( actual and formal parameter)

Page 35: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Call-by-Reference35

the memory location is sent ( the address) Is specified by the use of an asterisk( * ) in

front of the variable name in both the actual and formal .

Gives access to the contents of the storage area where values are stored.

When the called module change the value of the parameter , the calling module will see the change. They are using the same memory location.

Page 36: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Call-by-Value36

the value of the variable is sent to the called module by the calling module.

Accomplished by creating a copy of the value

The called module will make a new memory location for this variable .

When the called module makes a change in the variable ,the change will not be made in the calling module. Parameter have different memory location

Page 37: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Parameters – Call-by-value and Call-by-Reference

Page 38: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Parameters – Call-by-value and Call-by-Reference

Page 39: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Parameters – Call-by-value and Call-by-Reference

Page 40: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Parameter Example

Page 41: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Coupling diagram

A coupling diagram indicates how data couples modules together

Has a rectangle for each module . Line connecting the modules for each

datum sent from one module to another . The double-headed arrows indicate that

these parameters are call - by - reference . The single- headed arrows indicate that

these parameters are call-by-value .

Page 42: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Coupling diagram

Page 43: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Coupling diagram

Page 44: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

parameters

Parameter in the formal parameter listing are neither local nor global variables .

Should not be declared as local or global variables.

In the event of variable name duplication , the local variable has precedence over the parameter variable .

The parameter variable has precedence over the global variables .

Page 45: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Return value 45

Another way to couple modules . The return value is the result of the function . This is accomplished through the name of

the function. The result is placed temporarily in the name. The function must be used as parts of

another instruction. SquareRoot = 5 + SQRT ( 16 )

The value of SQRT is no longer available to be used

Page 46: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Variable Names and the data dictionary

46

Use a variable name that relates the name of the variable to its usage.

Variable name must be unique to the module in thy are defined.

Global variable must be unique to the total program .

The computer references values by their names .

Data dictionary is a great help to keep track of the variable usage in your program .And Defines all of the variables used within a program.

Page 47: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Example of a Data Dictionary

Page 48: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

The four Logic Structures

48

control the logic of the data flow through the module.

The four basic logic structures : Sequential logic structure Decision logic structure . Loop logic structure . Case logic structure .