Arithmetic Expressions in C++

Arithmetic Expressions in C++
Outline
 Data
declaration {section 2.3}
 Arithmetic operators in C++ {section
2.6}
 Mixed data type arithmetic in C++
 Arithmetic expressions in C++
 Operators’ precedence
 Arithmetic expressions’ evaluation
examples
CSCE 106
2
Data Declaration



Remember the memory anatomy?
Address Contents
It is much easier to set aside memory with
names (identifiers).
x
0
-27.2
Thus the declaration statements are used to set
aside memory with a specific name for the
data and define its values and operations.
n
1
354
type identifier-list;

Examples:
char response;
char c, grade = ‘A’;
int minElement;
int n, j = 1;
float score;
float x, y, z = 40.0;
bool flag;

c2
3
.
.
.
1024
H
ADD 7
.
.
.
75.62
The value can change during execution
CSCE 106
3
Data Declaration (cont’d)
When variables are
declared
1. Memory allocated for
value of specified type
2. Variable name
associated with that
memory location
3. Memory initialized with
values provided (if any)
CSCE 106
27
4
Constant Declarations
A constant is a memory cell whose value cannot
change during execution once it is set in the
declartion.
const type constant-identifier = value;
 E.g.:
const float KM_PER_MILE = 1.609;
 It is a good C++ programming practice to
capitalize all letters for constant names.
 Any data type (e.g. int, float, char, or bool)
could be declared as constant.

CSCE 106
5
Identifiers
 A valid
identifier (variable or function name) must
consist of letters, digits, or underscore only.
 A valid identifier must not begin with a digit.
 Valid identifiers: letter, letter1, _letter
 Invalid identifiers: 1letter, float, hell o
 You cannot use a C++ reserved word as an identifier.
 Always try to associate meaningful identifiers.
 It is a good C++ programming practice to capitalize the
first letter of each word (after the first) when using
multiple word identifiers
 Remember that C++ is case sensitive (cost != Cost)
CSCE 106
6
Arithmetic Operators in C++
+
*
/
%

Addition
Subtraction
Multiplication
Division
Modulus (remainder operator, only for integers)
Examples of integer modulus:
7%2=1
299 % 100 = 99
49 % 5 = 4
15 % 0 undefined

Examples of integer division (result is integer):
15 / 3 = 5
15 / 2 = 7
0 / 15 = 0
15 / 0
CSCE 106
undefined
7
Mixed Data Type Arithmetic in C++
 Example:
4.6 / 2 evaluates to 2.3
Rule: when an integer and a floating point
operand are combined by an operator, the
integer gets converted to the floating point
type.
CSCE 106
8
Arithmetic Expressions in C++
An assignment statement has the following format:
variable = expression;
e.g.:
kms = KM_PER_MILE * miles;
The arithmetic expression to the right of the
assignment operator (=) gets evaluated first
 Then the result is stored in the variable on the
left side of the assignment operator

CSCE 106
9
Arithmetic Expressions in C++
(cont’d)
 Mixed-type assignments:
If the variable on left side of assignment is of different type than
the type of the evaluated expression on the right side of =, the
result of the expression must be converted to the appropriate
type.
 Examples:
float a, b, x;
int m, n;

a = 10;
b = 3.5;
m = 5;
n = 10;
x = m / n;
m = b * 3;
CSCE 106
// result is 10.0 stored in a
// result is 0 assigned to x; therefore x is 0.0
// result is 10 assigned to m
10
Arithmetic Expressions in C++
(cont’d)

What if the arithmetic expression contains more than one
operator?
x = z - (a + b / 2) + w * -y;


How many operators do we have in the above expression?
Computers follow operator precedence rules to evaluate
expressions.
 The formula:
m = y-b
x-a
 The computer equivalent is:
m = (y - b) / (x - a);
Must use ( ) because of operator precedence.
CSCE 106
11
Order of Operator Precedence
Highest ( )
unary +, *, /, %
nested expressions evaluated
inside out
Associativity
If there are several, then
evaluate from left-to-right
binary +, If there are several, then
evaluate from left-to-right
Lowest
=
Warning: watch out for the types of operands and the type
of the result from evaluating each operation!
CSCE 106
12
Example 1
x = z - (a + b / 2) + w * -y;
8 - (3 + 9 / 2) + 2 * --5
5
/
4
+
7
*
10
1
+
x integer
z 8
a 3
b 9
w 2
y -5
x = 8 - (3 + 9 / 2) + 2 * - -5
9/2
(3 + 4 )
87
+2*--5
87
+2* 5
87
+ 10
1
+ 10
11
11
CSCE 106
13
Example 2
m=
x
+
5.5 +
k
/
5 /
2;
m integer
2
x 5.5
k
/
5
2
convert to float
2.0
+
7.5
convert to int
7
CSCE 106
14
Next lecture will be about
Library Functions
CSCE 106
15