cs111_ch04_v4

advertisement
Understanding the Mainline Logical
Flow Through a Program (continued)
• Procedural program: one procedure follows
another from beginning to end
• Mainline logic has three distinct parts:
– Housekeeping: steps to get ready
– Main loop: instructions executed for every input record
– End-of-job: steps taken at end of program
• Break the logic down into at least three modules
Programming Logic and Design, Introductory, Fourth Edition
1
Understanding the Mainline Logical
Flow Through a Program (continued)
Programming Logic and Design, Introductory, Fourth Edition
2
Understanding the Mainline Logical
Flow Through a Program (continued)
• Modularization of the program:
– Keeps the job manageable
– Allows multiple programmers to work simultaneously
– Keeps the program structured
Programming Logic and Design, Introductory, Fourth Edition
3
Housekeeping Tasks
• Housekeeping tasks: include all steps that occur
at the beginning of the program
– Declare variables
– Open files
– Perform one-time-only tasks such as printing
headings
– Read the first input record
Programming Logic and Design, Introductory, Fourth Edition
4
Declaring Variables
•
•
•
•
•
Assign identifiers to memory locations
Specify the name and data type
Use meaningful names and follow standards
Prefixes may be used to group related variables
Declare a variable for each field in a data file
Programming Logic and Design, Introductory, Fourth Edition
5
Declaring Variables (continued)
• Initializing (or defining) the variable: providing an
initial value
• Some languages provide default initial values
• Other languages leave variables with an unknown or
garbage value
• Variables representing data fields in files do not need
to be initialized
Programming Logic and Design, Introductory, Fourth Edition
6
Declaring Variables (continued)
• Can use variables for report headings
• Embed any required spaces
• Heading can be printed using these variables
Programming Logic and Design, Introductory, Fourth Edition
7
Declaring Variables (continued)
• Local variables: declared within a module
• Global variables: declared at the beginning of the
program, and used in all modules
• Annotation box: flowchart symbol containing
notes
• Data Dictionary: list of variables used in a
program, with their type, size, and description
Programming Logic and Design, Introductory, Fourth Edition
8
Opening Files
• Specify file name and path (location)
• Issue a file open command
• If no input file is opened, input may be accepted
from the standard input device (e.g., keyboard)
• You must open both input and output files to be
used, including printer output device
• If no output file is opened, standard output device
(e.g., monitor) may be used
Programming Logic and Design, Introductory, Fourth Edition
9
Printing Headings
• Printing headings for reports usually is done at
beginning of the program or possibly for each new
page
Programming Logic and Design, Introductory, Fourth Edition
10
Reading an Input Record
• Reading the first input record is the last
housekeeping task
• Interactive application:
– Interacts with users via keyboard or mouse input
– Program pauses when the read command is
executed until the user enters data
• Delimiter: a character designated as a separator
between data values
• Prompt: an output statement that asks the user to
enter specific data
Programming Logic and Design, Introductory, Fourth Edition
11
Reading the First Input Record
(continued)
• Interactive input: (from a keyboard)
Programming Logic and Design, Introductory, Fourth Edition
12
Reading an Input Record
• Input from a data file:
Programming Logic and Design, Introductory, Fourth Edition
13
Checking for the End of the File
• After reading a file
• For an interactive program, EOF may be determined
when:
– User enters a predetermined sentinel value
– User selects a screen option using a mouse
• For input from a file, the input device recognizes EOF
• EOF may occur on the first read
• If there is data, each record is processed before the
next read occurs
Programming Logic and Design, Introductory, Fourth Edition
14
Checking for End of File (continued)
Programming Logic and Design, Introductory, Fourth Edition
15
Modules, Subroutines, Procedures,
Functions, or Methods
• Module:
– Unit of code that performs one small task
– Called a subroutine, procedure, function, or method
• Modularization: breaking a large program into
modules
Programming Logic and Design, Introductory, Fourth Edition
16
Modules, Subroutines, Procedures,
Functions, or Methods (continued)
• Advantages of modularization:
–
–
–
–
Provides abstraction
Allows multiple programmers to work simultaneously
Allows code reuse
Makes identifying structures easier
Programming Logic and Design, Introductory, Fourth Edition
17
Modularization Provides Abstraction
• Abstraction:
– Focusing on important properties while ignoring nonessential details
– Avoids the low-level details and uses a high-level
approach
– Makes complex tasks look simple
Programming Logic and Design, Introductory, Fourth Edition
18
Modularization Provides Abstraction
(continued)
• A To-Do list
• with abstraction:
without abstraction:
Programming Logic and Design, Introductory, Fourth Edition
19
Modularization Allows Multiple
Programmers to Work on a Problem
• Large programming projects can be divided into
modules
• Modules can be written by different programmers
• Development time is significantly reduced
Programming Logic and Design, Introductory, Fourth Edition
20
Modularization Allows You to Reuse
Your Work
• Reusability: the ability to use modules in a variety
of applications
• Reliability: assurance that a module has been
tested and proven to function correctly
Programming Logic and Design, Introductory, Fourth Edition
21
Modularizing a Program
•
Most programs contain a main module
– Contains the mainline logic
– Accesses other modules or subroutines
•
Rules for module names used here:
– Must be one word
– Should be meaningful
– Are followed by a set of parentheses
Programming Logic and Design, Introductory, Fourth Edition
22
Modularizing a Program (continued)
• Calling program (or calling module): one that uses
another module
• Flowchart symbol for calling a module: a rectangle
with bar across the top
• Flowchart for the module contains:
– Module name in the start symbol
– exit or return in the stop symbol
• When a module is called, logic transfers to the model
• When module ends, logic transfers back to the caller
Programming Logic and Design, Introductory, Fourth Edition
23
Modularizing a Program (continued)
Programming Logic and Design, Introductory, Fourth Edition
24
Modules Calling Other Modules
Programming Logic and Design, Introductory, Fourth Edition
25
Understanding Documentation
• Documentation:
–
–
–
–
All supporting material that goes with a program
Two major categories: for users and for programmers
Usually created by system analysts and/or tech writers
May be printed or electronic (Web or CD)
• End users: people who use computer programs
• Program Documentation:
– Internal program documentation: comments within code
– External program documentation: supporting paperwork
written before programming begins
Programming Logic and Design, Introductory, Fourth Edition
26
Completing the Documentation
• Program documentation may contain:
–
–
–
–
–
Output design
Input description
Flowcharts
Pseudocode
Program code listing
• User documentation may contain
– Manuals
– Instructional material
– Operating instructions
Programming Logic and Design, Introductory, Fourth Edition
27
Download