Home | Computer | Assignment Operators in C++ | Compound Assignment Operators in C++

Assignment Operators in C++ | Compound Assignment Operators in C++

August 23, 2022

A statement used to assign the value to a variable is known as an assignment statement. The assignment operator = is used to assign a value or computational result to a variable.

The name of the variable is written on the left side of the assignment operator and the value is written on the right side of the assignment operator.  The value can be in different forms like a constant variable, expression, or function.

OperatorExampleEquivalent to
+=A += 10A = A + 10
-=A -= 10A = A – 10
*=A *= 10A = A * 10
/=A /= 10A = A / 10
%=A %=10A = A % 10
&=A &= 10A = A & 10
=A = 10A = 10
^=A ^=10A = A ^ 10
>>=A >>= 10A = A >> 10
<<=A <<= 10A = A << 10

Syntax of assignment operators in C++

The syntax of writing an assignment statement is as follows:

Variable = expression

Variable:  It is the name of the variable to which the value is assigned.

=   It is the assignment operator used to assign the value to the variable.

Expression:   It is the expression whose return value is assigned to the variable. It can be a constant, variable, or combination of operand and arithmetical operators.

First, the value of the right side is evaluated. Then the value is assigned to the variable on the left side.

Example

A = 100;

C = A + B;

X = C – D + 10;

#include <iostream.h>

#include <conio.h>

Void main()

{

clrscr();

int a,b;

a = 10;

b = 5;

cout<<“a + b = “<<a + b<<endl;

cout<<“a – b = “<<a – b<<endl;

cout<<“a * b = “<<a * b<<endl;

cout<<“a / b = “<<a / b<<endl;

cout<<“a % b = “<<a % b<<endl;

getch();

}

Output

a + b = 15

a – b = 5

a * b = 50

a / b = 2

a % b = 0

Compound Assignment Operators

An assignment statement that assigns a value to many variables is known as a compound assignment. The assignment operator = is used in this statement.

Example

A = B = 10;

Assigns the value 10 to both A and B. Some examples of compound assignment statements are as follows:

X = y = z = 100;

m = n = 50;

Syntax of compound assignment operator in C++

The general syntax of the compound assignment operator is as follows:

Variable op = expression

Variable:   The variable to assign a value.

Operator:  It can be any operator.

Expression: It can be a constant, variable, or arithmetic expression.

Example

N += 10;

Is equivalent to

N = N + 10;

Both statements are equivalent and perform the same operation. Both statements add 10 to the current value of N. All other mathematical operations can also be combined with the assignment operator.

 #include <iostream.h>

#include <conio.h>

Void main()

{

clrscr();

int a,b;

a = 10;

printf(” value of a: %d /n”,a);

a += 5;

printf(” value of a after a += 5: %d /n”,a);

a -= 5;

printf(” value of a after a -= 5: %d /n”,a);

a *= 2;

printf(” value of a after a *= 2: %d /n”,a);

a /= 2;

printf(” value of a after a /= 2: %d /n”,a);

a %= 2;

printf(” value of a after a %= 2: %d /n”,a);

getch();

}

Output

Value of a: 10

Value of a after a += 5: 15

Value of a after a -= 5: 10

Value of a after a *= 2: 20

Value of a after a /= 2: 10

Value of a after a %= 2: 0