You are on page 1of 5

Mid Term Assignment

Programming Fundamentals:

Name: Ahmed Ali

Section: C

Department: BSCS

Roll number: 123

Submitted to: Mam Sarah Ilyas


Q:- Program to perform all compound assignment
operations on an integer ?

Compound assignment operators

*// write a program that performs all compound assignment operations on an integer.

#include<iostream>
using namespace std;
int main()
//Program to perform all compound assignment operations on an
integer
{
cout << "all compound operation on an integer:\n\n";
int a=5,b=5;
a+=b;
cout << "the value is :"<< a << '\n';
a-=b;
cout << "the value is :"<< a << '\n';
a*=b;
cout << "the valus is :"<< a << '\n';
a/=b;
cout << "the valus is :"<< a << '\n';
a%=b;
cout << "the value is :"<< a << '\n';

system("pause");

return 0;

Result:
10
5
25
5
0

Definition of compound assignment operator:


The compound-assignment operators combine the simple-assignment operator with another
binary operator. Compound-assignment operators perform the operation specified by the
additional operator, and then assign the result to the left operand. For example, a compound-
assignment expression such as. expression1 += expression2.

Example:

n += 10;
is equivalent to
n = n + 10

Both assignments are equivalent and perform the same operation. Both statements add 10 in the
current value of n. all the other mathematical operators can also be combined with assignment
operator. Some examples of assignment operators are as follows

A += 10                                 is equivalent to A = A + 10
A -= 10                                  is equivalent to A = A – 10
A *= 10                                 is equivalent to A = A * 10
A /= 10                                  is equivalent to A = A / 10
A %= 10                                is equivalent to A = A % 10

The addition-assignment (+=) and subtraction-assignment (-=) operators can also have a
left operand of pointer type, in which case the right-hand operand must be of integral
type. The result of a compound-assignment operation has the value and type of the left
operand

#define MASK 0xff00

n &= MASK;

In this example, a bitwise-inclusive-AND operation is performed and


MASK, and the result is assigned to n. The manifest constant MASK is
defined with a #define preprocessor directive.
--- END ---
--- THANKS MAM! ---

You might also like