Operators
Operators
Combined Assignment
• Also consider it “arithmetic” assignment
• Updates a variable by applying an arithmetic
operation to a variable
• Operators: += -= *= /= %=
• Example:
sum += amt; is short for sum = sum +
amt;
p += 3 +
means p = p +(3+y);
y;
More Examples
x += 5; means x = x + 5;
x -= 5; means x = x – 5;
x *= 5; means x = x * 5;
x /= 5; means x = x / 5;
x %= 5; means x = x % 5;
int x = 5,y = 5,
z; x = ++x;
y = --y;
z = x++ +
y- - ; cout <<
z;
Increment and Decrement Operators
int num1 =
5 ; i n t num2
= 3; i n t
num3 = 2 ;
num1 =
num2++; num2
= - - num3;
cout <<
num1 <<
num2 <<
num3
Examples…
int
a=1;
int b;
b = ++a
* ++a;
cout<<a:
"<<a<<",
b:
"<<b;
cout<<en
dl;
a=1; b
= a++ *
a++;
Any Questions!