introduction to programming structure chapter 4 1
TRANSCRIPT
![Page 1: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/1.jpg)
INTRODUCTION TO PROGRAMMING
STRUCTURE
Chapter 4
1
![Page 2: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/2.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/3.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/4.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/5.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/6.jpg)
6
Sequential Logic Structure
![Page 7: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/7.jpg)
7
Decision Logic Structure
![Page 8: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/8.jpg)
8
Loop Logic Structure
![Page 9: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/9.jpg)
9
Case Logic Structure
![Page 10: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/10.jpg)
10
![Page 11: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/11.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/12.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/13.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/15.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/16.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/17.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/18.jpg)
COHESION & COUPLING
chapter 4
18
![Page 19: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/19.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/20.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/21.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/22.jpg)
![Page 23: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/23.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/24.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/25.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/26.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/27.jpg)
Interactivity chart27
![Page 28: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/28.jpg)
Scope of Local and Global Variables28
![Page 29: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/29.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/30.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/31.jpg)
Parameter Terminology31
![Page 32: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/32.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/33.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/34.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/35.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/36.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/37.jpg)
Parameters – Call-by-value and Call-by-Reference
![Page 38: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/38.jpg)
Parameters – Call-by-value and Call-by-Reference
![Page 39: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/39.jpg)
Parameters – Call-by-value and Call-by-Reference
![Page 40: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/40.jpg)
Parameter Example
![Page 41: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/41.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/42.jpg)
Coupling diagram
![Page 43: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/43.jpg)
Coupling diagram
![Page 44: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/44.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/45.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/46.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/47.jpg)
Example of a Data Dictionary
![Page 48: INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1](https://reader033.vdocuments.mx/reader033/viewer/2022061614/56649e3b5503460f94b2d364/html5/thumbnails/48.jpg)
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 .