Assignment Operators

By | September 27, 2023

Assignment Operators

Assignment operators play a pivotal role in programming by allowing developers to assign values to variables. These operators are fundamental to most programming languages, including C, C++, Java, etc. In this article, we will explore assignment operators, their types, and how they are used to manipulate data in programs.

Recommended Course 

An assignment operator consists of two operands: the left-side operand (a variable) and the right-side operand (a value). The operator aims to assign the value on the right side to the variable on the left side. However, there’s a crucial rule: the value’s data type on the right side must match the variable’s data type on the left side. If they don’t match, the compiler will generate an error.

Here is a list of common assignment operators:

  1. “=” (Simple Assignment Operator)

The most straightforward assignment operator, “=,” assigns the value on the right to the variable on the left. For instance:

c

Copy code

int a = 10;

char ch = ‘y’;

In the first line, the variable a is assigned the value 10, while in the second line, the variable ch is assigned the character ‘y’.

  1. “+=” (Addition Assignment Operator)

The “+=” operator combines addition and assignment. It adds the current value of the variable on the left to the value on the right and assigns the result back to the variable on the left. It can be expressed as follows:

c

Copy code

a += b; // Equivalent to a = a + b;

Suppose the initial value stored in a is 5. After executing (a += 6), a value becomes 11.

  1. “-=” (Subtraction Assignment Operator)

The “-=” operator combines subtraction and assignment. It subtracts the current value of the variable on the left from the value on the right and assigns the result back to the variable on the left. It can be expressed as follows:

c

Copy code

a -= b; // Equivalent to a = a – b;

If the initial value stored in a is 8, after executing (a -= 6), a value becomes 2.

  1. “*=” (Multiplication Assignment Operator)

The “*=” operator combines multiplication and assignment. It multiplies the current value of the variable on the left by the value on the right and assigns the result back to the variable on the left. It can be expressed as follows:

c

Copy code

a *= b; // Equivalent to a = a * b;

If the initial value stored in a is 5, after executing (a *= 6), a value becomes 30.

  1. “/=” (Division Assignment Operator)

The “/=” operator combines division and assignment. It divides the current value of the variable on the left by the value on the right and assigns the result back to the variable on the left. It can be expressed as follows:

c

Copy code

a /= b; // Equivalent to a = a / b;

If the initial value stored in a is 6, after executing (a /= 2), a value becomes 3.

Putting Assignment Operators to Work

To demonstrate the use of assignment operators, let’s consider a simple C program:

c

Copy code

#include <stdio.h>

int main() {

    int a = 10;

    printf(“Value of a is %d\n”, a);

    a += 10;

    printf(“Value of a is %d\n”, a);

    a -= 10;

    printf(“Value of a is %d\n”, a);

    a *= 10;

    printf(“Value of a is %d\n”, a);

    a /= 10;

    printf(“Value of a is %d\n”, a);

    return 0;

}

When executed, this program will output the following:

csharp

Copy code

value of a is 10

value of a is 20

value of a is 10

value of a is 100

value of a is 10

This output illustrates how assignment operators modify variable A’s value through various arithmetic operations. Assignment operators are fundamental tools in programming, allowing developers to manipulate variables by assigning values based on different operations. Understanding these operators’ behavior is crucial for writing efficient and error-free code. As you continue your programming journey, you’ll encounter these operators frequently and become skilled at using them to build robust and dynamic applications.

Assignment Operators Frequently Asked Questions

Q1.  What’s the operator of an assignment? 

Ans. The assignment operator =Assigns the value of its right-hand operand to a variable, property, or indexer element given by a left-hand operand. The result of an assignment expression is the value assigned to the left-hand operand.

Q2. What are assignment operators in C++?

Ans. The Assignment Operator in C++ is a Binary Operator that uses the symbol “=.” It requires a variable as the left operand and a value to be assigned as the right operand.

Q3. Define add and assignment operators.

Ans. The add and assignment operator adds the value on the right to the left and stores the result on the left. The subtract and assignment operator subtracts the value on the right from the left and stores the result on the left.

Telegram Group Join Now
WhatsApp Channel Join Now
YouTube Channel Subscribe
Scroll to Top
close
counselling
Want to Enrol in PW Skills Courses
Connect with our experts to get a free counselling & get all your doubt cleared.