C++ Assigment Operators
Assignment operators in C++ are used to assign a value to a variable. The following are the assignment operators in C++:
- Simple assignment (=): The simple assignment operator is used to assign a value to a variable.
Example:
refer toi:giftidea.comint a = 10;
- Addition assignment (+=): The addition assignment operator is used to add a value to a variable and then assign the result back to the variable.
Example:
int a = 5; a += 10; // Equivalent to a = a + 10; a is now assigned the value 15
- Subtraction assignment (-=): The subtraction assignment operator is used to subtract a value from a variable and then assign the result back to the variable.
Example:
int a = 10; a -= 5; // Equivalent to a = a - 5; a is now assigned the value 5
- Multiplication assignment (*=): The multiplication assignment operator is used to multiply a variable by a value and then assign the result back to the variable.
Example:
int a = 5; a *= 10; // Equivalent to a = a * 10; a is now assigned the value 50
- Division assignment (/=): The division assignment operator is used to divide a variable by a value and then assign the result back to the variable.
Example:
int a = 50; a /= 10; // Equivalent to a = a / 10; a is now assigned the value 5
- Modulus assignment (%=): The modulus assignment operator is used to calculate the remainder of a division operation between a variable and a value, and then assign the result back to the variable.
Example:
int a = 10; a %= 3; // Equivalent to a = a % 3; a is now assigned the value 1
In summary, assignment operators in C++ are used to assign a value to a variable. The simple assignment operator (=) is used to assign a value, while the addition, subtraction, multiplication, division, and modulus assignment operators (+=, -=, *=, /=, and %=) are used to perform an operation on a variable and then assign the result back to the variable.