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

Similar documents
Programming Logic and Design Sixth Edition Chapter 2 Working with Data, Creating Modules, and Designing High-Quality Programs

A Beginner s Guide to Programming Logic, Introductory. Chapter 2 Working with Data, Creating Modules, and Designing High-Quality Programs

Programming Logic and Design Seventh Edition Chapter 2 Elements of High-Quality Programs

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

Pseudo Code and Flow Charts. Chapter 1 Lesson 2

Database Design. 8-4 Drawing Conventions for Readability. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Chapter 1: An Overview of Computers and Logic

Visual Basic. Chapter 3

Chapter 2 Author Notes

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Method & Tools for Program Analysis & Design

Low-Level Languages. Computer Programs and Programming Languages

5. Control Statements

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Lesson 1. Introduction to Programming OBJECTIVES

Marking Scheme for the Sample Exam Paper

Test Bank for An Object Oriented Approach to Programming Logic and Design 4th Edition by Joyce Farrell

Simply Java Programming: An Application Driven, Tutorial

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

Chapter 1: Introduction to Computers and Java

8/23/2014. Chapter Topics. Introduction. Java History. Why Program? Java Applications and Applets. Chapter 1: Introduction to Computers and Java

Week - 01 Lecture - 04 Downloading and installing Python

CS112 Lecture: Working with Numbers

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Chapter 1 Introduction to Computers and C++ Programming

Chapter Twelve. Systems Design and Development

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Heap Management. Heap Allocation

Programming Style. Quick Look. Features of an Effective Style. Naming Conventions

Functions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing)

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

User Interface Programming OOP/Java Primer. Step 3 - documentation

Discovering Computers 2008

Java Style Guide. 1.0 General. 2.0 Visual Layout. Dr Caffeine

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

C++ Programming Language Lecture 2 Problem Analysis and Solution Representation

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

S/W Programming & Languages

Steps to program development

Lecture Notes on Queues

Additional Support and Disability Advice Centre

Problem Solving and Program Design - Chapter 1. Cory L. Strope

Outline. Program development cycle. Algorithms development and representation. Examples.

Introduction. A Brief Description of Our Journey

Discovering Computers Chapter 13 Programming Languages and Program Development

Introduction to Computer Programming/Handout 01 Page 1 of 13

(Murlidhar Group of Institutions,Bhavnagar Road, Rajkot) by:-assit. Prof. Vijay Vora (SOOADM) MCA-III

COP 1170 Introduction to Computer Programming using Visual Basic

Chapter 1. Introduction to Computers and Java Objects. Background information. » important regardless of programming language. Introduction to Java

Programming Languages and Program Development Life Cycle Fall Introduction to Information and Communication Technologies CSD 102

CHAPTER 1 Introduction to Computers and Java

Software Design Fundamentals. CSCE Lecture 11-09/27/2016

COSC345 Software Engineering. Documentation

CS 211 Programming Practicum Fall 2018

A A B U n i v e r s i t y

Programming: detailed instructions which tell the computer hardware what to do aka software Computer Science: the study NOT of computers, but of what

Module 1: Introduction to Computers, Programs, and Java

Variables and Constants

Intermediate Code Generation

Introducing Computer Programming

Python - Week 1. Mohammad Shokoohi-Yekta

IT 374 C# and Applications/ IT695 C# Data Structures

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Chapter 3: Operators, Expressions and Type Conversion

Basic Computer Skills: An Overview

Engineering Computing M1H Together Towards A Green Environment

0 Introduction: Computer systems and program development

Introduction. C provides two styles of flow control:

COMPILER DESIGN. For COMPUTER SCIENCE

Logical File Organisation A file is logically organised as follows:

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 14: OCT. 25TH INSTRUCTOR: JIAYIN WANG

2.8. Decision Making: Equality and Relational Operators

A Foundation for Programming

CSE 230 Intermediate Programming in C and C++ Functions

The compiler is spewing error messages.

Working with JavaScript

Chapter 2: Understanding Structure. Programming Logic and Design, 4 th Edition Introductory

Topic 1: Programming concepts

Lecture Notes for Chapter 2: Getting Started

Chapter 1 Introduction to Computers, Programs, and Java

Compilers. Prerequisites

DATA AND ABSTRACTION. Today you will learn : How to work with variables How to break a program down Good program design

Lecture 10 Notes Linked Lists

Repetitive Program Execution

Variable A variable is a value that can change during the execution of a program.

Java: Comment Text. Introduction. Concepts

1) Which of the following is an example of a programming language? 1) A) Microsoft Word B) English C) HTML D) Java

COMPILER DESIGN LECTURE NOTES

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

Chapter 2 Basic Elements of C++

1. In waterfall model, output of one phase is input to next phase. True or false.

Lecturer 4: File Handling

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Mustafa T. Babagil & Filiz Bilen Page 1 COMP190, COMP191 AND COMP101 LAB MANUAL 2

Introduction to Java Programming

File System Interface: Overview. Objective. File Concept UNIT-IV FILE SYSTEMS

5 The Control Structure Diagram (CSD)

Transcription:

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

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 Introductory 2

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 Introductory 3

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 Introductory 4

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 Introductory 5

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

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 Introductory 7

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 Introductory 8

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 Introductory 9

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

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 Introductory 11

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 Introductory 12

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 Introductory 13

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 Introductory 14

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 Introductory 15

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 Introductory 16

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 Introductory 17

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 Introductory 18

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 Introductory 19

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 Introductory 20

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 Introductory 21

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 Introductory 22

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 Introductory 23

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 Introductory 24

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 Introductory 25

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

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 Introductory 27

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

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 Introductory 29

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 Introductory 30

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 Introductory 31