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

Operators

The document discusses different types of operators in C++ including arithmetic assignment operators like +=, -=, *=, /=, and %=; increment and decrement operators like ++var, var++, --var, and var--; and how they update variables. It provides examples of how each operator works, such as x += 5; meaning x = x + 5; and how increment/decrement operators affect variable values whether used before or after a variable.

Uploaded by

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

Operators

The document discusses different types of operators in C++ including arithmetic assignment operators like +=, -=, *=, /=, and %=; increment and decrement operators like ++var, var++, --var, and var--; and how they update variables. It provides examples of how each operator works, such as x += 5; meaning x = x + 5; and how increment/decrement operators affect variable values whether used before or after a variable.

Uploaded by

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

Operators Classification

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;

RULE: The right hand side is evaluated first, then the


combined assignment operation is done.
x *= a + b; means x = x * (a + b);
Increment and Decrement Operators

Operator Name Description


++var pre-increment The expression (++var) increments var by 1 and
evaluates to the new value in var after the increment.

var++ post-increment The expression (var++) evaluates to the original value in


var and increments var by 1.

--var pre-decrement The expression (--var) decrements var by 1 and


evaluates to the new value in var after the decrement.

var-- post-decrement The expression (var--) evaluates to the original value in


var and decrements var by 1.
Increment and Decrement Operators

Evaluate the followings:

int val = 10;


int result = 10 * val++;
cout<<val<<“ “<<result;

int val = 10;


int result = 10 * ++val;
cout<<val<<“ “<<result;
Increment and Decrement Operators
• Output of the following code:

int x = 5,y = 5,
z; x = ++x;
y = --y;
z = x++ +
y- - ; cout <<
z;
Increment and Decrement Operators

• Output of the following code:

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!

You might also like