Professional Documents
Culture Documents
1
Arithmetic
Arithmetic is performed with operators.
Arithmetic operators are listed in following table
Multiplication * bm b * m
Division / x/y x / y
Modulus % r mod s r % s
3
4
Examples comparing mathematical and C++ expressions
5
Operator precedence
Some arithmetic operators act before others
(e.g., multiplication before addition)
Be sure to use parenthesis when needed
Example: Find the average of three variables
a, b and c
Do not use: a + b + c / 3 (incorrect)
Use: (a + b + c ) / 3 (correct)
6
Rules of operator precedence
7
Operator Precedence
20 - 4 / 5 * 2 + 3 * 5 % 4
(4 / 5)
((4 / 5) * 2)
((4 / 5) * 2) (3 * 5)
((4 / 5) * 2) ((3 * 5) % 4)
(20 -((4 / 5) * 2)) ((3 * 5) % 4)
(20 -((4 / 5) * 2)) + ((3 * 5) % 4)
8
Assignment operators
= is the assignment operator
Used to assign a value to a variable
An assignment statement changes the value of a variable
General Form:
identifier = expression;
9
Assignment operators
The ‘=‘ operator in C++ is not an equal sign
The following statement cannot be necessarily true in algebra
number_of_bars = number_of_bars + 3;
10
Assignment expression abbreviations
Program can be written and compiled a bit faster by the use of abbreviated
assignment operators
C++ provides several assignment operators for abbreviating assignment
expressions.
Addition assignment operator
c = c + 3; abbreviated to
c += 3;
Statements of the form
variable = variable operator expression;
can be rewritten as
variable operator= expression;
Other assignment operators
d -= 4 (d = d - 4)
e *= 5 (e = e * 5)
f /= 3 (f = f / 3)
g %= 9 (g = g % 9)
11
Arithmetic assignment operators
+= c += 7 c = c + 7 10 to c
-= d -= 4 d = d - 4 1 to d
*= e *= 5 e = e * 5 20 to e
/= f /= 3 f = f / 3 2 to f
%= g %= 9 g = g % 9 3 to g
12
Increment and Decrement Operators
Increment and decrement operators are unary operators as they
require only one operand.
13
Increment and Decrement Operators
-- -- unary decrement operator
Subtracts 1 from the value of a variable
x --;
is equivalent to x = x – 1;
Pre-decrement
When the operator is used before the variable (--c)
Variable is changed, then the expression it is in is
evaluated.
Post-decrement
When the operator is used after the variable (c--)
Expression the variable is in executes, then the variable is
changed.
14
Increment and Decrement Operators
Example
If c = 5, then
cout << ++c;
c is changed to 6, then printed out
cout << c++;
Prints out 5 (cout is executed before the increment)
c then becomes 6
15
Summarizing increment and decrement operators in a table
16
An example to understand the effect of pre-
increment and post-increment
1 // example
2 // Pre incrementing and post incrementing.
3 #include <iostream.h>
4
5
8 // function main begins program execution
9 int main()
10 {
11 int c; // declare variable
12
13 // demonstrate pos tincrement
14 c = 5; // assign 5 to c
15 cout << c << endl; // print 5
16 cout << c++ << endl; // print 5 then post increment
17 cout << c << endl << endl; // print 6
18
19 // demonstrate pre increment
20 c = 5; // assign 5 to c
21 cout << c << endl; // print 5
22 cout << ++c << endl; // pre increment then print 6
cout << c << endl; // print 6
24
25 return 0; // indicate successful termination
26
27 } // end function main
17
output
Cout<<c prints c =5
Cout<<c++ prints c =5 then increment c by 1 to 6
Cout<<c prints c =6
Cout<<c prints c =5
Cout<<++c first increment c by one to to 6 then prints c =6
Cout<<c prints c =6
18
Precedence of the operators encountered so far
19