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

Similar documents
Programming Logic and Design Sixth Edition 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

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

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

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

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

SKILL AREA 306: DEVELOP AND IMPLEMENT COMPUTER PROGRAMS

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

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

Microsoft Visual Basic 2005: Reloaded

Section 1. The essence of COBOL programming. Mike Murach & Associates

Pseudo Code and Flow Charts. Chapter 1 Lesson 2

Skill Area 336 Explain Essential Programming Concept. Programming Language 2 (PL2)

CS112 Lecture: Working with Numbers

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Part II Composition of Functions

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

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

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Chapter 1: An Overview of Computers and Logic

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

VARIABLES & ASSIGNMENTS

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

Programming Practice (vs Principles)

APPENDIX E SOLUTION TO CHAPTER SELF-TEST CHAPTER 1 TRUE-FALSE FILL-IN-THE-BLANKS

Introduction to Modules. Chapter 3. Defining and Calling a Module. Introduction to Modules. 5 Benefits of Modules. Modules CSUS, Spring 2016

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Full file at

CSCE 120: Learning To Code

Programming Language 2 (PL2)

Variables, expressions and statements

3. Simple Types, Variables, and Constants

THINGS YOU NEED TO KNOW ABOUT USER DOCUMENTATION DOCUMENTATION BEST PRACTICES

Chapter 9. Process Modeling. McGraw-Hill/Irwin. Copyright 2007 by The McGraw-Hill Companies, Inc. All rights reserved.

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Microsoft Visual Basic 2015: Reloaded

2. Numbers In, Numbers Out

Unit 6 - Software Design and Development LESSON 3 KEY FEATURES

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

The C++ Language. Arizona State University 1

Shell Script Programming

Method & Tools for Program Analysis & Design

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

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

Programming for Engineers Introduction to C

(I m not printing out these notes! Take your own.)

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Assessment of Programming Skills of First Year CS Students: Problem Set

Algorithms and Flowcharts

CSI Lab 02. Tuesday, January 21st

BITG 1233: Introduction to C++

Language Fundamentals

Outline. Example: Entertaining Friends. Modular Programming. Example: Entertaining Friends. Example: Entertaining Friends. Lecture 1.

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Unit 6 - Software Design and Development LESSON 3 KEY FEATURES

Variables and Constants

Indicate the answer choice that best completes the statement or answers the question. Enter the appropriate word(s) to complete the statement.

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

Chapter 17. Fundamental Concepts Expressed in JavaScript

The Java Language Rules And Tools 3

CIS 3260 Intro. to Programming with C#

2. Numbers In, Numbers Out

Part 1 Simple Arithmetic

Module 1: Introduction to Computers, Programs, and Java

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Lecture 2 Tao Wang 1

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

4. The is a diagram that graphically depicts the steps that take place in a program. a. Program b. Flowchart c. Algorithm d. Code e.

Unit II. (i) Computer Programming Languages

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

VARIABLES. 1. STRINGS Data with letters and/or characters 2. INTEGERS Numbers without decimals 3. FLOATING POINT NUMBERS Numbers with decimals

Review. Input, Processing and Output. Review. Review. Designing a Program. Typical Software Development cycle. Bonita Sharif

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Chapter 2.5 Writing maintainable programs

Learning to Provide Modern Solutions

Unit E Step-by-Step: Programming with Python

Chapter 2 Basic Elements of C++

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Password Management Guidelines for Cisco UCS Passwords

Visual Basic. Chapter 3

Section we will not cover section 2.11 feel free to read it on your own

Briefly describe the purpose of the lexical and syntax analysis phases in a compiler.

Arithmetic Operations

Chapter 1 INTRODUCTION. SYS-ED/ Computer Education Techniques, Inc.

PLD Semester Exam Study Guide Dec. 2018

Web Application Development (WAD) V th Sem BBAITM(Unit-1) By: Binit Patel

Program Planning, Data Comparisons, Strings

Special Section: Building Your Own Compiler

I. Variables and Data Type week 3

Formulas in Microsoft Excel

Chapter 2 Working with Data Types and Operators

CSc Introduction to Computing

Programming in ROBOTC ROBOTC Rules

Transcription:

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

Objectives In this chapter, you will learn about: Declaring and using variables and constants Assigning values to variables The advantages of modularization Modularizing a program The most common configuration for mainline logic A Beginner's Guide to Programming Logic, Introductory 2

Objectives (continued) In this chapter, you will learn about: (continued) Hierarchy charts Some features of good program design A Beginner's Guide to Programming Logic, Introductory 3

Declaring and Using Variables Data items and Constants All the text, numbers, and other information that are processed by a computer Stored in variables in memory Data types Numeric consists of numbers String is anything not used in mathematics Different forms Variables Literals, or unnamed constants Named constants A Beginner's Guide to Programming Logic, Introductory 4

Working with Variables Named memory locations Contents can vary or differ over time Declaration Statement that provides a data type and an identifier for a variable Identifier Variable s name A Beginner's Guide to Programming Logic, Introductory 5

Working with Variables (continued) Figure 2-1 Flowchart and pseudocode for the number-doubling program A Beginner's Guide to Programming Logic, Introductory 6

Working with Variables (continued) Data type Classification that describes: What values can be held by the item How the item is stored in computer memory What operations can be performed on the data item Initializing a variable Declare a starting value for any variable Garbage Variable s unknown value before initialization A Beginner's Guide to Programming Logic, Introductory 7

Figure 2-2 Flowchart and pseudocode of number-doubling program with variable declarations A Beginner's Guide to Programming Logic, Introductory 8

Naming Variables Programmer chooses reasonable and descriptive names for variables Programming languages have rules for creating identifiers Most languages allow letters and digits Some languages allow hyphens, dollar signs, or other special characters Reserved keywords are usually not allowed We ll assume case sensitive variable names Different limits on the length of variable names A Beginner's Guide to Programming Logic, Introductory 9

Naming Variables (continued) Camel casing Variable names such as hourlywage have a hump in the middle Variable names used throughout book Must be one word Must start with a letter Should have some appropriate meaning A Beginner's Guide to Programming Logic, Introductory 10

Understanding Unnamed, Literal Constants and their Data Types Numeric constant (or literal numeric constant) Specific numeric value Example: 43 Does not change String constant (or literal string constant) String of characters enclosed within quotation marks Example: "Amanda" Unnamed constants Do not have identifiers like variables do A Beginner's Guide to Programming Logic, Introductory 11

Understanding the Data Types of Variables Numeric variable Holds digits Can perform mathematical operations on it String variable Can hold text Letters of the alphabet Special characters such as punctuation marks Type-safety Assign data to a variable only if it is the correct type A Beginner's Guide to Programming Logic, Introductory 12

Declaring Named Constants Named constant Similar to a variable Can be assigned a value only once Assign a useful name to a value that will never be changed during a program s execution Magic number Unnamed constant Purpose is not immediately apparent, so avoid Use taxamount = price * SALES_TAX_AMOUNT instead of taxamount = price * 0.06 A Beginner's Guide to Programming Logic, Introductory 13

Assigning Values to Variables Assignment statement set myanswer = mynumber * 2 Assignment operator Equal sign Always operates from right to left Valid set somenumber = 2 set somenumber = someothernumber Not valid set 2 + 4 = somenumber A Beginner's Guide to Programming Logic, Introductory 14

Performing Arithmetic Operations Standard arithmetic operators: + (plus sign) addition (minus sign) subtraction * (asterisk) multiplication / (slash) division A Beginner's Guide to Programming Logic, Introductory 15

Performing Arithmetic Operations Rules of precedence (continued) Also called the order of operations Dictate the order in which operations in the same statement are carried out Expressions within parentheses are evaluated first Multiplication and division are evaluated next From left to right Addition and subtraction are evaluated next From left to right A Beginner's Guide to Programming Logic, Introductory 16

Performing Arithmetic Operations (continued) Left-to-right associativity Operations with the same precedence take place from left to right A Beginner's Guide to Programming Logic, Introductory 17

Performing Arithmetic Operations (continued) Table 2-1 Precedence and associativity of five common operators A Beginner's Guide to Programming Logic, Introductory 18

Understanding the Advantages Modules of Modularization Subunit of programming problem Also called subroutines, procedures, functions, or methods Modularization Breaking down a large main program into modules Statements taken out of a main program and put into a module have been encapsulated Main program shorter and easier to understand Reasons Abstraction Allows multiple programmers to work on a problem Reuse your work more easily A Beginner's Guide to Programming Logic, Introductory 19

Modularization Provides Abstraction Abstraction Paying attention to important properties while ignoring nonessential details Selective ignorance Newer high-level programming languages Use English-like vocabulary One broad statement corresponds to dozens of machine instructions Modules provide another way to achieve abstraction A Beginner's Guide to Programming Logic, Introductory 20

Modularization Allows Multiple Programmers to Work on a Problem More easily divide the task among various people Rarely does a single programmer write a commercial program Professional software developers can write new programs quickly by dividing large programs into modules Assign each module to an individual programmer or team A Beginner's Guide to Programming Logic, Introductory 21

Modularization Allows You to Reuse Reusability Your Work Feature of modular programs Allows individual modules to be used in a variety of applications Many real-world examples of reusability Reliability Assures that a module has been tested and proven to function correctly A Beginner's Guide to Programming Logic, Introductory 22

Modularizing a Program Main program Basic steps (mainline logic) of the program Include in a module Header Body Return statement Naming a module Similar to naming a variable Module names are followed by a set of parentheses A Beginner's Guide to Programming Logic, Introductory 23

Modularizing a Program (continued) When a main program wants to use a module Calls the module s name Flowchart Symbol used to call a module is a rectangle with a bar across the top Place the name of the module you are calling inside the rectangle Draw each module separately with its own sentinel symbols A Beginner's Guide to Programming Logic, Introductory 24

Modularizing a Program (continued) Determine when to break down any particular program into modules Does not depend on a fixed set of rules Programmers do follow some guidelines Statements should contribute to the same job Functional cohesion A Beginner's Guide to Programming Logic, Introductory 25

Declaring Variables and Constants within Modules Place any statements within modules Input, processing, and output statements Variable and constant declarations Variables and constants declared in a module are usable only within the module Visible In scope Portable Self-contained units that are easily transported A Beginner's Guide to Programming Logic, Introductory 26

Figure 2-3 Program that produces a bill using only main program A Beginner's Guide to Programming Logic, Introductory 27

Figure 2-5 The billing program with constants declared within the module A Beginner's Guide to Programming Logic, Introductory 28

Figure 2-5 The billing program with constants declared within the module A Beginner's Guide to Programming Logic, Introductory 29

Declaring Variables and Constants within Modules (continued) Global variables and constants Declared at the program level Visible to and usable in all the modules called by the program Many programmers avoid global variables to minimize errors A Beginner's Guide to Programming Logic, Introductory 30

Understanding the Most Common Configuration for Mainline Logic Mainline logic of almost every procedural computer program follows a general structure Declarations for global variables and constants Housekeeping tasks Detail loop tasks End-of-job tasks A Beginner's Guide to Programming Logic, Introductory 31

Understanding the Most Common Configuration for Mainline Logic (continued) Figure 2-6 Flowchart and pseudocode of mainline logic for a typical procedural program A Beginner's Guide to Programming Logic, Introductory 32

Creating Hierarchy Charts Hierarchy chart Shows the overall picture of how modules are related to one another Tells you which modules exist within a program and which modules call others Specific module may be called from several locations within a program Planning tool Develop the overall relationship of program modules before you write them Documentation tool A Beginner's Guide to Programming Logic, Introductory 33

Features of Good Program Design Use program comments where appropriate Identifiers should be well-chosen Strive to design clear statements within your programs and modules Write clear prompts and echo input Continue to maintain good programming habits as you develop your programming skills A Beginner's Guide to Programming Logic, Introductory 34

Using Program Comments Program comments Written explanations Not part of the program logic Serve as documentation for readers of the program Syntax used differs among programming languages Flowchart Use an annotation symbol to hold information that expands on what is stored within another flowchart symbol A Beginner's Guide to Programming Logic, Introductory 35

Using Program Comments (continued) Figure 2-12 Pseudocode that declares some variables and includes comments A Beginner's Guide to Programming Logic, Introductory 36

Figure 2-13 Flowchart that includes some annotation symbols A Beginner's Guide to Programming Logic, Introductory 37

Choosing Identifiers General guidelines Give a variable or a constant a name that is a noun (e.g. name, age, testscore) Give a module a verb as an identifier (e.g. runtest) Use meaningful names (e.g. not firstmodule) Self-documenting Use pronounceable names (e.g. not preparead) Be judicious in your use of abbreviations (e.g. not getstat getstatistic? getstatic? getstate?) Avoid digits in a name (e.g. not run0 or run1) A Beginner's Guide to Programming Logic, Introductory 38

Choosing Identifiers (continued) General guidelines (continued) Use the system your language allows to separate words in long, multiword variable names Consider including a form of the verb to be Name constants using all uppercase letters separated by underscores (_) Organizations sometimes enforce different rules for programmers to follow when naming variables Hungarian notation Declarations num numpagecount A Beginner's Guide to Programming Logic, Introductory 39

Choosing Identifiers (continued) Programmers create lists of all variables Data dictionary A Beginner's Guide to Programming Logic, Introductory 40

Designing Clear Statements Avoid confusing line breaks Use temporary variables to clarify long statements A Beginner's Guide to Programming Logic, Introductory 41

Avoiding Confusing Line Breaks Most modern programming languages are freeform Take care to make sure your meaning is clear Do not combine multiple statements on one line input percentage, total = percentage * 100 input percentage total = percentage * 100 input percentage total = percentage * 100 A Beginner's Guide to Programming Logic, Introductory 42

Using Temporary Variables to Clarify Temporary variable Work variable Long Statements Not used for input or output Working variable that you use during a program s execution Consider using a series of temporary variables to hold intermediate results A Beginner's Guide to Programming Logic, Introductory 43

Using Temporary Variables to Clarify Long Statements (continued) Figure 2-14 Two ways of achieving the same salespersoncommission result A Beginner's Guide to Programming Logic, Introductory 44

Writing Clear Prompts and Echoing Prompt Input Message displayed on a monitor to ask the user for a response Used both in command-line and GUI interactive programs Echoing input Repeating input back to a user either in a subsequent prompt or in output A Beginner's Guide to Programming Logic, Introductory 45

Writing Clear Prompts and Echoing Input (continued) Figure 2-15 Beginning of a program that accepts a name and balance as input A Beginner's Guide to Programming Logic, Introductory 46

Figure 2-16 Beginning of a program that accepts a name and balance as input and uses a separate prompt for each item A Beginner's Guide to Programming Logic, Introductory 47

Maintaining Good Programming Habits Every program you write will be better if you: Plan before you code Maintain the habit of first drawing flowcharts or writing pseudocode Desk-check your program logic on paper Think carefully about the variable and module names you use Design your program statements to be easy to read and use A Beginner's Guide to Programming Logic, Introductory 48

Summary Variables Named memory locations with variable contents Equal sign is the assignment operator Break down programming problems into reasonable units called modules Include a header, a body, and a return statement Mainline logic of almost every procedural computer program can follow a general structure As your programs become more complicated: Need for good planning and design increases A Beginner's Guide to Programming Logic, Introductory 49