Fourth R Inc.1 WELCOME TO MICROSOFT OFFICE FRONTPAGE 2003 INTRODUCTORY COURSE.
Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete...
-
Upload
shana-lawson -
Category
Documents
-
view
217 -
download
1
Transcript of Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete...
![Page 1: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/1.jpg)
Programming Logic and Design
Fourth Edition, Introductory
Chapter 4Designing and Writing a Complete Program
![Page 2: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/2.jpg)
Programming Logic and Design, Introductory, Fourth Edition 2
Objectives
• Plan the mainline logic for a complete program
• Describe typical housekeeping tasks
• Describe tasks typically performed in the main loop of a program
• Describe tasks performed in the end-of-job module
• Understand the need for good program design
![Page 3: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/3.jpg)
Programming Logic and Design, Introductory, Fourth Edition 3
Objectives (continued)
• Appreciated 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
![Page 4: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/4.jpg)
Programming Logic and Design, Introductory, Fourth Edition 4
Understanding the Mainline Logical Flow Through a Program
• Understand what the goals are– Ask the user to clarify if necessary
![Page 5: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/5.jpg)
Programming Logic and Design, Introductory, Fourth Edition 5
Understanding the Mainline Logical Flow Through a Program (continued)
• Ensure you have all the data required to produce the desired output
![Page 6: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/6.jpg)
Programming Logic and Design, Introductory, Fourth Edition 6
Understanding the Mainline Logical Flow Through a Program (continued)
• Understand the big picture first
![Page 7: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/7.jpg)
Programming Logic and Design, Introductory, Fourth Edition 7
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
![Page 8: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/8.jpg)
Programming Logic and Design, Introductory, Fourth Edition 8
Understanding the Mainline Logical Flow Through a Program (continued)
![Page 9: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/9.jpg)
Programming Logic and Design, Introductory, Fourth Edition 9
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
![Page 10: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/10.jpg)
Programming Logic and Design, Introductory, Fourth Edition 10
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
![Page 11: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/11.jpg)
Programming Logic and Design, Introductory, Fourth Edition 11
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
![Page 12: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/12.jpg)
Programming Logic and Design, Introductory, Fourth Edition 12
Declaring Variables (continued)
![Page 13: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/13.jpg)
Programming Logic and Design, Introductory, Fourth Edition 13
Declaring Variables (continued)
• Group name: – Name for a group of associated variables– Can handle the entire group with a single instruction
![Page 14: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/14.jpg)
Programming Logic and Design, Introductory, Fourth Edition 14
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
![Page 15: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/15.jpg)
Programming Logic and Design, Introductory, Fourth Edition 15
Declaring Variables (continued)
• Can use variables for report headings• Embed any required spaces
• Heading can be printed using these variables
![Page 16: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/16.jpg)
Programming Logic and Design, Introductory, Fourth Edition 16
Declaring Variables (continued)
• Every language provides methods for:– Advancing the paper to top of page– Printing single, double, or triple spaced lines
• Use annotation symbol to show variables
![Page 17: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/17.jpg)
Programming Logic and Design, Introductory, Fourth Edition 17
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
![Page 18: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/18.jpg)
Programming Logic and Design, Introductory, Fourth Edition 18
A One-Time-Only Task -- Printing Headings
• Printing headings for reports usually is done at beginning of the program
![Page 19: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/19.jpg)
Programming Logic and Design, Introductory, Fourth Edition 19
Reading 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
![Page 20: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/20.jpg)
Programming Logic and Design, Introductory, Fourth Edition 20
Reading the First Input Record (continued)
• Interactive input:
![Page 21: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/21.jpg)
Programming Logic and Design, Introductory, Fourth Edition 21
Reading the First Input Record (continued)
• Input from a data file:
• Input from a data file using a group name:
![Page 22: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/22.jpg)
Programming Logic and Design, Introductory, Fourth Edition 22
Checking for the End of the File
• 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
![Page 23: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/23.jpg)
Programming Logic and Design, Introductory, Fourth Edition 23
Checking for End of File (continued)
![Page 24: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/24.jpg)
Programming Logic and Design, Introductory, Fourth Edition 24
Checking for End of File (continued)
![Page 25: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/25.jpg)
Programming Logic and Design, Introductory, Fourth Edition 25
Checking for End of File (continued)
![Page 26: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/26.jpg)
Programming Logic and Design, Introductory, Fourth Edition 26
Checking for End of File (continued)• Handling the report headings in a separate module:
![Page 27: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/27.jpg)
Programming Logic and Design, Introductory, Fourth Edition 27
Writing the Main Loop
• Each data record passes through the main loop once
• Inventory program main loop steps:1. Calculate the profit for an item
2. Print the item’s information on the report
3. Read the next inventory record
![Page 28: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/28.jpg)
Programming Logic and Design, Introductory, Fourth Edition 28
Writing the Main Loop (continued)
• Must declare additional variables for calculation results
![Page 29: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/29.jpg)
Programming Logic and Design, Introductory, Fourth Edition 29
Writing the Main Loop (continued)
![Page 30: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/30.jpg)
Programming Logic and Design, Introductory, Fourth Edition 30
Writing 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
![Page 31: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/31.jpg)
Programming Logic and Design, Introductory, Fourth Edition 31
Performing 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
![Page 32: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/32.jpg)
Programming Logic and Design, Introductory, Fourth Edition 32
Performing End-of-Job Tasks (continued)
![Page 33: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/33.jpg)
Programming Logic and Design, Introductory, Fourth Edition 33
Performing End-of-Job Tasks (continued)
![Page 34: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/34.jpg)
Programming Logic and Design, Introductory, Fourth Edition 34
Performing End-of-Job Tasks (continued)
![Page 35: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/35.jpg)
Programming Logic and Design, Introductory, Fourth Edition 35
Understanding 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
![Page 36: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/36.jpg)
Programming Logic and Design, Introductory, Fourth Edition 36
Storing 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
![Page 37: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/37.jpg)
Programming Logic and Design, Introductory, Fourth Edition 37
Storing Program Components in Separate Files (continued)
![Page 38: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/38.jpg)
Programming Logic and Design, Introductory, Fourth Edition 38
Storing 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
![Page 39: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/39.jpg)
Programming Logic and Design, Introductory, Fourth Edition 39
Selecting 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 O’s– Ones and lowercase L’s
![Page 40: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/40.jpg)
Programming Logic and Design, Introductory, Fourth Edition 40
Designing 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
![Page 41: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/41.jpg)
Programming Logic and Design, Introductory, Fourth Edition 41
Avoiding Confusing Line Breaks
• Free-form coding allows programmer to decide where to break lines of code
![Page 42: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/42.jpg)
Programming Logic and Design, Introductory, Fourth Edition 42
Using Temporary Variables to Clarify Long Statements
• Use temporary variables to store intermediate results
![Page 43: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/43.jpg)
Programming Logic and Design, Introductory, Fourth Edition 43
Using 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
![Page 44: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/44.jpg)
Programming Logic and Design, Introductory, Fourth Edition 44
Using Constants Where Appropriate (continued)
![Page 45: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/45.jpg)
Programming Logic and Design, Introductory, Fourth Edition 45
Maintaining 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
![Page 46: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/46.jpg)
Programming Logic and Design, Introductory, Fourth Edition 46
Summary
• 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
![Page 47: Programming Logic and Design Fourth Edition, Introductory Chapter 4 Designing and Writing a Complete Program.](https://reader036.fdocuments.in/reader036/viewer/2022062421/56649d015503460f949d34da/html5/thumbnails/47.jpg)
Programming Logic and Design, Introductory, Fourth Edition 47
Summary (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