Arithmetic Operators
and Expression
Defining Variables
A variable must be defined before you can use it in a
program.
Syntax: type name1 ,name2 , ...;
Example:
char c;
int i, counter;
double x, y, size;
Initialization Variables
A variable can be initialized, a value can be assigned to the
variable, during its definition.
An equals sign = and an initial value for the variable.
Round brackets containing the value of the variable.
Examples:
char c = 'a';
float x(1.875);
Constant Objects
The const keyword is used to create a “read only” object.
If the object type is constant, it cannot be modified and must be
initialized during its definition.
Examples:
const double pi = 3.1415947;
The value of pi cannot be modified by the program.
pi = pi + 2.0; // invalid
Arithmetic Operators and Expression
Arithmetic operators are used to perform calculations.
Arithmetic expression is using arithmetic operators and
numbers or operands.
The Binary Arithmetic Operators
Binary operator: An operator that has two operands.
The Unary Arithmetic Operators
Unary operator: An operator that has only one operand.
Sign Operators
The sign operator – returns the value of the operand but inverts
the sign.
Examples:
int n = –5; cout << -n; // Output: 5
Increment / Decrement Operators
The increment operator ++ modifies the operand by adding 1 to its value.
Given that i is a variable, both i++ (postfix notation) and ++i (prefix
notation) raise the value of i by 1.
The difference is:
++i i is incremented first and the new value of i is then applied.
i++ the original value of i is applied before i is incremented.
The decrement operator -- modifies the operand by reducing the value of
the operand by 1.
Simple Assignments
A simple assignment uses the assignment operator = to assign
the value of a variable to an expression.
Examples:
z = 7.5;
y = z;
x = 2.0 + 4.2 * z;
Multiple Assignments
Multiple assignments always evaluated from right to
left.
Example: i = j = 9;
The value 9 is first assigned to j and then to i.
Compound Assignments
Compound assignment operators that simultaneously perform
an arithmetic operation and an assignment.
Examples:
i += 3; is equivalent to i = i + 3;
i *= j + 2; is equivalent to i = i * (j+2);
Compound assignment operators can be composed from any
binary arithmetic operator ( += , -= , *= , /= , %=)
Order of Precedence (Priority Rules)
When more than one arithmetic operator is used in an
expression, uses the operator precedence rules to evaluate the
expression.
*, /, % are at a higher level of precedence than: +, -
The operations are performed from left to right.
parentheses have priority.
Example : 3*7-6+2*5/4+6
(((3 * 7) – 6) + ((2 * 5) / 4 )) + 6 3*7
= ((21 – 6) + ((2 * 5) / 4)) + 6 2*5
= ((21 – 6) + (10 / 4)) + 6 10/4
= ((21 – 6) + 2) + 6 21-6
= (15 + 2) + 6 15+2
= 17 + 6 17+6
= 23
3+4*5
*is evaluated before +.
The result of this expression is 23.
(3 + 4) * 5
+ is evaluated before * and
The result of this expression is 35.
Exercise
Write a C++ program that two defines variables for floating-point
numbers and initializes them with the values
123.456 and 76.543
Then display the sum and the difference of these two numbers
on screen.
Special Characters