0% found this document useful (0 votes)
34 views

An Introduction To Programming With C++ Eighth Edition: Variables and Constants

v

Uploaded by

Saaliha Saabira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

An Introduction To Programming With C++ Eighth Edition: Variables and Constants

v

Uploaded by

Saaliha Saabira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 34

An Introduction to Programming with C++

Eighth Edition

Chapter 3:
Variables and Constants

© 2016 Cengage Learning®. May not be scanned, copied or


duplicated, or posted to a publicly accessible website, in
whole or in part.
Chapter Objectives

• Distinguish among a variable, a named constant, and a


literal constant
• Explain how data is stored in memory
• Select an appropriate name, data type, and initial value
for a memory location
• Declare a memory location in C++

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 2
whole or in part.
Beginning Step 4 in the Problem-Solving
Process
• After Step 3, programmer has an algorithm and has
desk-checked it
• The fourth step in the process is coding the algorithm
into a program
• The step begins by assigning a descriptive name, data
type, and (optionally) initial value to each unique input,
processing, and output item in the IPO chart
• These are used to store the item in the computer’s
internal memory

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 3
whole or in part.
Internal Memory

• Computer’s internal memory is composed of memory


locations, each with a unique numeric address
• Similar to collection of storage bins
• Each address can store one item at a time
• Address can contain numbers, text, or program
instructions
• To use a memory location, programmer must reserve
the address, called declaring

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 4
whole or in part.
Internal Memory (cont’d.)

• Declaring a memory location is done with an instruction


that assigns a name, data type, and (optional) initial
value
• The name allows the programmer to refer to the
memory location elsewhere in the program using a
descriptive word, rather than the numeric address
• The data type indicates what type of information the
address will store (e.g., number or text)

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 5
whole or in part.
Internal Memory (cont’d.)

• Two types of memory locations can be declared:


variables and named constants
• Variables are memory locations whose values can
change during runtime (when the program is running)
• Most memory locations are variables
• Named constants are memory locations whose values
cannot change during program execution

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 6
whole or in part.
Internal Memory (cont’d.)

Figure 3-1 Illustration of show boxes and memory locations


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 7
whole or in part.
Selecting a Name for a Memory Location

• Name (identifier) assigned to a memory location should


be descriptive
• Should help the programmer/other programmers
remember/understand the memory location’s purpose
• Should be as short as possible while still being
descriptive (especially if referenced often)
• Short names are easier to read and result in more
concise code

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 8
whole or in part.
Selecting a Name for a Memory Location
(cont’d.)
• Rules for memory location names in C++
– Name must begin with a letter and contain only letters,
numbers, and the underscore character
– No punctuation marks, spaces, or other special
characters (such as $ or %) are allowed
– Cannot be a keyword (word that has special meaning in
C++)
– Names are case sensitive
• Example: discount is different from DISCOUNT and
from Discount

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 9
whole or in part.
Selecting a Name for a Memory Location
(cont’d.)
• Most programmers use uppercase letters for named
constants and lowercase for variables
– Example: PI (constant), radius (variable)
• If constants contain more than one word, separate
words with underscores
– Example: TAX_RATE
• If variables contain more than one word, capitalize the
first letter of each word after the first (called camel
case)
– Example: adjustedGrossIncome

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 10
whole or in part.
Selecting a Name for a Memory Location
(cont’d.)

Figure 3-2 How to name a memory location in C++


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 11
whole or in part.
Revisiting the Addison O’Reilly Problem

Figure 3-3 Problem specification, IPO chart,


and desk-check table from Chapter 2
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 12
whole or in part.
Revisiting the Addison O’Reilly Problem
(cont’d.)
• IPO chart contains four input, processing, and output
items
• Four memory locations are needed to store the values
of the items
• Memory locations will be variables since their values
will change during runtime

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 13
whole or in part.
Revisiting the Addison O’Reilly Problem
(cont’d.)

Figure 3-4 Names of the variables for the Addision O’Reilly problem

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 14
whole or in part.
Selecting a Data Type for a Memory
Location
• Memory locations come in different types and sizes
• Type and size you choose depends on the item you
want to store
• A memory location will only accept an item that
matches its data type
• Data type of a memory location is determined by the
programmer when declaring the location

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 15
whole or in part.
Selecting a Data Type for a Memory
Location (cont’d.)
• Fundamental data types are basic data types built into
C++
– Also called primitive or built-in data types
– Include short, int, float, double, bool, and
char
• bool data type stores Boolean values (true and false)
• short and int types store integers (numbers without
a decimal place)
– Differences are range of values and memory used (int
has the greater of both)

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 16
whole or in part.
Selecting a Data Type for a Memory
Location (cont’d.)
• float and double types store real numbers
(numbers with a decimal place)
– Differences are range of values, precision, and memory
used (double has the greater of each)
• char type stores characters (letter, symbol, or number
that will not be used in a calculation)
– Only one character stored at a time
• string data type is a user-defined data type (defined
with a class, or group of instructions)
– Can store zero or more characters

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 17
whole or in part.
Selecting a Data Type for a Memory
Location (cont’d.)

Figure 3-5 Most commonly used data types in C++


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 18
whole or in part.
Selecting a Data Type for a Memory
Location (cont’d.)

Figure 3-6 Data type assigned to each


variable for the Addison O’Reilly problem

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 19
whole or in part.
Selecting an Initial Value for a Memory
Location
• Setting an initial value for a variable or named constant
is called initializing
• Required for constants; recommended for variables
• Memory locations are usually initialized with a literal
constant (item of data that can appear in a program
instruction and be stored in memory)
• Data type of literal constant should match data type of
memory location it is assigned to

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 20
whole or in part.
Selecting an Initial Value for a Memory
Location (cont’d.)

• Numeric literal constants initialize short, int, float,


and double data types
– Can contain digits 0 through 9, +, -, ., and e or E (for
scientific notation)
• Character literal constants initialize char data types
– Consist of one character in single quotation marks
• String literal constants initialize string data types
– Zero or more characters enclosed in double quotation
marks
– Empty string (“”) is a valid string literal constant
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 21
whole or in part.
Selecting an Initial Value for a Memory
Location (cont’d.)
• Before assigning initial value to a memory location,
computer checks that value’s data type matches
location’s data type
• If they don’t match, computer performs implicit type
conversion to match them
– If initial value is converted to type that holds larger
numbers, value is promoted
– If initial value is converted to type that only holds smaller
numbers, value is demoted
• Promoting will not usually have adverse effects, but
demoting can (information is lost)
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 22
whole or in part.
Selecting an Initial Value for a Memory
Location (cont’d.)

• Important to initialize memory locations with values of


the same data type
• Named constants should be initialized with the value
they will hold for the duration of the program
• Variables whose initial values are not known should still
be initialized
– short and int types usually initialized to 0
– float and double types usually initialized to 0.0
– string types usually initialized to empty string (“”)
– bool types initialized to either true or false
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 23
whole or in part.
Selecting an Initial Value for a Memory
Location (cont’d.)

Figure 3-11 Initial values for the variables


in the Addison O’Reilly problem

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 24
whole or in part.
Declaring a Memory Location

• Variables and named constants are declared using a


statement (C++ instruction)
• A statement that declares a variable causes the
computer to set aside a memory location with the given
name, data type, and initial value
• Statements must follow correct syntax (rules of a
programming language)
• In C++, all statements must end with a semicolon

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 25
whole or in part.
Declaring a Memory Location (cont’d.)

• When declaring variables, a data type and name must


be provided
• Syntax for declaring a variable in C++
– dataType variableName [= initialValue];
• After variable is declared, you use its name to refer to it
later in the program
• Initial value is optional but recommended
• If variable is not initialized, it contains the previous
value of that memory location, which may be the wrong
type (called a garbage value)

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 26
whole or in part.
Declaring a Memory Location (cont’d.)

• Syntax for declaring a named constant in C++


– const dataType constantName = value;
• The const keyword indicates that the memory location
is a named constant (value cannot be changed during
runtime)
• Initial value required for constants, unlike variables
• As with variables, after declaring a constant, you can
use its name to refer to it later in the program

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 27
whole or in part.
Declaring a Memory Location (cont’d.)

• Several advantages to using named constants when


appropriate
– Make program more self-documenting (meaningful words
in place of numbers)
– Value cannot be inadvertently changed during runtime
– Typing a name is less error-prone than a long number
– Mistyping a constant’s name will trigger a compiler error;
mistyping a number will not
– If the constant needs to be changed when modifying the
program, it only needs to be changed in one place

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 28
whole or in part.
Declaring a Memory Location (cont’d.)

Figure 3-12 How to declare a variable in C++

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 29
whole or in part.
Declaring a Memory Location (cont’d.)

Figure 3-13 C++ declaration statements for the


variables in the Addision O’Reilly problem

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 30
whole or in part.
Declaring a Memory Location (cont’d.)

Figure 3-14 How to declare a named constant in C++

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 31
whole or in part.
Summary

• Fourth step in problem-solving process is coding the


algorithm
• Memory location is declared for each input, processing,
and output item in IPO chart
• Numeric data is stored in computer’s internal memory
using binary number system
• Memory locations store one item at a time
• Memory location’s data type determines how a value is
stored and interpreted when retrieved

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 32
whole or in part.
Summary (cont’d.)

• Two types of memory locations: variables and named


constants
• Memory locations are declared using a statement that
assigns a name, data type, and initial value
• Initial value required for named constants but optional
for variables (though recommended)
• Most memory locations initialized with literal constants,
except bool (initialized with keywords true or
false)

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 33
whole or in part.
Summary (cont’d.)

• Data type of literal constant assigned to memory


location should be same as memory location’s type
• If types don’t match, implicit type conversion is used to
either promote or demote so they match
• Promoting doesn’t usually cause problems, but
demoting can
• Syntax for declaring variables
– dataType variableName [= initialValue];
• Syntax for declaring named constants
– const dataType constantName = value;

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in 34
whole or in part.

You might also like