Title: Programming Logic and Design Fourth Edition, Comprehensive
1Programming Logic and Design Fourth Edition,
Comprehensive
- Chapter 4
- Designing and Writing
- a Complete Program
2Objectives
- Plan the mainline logic for a complete program
- Describe typical housekeeping tasks (
initialization activities) - Describe tasks typically performed in the main
loop of a program ( main processing loop ) - Describe tasks performed in the end-of-job module
( termination activities ) - Understand the need for good program design
3Objectives (continued)
- Appreciate the advantages of storing program
components in separate files - Select superior variable and module names
- Design clear module statements
- Understand the need for maintaining good
programming habits
4Understanding the Mainline Logical Flow Through a
Program
- Understand what the goals are
- Ask the user to clarify if necessary
5Understanding the Mainline Logical Flow Through a
Program (continued)
- Ensure you have all the data required to produce
the desired output
6Understanding the Mainline Logical Flow Through a
Program (continued)
- Understand the big picture first
7Understanding 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
8Conceptual View of the shell of a Typical File
Processing Program
9Global and Local Variables
- Global Variables
- Declared outside of all modules usually at the
beginning of the program - Can be used by any module in the program
- Only declared if required by the program
- Local Variables
- Declared within a specific module
- Can only be used in the module in which they are
declared not accessible by any other module - Most modules will have some local variables
10Understanding the Mainline Logical Flow Through a
Program (continued)
11Understanding 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
12Housekeeping Tasks
- Housekeeping tasks include all steps that occur
at the beginning of the program - Declare variables
- Assumption in this book is that every variable
will be a global variable ( Not realistic
for most modern programming languages ) - Open files
- Perform one-time-only tasks such as printing
headings - Read the first input record ( called a priming
read )
13Declaring Variables
- Assign identifiers to memory locations
- Specify the name and data type for each variable
- Use meaningful names and follow standards
- A prefix may used as part of the name for many
variables. - Declare a variable for each field in a data file
- Variables may be initialized at declaration
14Declaring Variables (continued)
15Declaring Variables (continued)
- Group name
- Name for a group of associated variables
- Can handle the entire group with a single
instructionfor example read invRecord
16Declaring Variables (continued)
- Initializing a variable allows you to specify a
starting 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 ( they will be
assigned values read from the file ).
17Declaring Variables (continued)
- Can use variables for report headings
- Embed any required spaces
- Heading can be printed using these variables
18Declaring Variables (continued)
- Use annotation symbol to show variables
- Assumption is that all of the variables to be
used in the program will be declared in the
housekeeping module.
19Opening Files
- Specify file name and path (location)
- Issue a file open command
- If a file is not used, 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
20A One-Time-Only TaskPrinting Headings
- Printing headings for reports usually is done at
beginning of the program
21Reading the First 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
22Reading the First Input Record (continued)
23Reading the First Input Record (continued)
- Input from a data file
- Input from a data file using a group name
24Checking for the End of File ( EOF )
- First task after housekeeping
- 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 - If no data in the file, EOF occurs on the first
read - If there is data, each record is processed before
the next read occurs
25(No Transcript)
26(No Transcript)
27(No Transcript)
28- Handling the report headings in a separate module
29Writing the Main Loop
- Each data record passes through the main loop
once - Inventory program main loop steps
- Calculate the profit for an item
- Print the items information on the report
- Read the next inventory record
30Writing the Main Loop (continued)
- Must declare additional variables for calculation
results
31(No Transcript)
32Writing the Main Loop (continued)
- Detail lines are printed one line at a time
- Calculations can be done within the print
statement - Work variable (or work field) a variable used to
temporarily hold a calculation ( aka calculated
field )
33Performing End-of-Job Tasks
- End-of-job tasks may include
- Printing summaries or grand totals
- Printing End of Report message
- Closing any open files
- Footer line (or footer) end-of-job message line
34(No Transcript)
35(No Transcript)
36(No Transcript)
37Understanding the Need for Good Program Design
- Good design is
- Critical for very large programs
- Needed to guarantee that components work together
properly - Well-designed program modules should work
- As stand-alone modules
- As part of larger systems
38Storing Program Components in Separate Files
- Large programs may contain hundreds of variables
and thousands of lines of code - Manage lengthy programs by breaking into modules
- Many languages allow program components to be
stored in separate files - Storing components separately simplifies reuse
- Accessing modules from separate files is done
with a statement like include, import, or copy
39Storing Program Components in Separate Files
(continued)
40Storing Program Components in Separate Files
(continued)
- Advantages of storing components separately
- Simplifies reuse
- Can be provided in compiled form only, to hide
details - Implementation hiding
- hiding details of how a program or module works
41Selecting Variable and Module Names
- Using meaningful names
- Improves code readability
- Is a form of self-documenting the program
- Use pronounceable names
- Commonly used abbreviations are ok (e.g., SSN)
- Avoid digits in a name to avoid confusing
- Zeros and Os
- Ones and lowercase Ls
42Designing Clear Module Statements
- Follow these rules
- Select good identifier names
- Avoid confusing line breaks
- Use temporary variables to clarify long
statements - Use constants where appropriate
43Avoiding Confusing Line Breaks
- Free-form coding allows programmer to decide
where to break lines of code
44Using Temporary Variables to Clarify Long
Statements
- Use temporary variables to store intermediate
results
45Using Constants Where Appropriate
- Named constant
- a constant whose value never changes during
execution - Advantages
- Improves code readability
- If the value changes later, there is only one
place in the code to make the change - Usually written with all uppercase letters
- ATHLETIC_FEE
- TUITION_PER_CREDIT_HOUR
46Using Constants Where Appropriate (continued)
47Maintaining Good Programming Habits
- Program will be better written if you plan before
you code - Walk through program logic on paper before coding
(desk-checking) - Select good variable and module names for
readability
48Summary
- Three steps to designing a good program
- Understand the output that is required
- Ensure you have the necessary input data
- Plan the mainline logic
- Housekeeping tasks done at the beginning of the
program declaring variables, opening files,
printing headings - Main loop is controlled by EOF decision
- Each data record passes through the main loop
once
49Summary (continued)
- End-of-job steps done at end of the program
printing summaries, closing files - Good design becomes more critical as programs get
larger - Program components can be stored in separate
files - Select meaningful, pronounceable names
- Avoid confusing line breaks, use temporary
variables, and use constants where appropriate