Chapter 4: Writing and Designing a Complete Program

Slides:



Advertisements
Similar presentations
Chapter 3: Modules, Hierarchy Charts, and Documentation
Advertisements

Chapter 2: Understanding Structure
Chapter 3: Modularization
Chapter 2: Modularization
Programming Logic and Design Eighth Edition
Repetition Control Structures
Modules, Hierarchy Charts, and Documentation
Programming Logic and Design Fourth Edition, Introductory
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture two Dr. Hamdy M. Mousa.
Programming Logic and Design, Second Edition, Comprehensive
Programming Logic and Design, Third Edition Comprehensive
Programming Logic and Design Fourth Edition, Introductory
ITEC113 Algorithms and Programming Techniques
Starting Out with C++, 3 rd Edition 1 Chapter 1. Introduction to Computers and Programming.
Writing a Complete Program
An Overview of Computers and Logic
Modules, Hierarchy Charts, and Documentation
Understanding the Mainline Logical Flow Through a Program (continued)
Guide To UNIX Using Linux Third Edition
Programming Logic and Design, Introductory, Fourth Edition1 Understanding Computer Components and Operations (continued) A program must be free of syntax.
Chapter 13: Object-Oriented Programming
Chapter 1 Program Design
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Programming Logic and Design Fourth Edition, Introductory
- Meeting 4 – Writing a Complete Program
CS102 Introduction to Computer Programming
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
An Overview of Computers and Logic
Programming Logic and Design Sixth Edition Chapter 2 Working with Data, Creating Modules, and Designing High-Quality Programs.
Simple Program Design Third Edition A Step-by-Step Approach
Programming Logic and Design, Second Edition, Comprehensive
Programming Logic and Design Fifth Edition, Comprehensive
Programming Logic and Design, Second Edition, Comprehensive
Programming Logic and Design Fifth Edition, Comprehensive
File Handling and Control Break Logic. Objectives In this chapter, you will learn about: Computer files Writing a program that reads from and/or writes.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Programming Logic and Design Using Methods. 2 Objectives Review how to use a simple method with local variables and constants Create a method that requires.
An Object-Oriented Approach to Programming Logic and Design Chapter 1 An Overview of Computers and Logic.
Chapter 2 Pseudocode. Objectives To introduce common words, keywords and meaningful names when writing pseudocode To define the three basic control structures.
Introduction to Computer Application (IC) MH Room 517 Time : 7:00-9:30pm.
Chapter 5: Making Decisions
2 Chapter 21 Understanding Structure Programming Logic and Design, Second Edition, Comprehensive 2.
Chapter 11: Sequential File Merging, Matching, and Updating Programming Logic and Design, Third Edition Comprehensive.
11 Chapter 111 Sequential File Merging, Matching, and Updating Programming Logic and Design, Second Edition, Comprehensive 11.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the program development.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 7 Using Methods.
Programming Logic and Design Seventh Edition Chapter 1 An Overview of Computers and Programming.
Chapter 1: Introduction to Computers and Programming
Chapter 2: Input, Processing, and Output
DDC 1023 – Programming Technique
Programming Logic and Design Fourth Edition, Comprehensive
Chapter 3: Using Methods, Classes, and Objects
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 2 Applications and Data.
Designing and Debugging Batch and Interactive COBOL Programs
Unit# 9: Computer Program Development
An Introduction to Structured Program Design in COBOL
Programming Logic and Design Fourth Edition, Comprehensive
Writing a Complete Program
Introduction to Computer Programming
Tonga Institute of Higher Education IT 141: Information Systems
Programming Logic and Design Fifth Edition, Comprehensive
Topics Introduction to Repetition Structures
Tonga Institute of Higher Education IT 141: Information Systems
Programming Logic and Design Eighth Edition
Presentation transcript:

Chapter 4: Writing and Designing a Complete Program Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Objectives After studying Chapter 4, you should be able to: 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 Programming Logic and Design, Third Edition Comprehensive

Objectives (continued) Understand the need for good program design 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 Programming Logic and Design, Third Edition Comprehensive

Understanding the Mainline Logical Flow Through a Program It’s wise to try to understand the big picture first You can write a program that reads records from an input file and produces a printed report as a procedural program: one procedure follows another from the beginning until the end Programming Logic and Design, Third Edition Comprehensive

Understanding the Mainline Logical Flow Through a Program (continued) The overall logic, or mainline logic, of almost every procedural computer program follows a general structure that consists of: Housekeeping, or initialization tasks Main Loop End-of-job routine Programming Logic and Design, Third Edition Comprehensive

Understanding the Mainline Logical Flow Through a Program (continued) Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Housekeeping Tasks Housekeeping tasks include all the steps that must take place at the beginning of a program Very often, this includes: Declaring variables Opening files Performing any one-time-only tasks that should occur at the beginning of the program, such as printing headings at the beginning of a report Reading the first input record Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Declaring Variables When you declare variables, you assign reasonable names to memory locations, so you can store and retrieve data there Declaring a variable involves selecting a name and a type You can provide any names for your variables The variable names just represent memory positions, and are internal to your program Programming Logic and Design, Third Edition Comprehensive

Declaring Variables (continued) In most programming languages, you can give a group of associated variables a group name Allows you to handle several associated variables using a single instruction Differs in each programming language This book follows the convention of underlining any group name and indenting the group members beneath Programming Logic and Design, Third Edition Comprehensive

Declaring Variables (continued) Programming Logic and Design, Third Edition Comprehensive

Declaring Variables (continued) In addition to declaring variables, sometimes you want to provide a variable with an initial value Providing a variable with a value when you create it is known as initializing, or defining the variable In many programming languages, if you do not provide an initial value when declaring a variable, then the value is unknown or garbage Programming Logic and Design, Third Edition Comprehensive

Declaring Variables (continued) Some programming languages do provide you with an automatic starting value for example, in Java, BASIC, or RPG, all numeric variables automatically begin with the value zero However, in C++, C#, Pascal, and COBOL, variables do not receive any initial value unless you provide one Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Opening Files If a program will use input files, you must tell the computer where the input is coming from—for example, a specific disk drive, CD, or tape drive Process known as opening a file Because a disk can have many files stored on it, the program also needs to know the name of the file being opened In many languages, if no input file is opened, input is accepted from a default or standard input device, most often the keyboard Programming Logic and Design, Third Edition Comprehensive

A One-Time-Only Task—Printing Headings A common housekeeping task involves printing headings at the top of a report In the inventory report example, three lines of headings appear at the beginning of the report In this example, printing the heading lines is straightforward: print mainHeading print columnHead1 print columnHead2 Programming Logic and Design, Third Edition Comprehensive

Reading the First Input Record If the input file has no records, when you read the first record, the computer: recognizes the end-of-file condition and proceeds to the finishUp() module, never executing mainLoop() Programming Logic and Design, Third Edition Comprehensive

Reading the First Input Record (continued) More commonly, an input file does have records After the first read the computer: determines that the eof condition is false, and the logic proceeds to the mainLoop() Immediately after reading from a file, should determine whether eof was encountered Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Writing the Main Loop The main loop of a program, controlled by the eof decision, is the program’s “workhorse” Each data record will pass once through the main loop, where calculations are performed with the data and the results printed Eventually, during an execution of the mainLoop(), the program will read a new record and encounter the end of the file Programming Logic and Design, Third Edition Comprehensive

Writing the Main Loop (continued) When you ask the eof question in the main line of the program, the answer will be yes, and the program will not enter the mainLoop()again\ Instead, the program logic will enter the finishUp()routine Programming Logic and Design, Third Edition Comprehensive

Performing End-Of-Job Tasks Within any program, the end-of-job routine holds the steps you must take at the end of the program, after all input records are processed Very often, end-of-job modules must close any open files The end-of-job module for the inventory report program is very simple Programming Logic and Design, Third Edition Comprehensive

Understanding the Need for Good Program Design As your programs become larger and more complicated, the need for good planning and design increases Each program module you design needs to work well as a stand-alone module and as an element of larger systems Programming Logic and Design, Third Edition Comprehensive

Storing Program Components in Separate Files If you write a module and store it in the same file as the program that uses it, your program files become large and hard to work with, whether you are trying to read them on a screen or on multiple printed pages In addition, when you define a useful module, you might want to use it in many programs Storing components in separate files can provide an advantage beyond ease of reuse Programming Logic and Design, Third Edition Comprehensive

Storing Program Components in Separate Files (continued) When you let others use your programs or modules, you often provide them with only the compile version of your code, not the source code, which is composed of readable statements Storing your program statements in a separate, non-readable, compiled file is an example of implementation hiding, or hiding the details of how the program or module works Other programmers can use your code, but cannot see the statements you used to create it Programming Logic and Design, Third Edition Comprehensive

Selecting Variable and Module Names An often-overlooked element in program design is the selection of good data and module names (sometimes generically called identifiers) Every programming language has specific rules for the construction of names—some languages limit the number of characters, some allow dashes, and so on Programming Logic and Design, Third Edition Comprehensive

Designing Clear Module Statements In addition to selecting good identifiers, use the following tactics to contribute to the clarity of your program module statements: Avoid confusing line breaks Use temporary variables to clarify long statements Use constants where appropriate Programming Logic and Design, Third Edition Comprehensive

Avoiding Confusing Line Breaks Some older programming languages require that program statements be placed in specific columns Most modern programming languages are free form Programming Logic and Design, Third Edition Comprehensive

Avoiding Confusing Line Breaks (continued) Programming Logic and Design, Third Edition Comprehensive

Using Temporary Variables to Clarify Long Statements When you need several mathematical operations to determine a result, consider using a series of temporary variables to hold intermediate results Programming Logic and Design, Third Edition Comprehensive

Using Constants Where Appropriate Whenever possible, use named values in your programs Programming Logic and Design, Third Edition Comprehensive

Maintaining Good Programming Habits Every program you write will be better if you plan before you code If you maintain the habits of first drawing flowcharts or writing pseudocode, your future programming projects will go more smoothly If you walk through your program logic on paper (called desk-checking) before starting to type statements in C++, COBOL, Visual Basic, or Java, your programs will run correctly sooner Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Summary When you write a complete program, you first determine whether you have all the necessary data to produce the report Housekeeping tasks include all steps that must take place at the beginning of a program The main loop of a program is controlled by the eof decision As your programs become larger and more complicated, the need for good planning and design increases Programming Logic and Design, Third Edition Comprehensive

Programming Logic and Design, Third Edition Comprehensive Summary (continued) Most modern programming languages allow you to store program components in separate files and use instructions to include them in any program that uses them When selecting data and module names, use meaningful, pronounceable names When writing program statements, you should avoid confusing line breaks, use temporary variables to clarify long statements, and use constants where appropriate Programming Logic and Design, Third Edition Comprehensive