Programming Logic and Design: Seventh Edition
Programming Logic and Design: Seventh Edition
Seventh Edition
Chapter 2
Elements of High-Quality Programs
Objectives
In this chapter, you will learn about:
• Declaring and using variables and constants
• Assigning values to variables [assignment statement]
• Initializing a variable
• Performing arithmetic operations
• The advantages of modularization
• Hierarchy charts [aka structure charts]
• Features of good program design
• Different forms
– Variables
– Literals ( unnamed constants )
– Named constants ( cannot be modified )
Programming Logic & Design,
3 Seventh Edition
Working with Variables
• A variable is a named location in RAM where data is stored for use by a running
program.
• Contents can vary or differ over time
• Declaration
– Statement that provides a data type and an identifier for a variable.
– Most programming languages require you to declare a variable before you try to use it in
your program.
• Identifier
– Something you provide a name for:
• variable
• named constant
• module name
• Assignment
– A variable gets a value as the result of an assignment statement
Programming Logic & Design,
4 Seventh Edition
Working with Variables (continued)
A later example will show declarations
• Initializing a variable
– Providing a starting value for any variable
– Can be done as part of the declaration e.g. num count = 0
• Type Safety
– a feature of most languages that prevents assigning values of an incorrect data type
Start
main( )
Stop
• Using “_”
– hourly_wage, number_doubler, total_cost
• Variable names used throughout book
– Must be one word (no spaces)
– Should have a name which conveys meaning!
Programming Logic & Design,
9 Seventh Edition
Understanding Literals and their Data Types
– Numeric:
• Specific numeric value
– Examples: 43 -3 1415 2.35486 e8 .6 5. 0
– String:
• String of characters enclosed within quotation marks
– Examples: "Hello World", "Sum is ", "" (empty string)
– Character:
• A single character ( usually enclosed in single quotes )
– Examples: ‘A’, ‘c’, ‘\n’ [newline character in Java]
Programming Logic & Design,
10 Seventh Edition
Understanding the Data Types of Variables
• Numeric variable
– Usually signed (may be negative or positive)
– Integer ( whole numbers only )
– Real ( may contain decimal digits )
• String variable
– Can hold any sequence of 0 or more characters
– Letters of the alphabet (upper- and lower-case), digits 0-9
– Special characters such as punctuation marks
• Character variable
– Can hold a single character
• Magic number
– A literal
– Purpose is not immediately apparent
– Avoid this! Use a named constant instead
• Assignment operator
– Usually an Equal sign ( = ). Some languages use ( := )
– Association is from right to left a = b = c = 0;
• Valid
– set someNumber = 2 [ Note: Basic allows use of let ]
• someNumber = 2
– set someNumber = someOtherNumber
• someNumber = someOtherNumber
• Not valid
– set 2 + 4 = someNumber [ variable must be on left side of assignment operator ]
• Associativity
– Operations with the same precedence are usually
evaluated from left to right.
x = 5 * (7 / 3) + 6 – 2 + 3 * 5
• Module
– Subunit of a programming problem
– Also called subroutine, procedure, function, or method
• Modularization
– Breaking down a large program into modules [functional decomposition]
– Reasons:
• Divide and conquer strategy, aka stepwise refinement
• Abstraction ( focus on mainline logic first )
• Allows multiple programmers to work on a problem
• Reuse your work more easily
• Reliability
– Feature of programs that assures you a module has been
tested and proven to function correctly
• Parts of a module
– Header <return type> <module name> <module parameter list>
– Body statements return statement
• Naming a module
– A module name is an identifier
– Similar to naming a variable
– Module names are usually followed by a set of parentheses
• Flowchart
– Symbol used to call a module is a rectangle with a line
across the top or along the sides
– Place the name of the module you are calling inside the
rectangle
– Draw each module separately with its own terminal
symbols ( module name / return )
Programming Logic & Design,
23 Seventh Edition
Figure 2-3 Program that produces a bill using only main program logic
Programming Logic & Design,
24
Seventh Edition
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
– Part of the process of abstraction
– Statements should contribute to the same job (task)
• Functional cohesion
• Most newer languages require you to create a module for the mainline
logic with a specific name, such as main. [Java – main()]
• Some languages do not have a “named” mainline logic module.
Programming Logic & Design,
29 Seventh Edition
Understanding the Most Common
Configuration for Mainline Logic (continued)
Here is another
flowchart symbol
used to represent
a module.
Figure 2-6 Flowchart and pseudocode of mainline logic for a typical procedural program
• Flowchart
– Use an annotation symbol to hold information that
expands on what is stored within another flowchart symbol
Figure 2-12 Pseudocode that declares some variables and includes comments
• Temporary variable
– Work variable
– Not used for input or output
– Working variable that you use during a program’s
execution
• Prompt
– Message displayed on a monitor to ask the user for a
response
– Used both in command-line and GUI interactive programs
• Echo input
– Repeat input back to a user either in a subsequent prompt
or in output.