Saral Code logo
Saral Code
    HomeBlogTutorialsMCQs

No Related Topics Available


Assignment operators are used to assign a value to a variable in C++. These operators include simple assignment (=), as well as compound assignment operators like +=, -=, *=, /=, and %= which combine an arithmetic operator and assignment into a single operation.

Here are some examples of how to use these operators:

Simple Assignment (=)

The simple assignment operator is used to assign a value to a variable. Here's an example:

C++
int a;
a = 10; // a now has the value of 10

Addition Assignment (+=)

The addition assignment operator is used to add a value to a variable and assign the result to the variable. Here's an example:

C++
int a = 10;
a += 5; // a now has the value of 15

This is equivalent to writing a = a + 5.

Subtraction Assignment (-=)

The subtraction assignment operator is used to subtract a value from a variable and assign the result to the variable. Here's an example:

C++
int a = 10;
a -= 5; // a now has the value of 5

This is equivalent to writing a = a - 5.

Multiplication Assignment (*=)

The multiplication assignment operator is used to multiply a variable by a value and assign the result to the variable. Here's an example:

C++
int a = 10;
a *= 5; // a now has the value of 50

This is equivalent to writing a = a * 5.

Division Assignment (/=)

The division assignment operator is used to divide a variable by a value and assign the result to the variable. Here's an example:

C++
int a = 10;
a /= 5; // a now has the value of 2

This is equivalent to writing a = a / 5.

Modulus Assignment (%=)

The modulus assignment operator is used to take the modulus of a variable by a value and assign the result to the variable. Here's an example:

C++
int a = 10;
a %= 3; // a now has the value of 1

This is equivalent to writing a = a % 3.

I hope this helps you to understand assignment operators in C++!