?>
profile image
Announcements
No new announcements.
mentora icon
bookmark
Table of Contents

    Assignment Operators in C++ Programming

    In this lesson, we are going to look at what is assignment operator and will learn the different types of assignment operator with the help of examples.

    What is Assignment Operator

    Assignment Operator in C++ is used for assigning value to a variable. Using the assignment operator we can assign the value which is on the right side of the operator to the variable on the left side of the operator.

    Video Explanation

    Click the button below to watch the explanation.

    video image

    Let's discuss about the different types of assignment operators with examples.

    Assign Operator =

    The Assign Operator = is used to assign a value to a variable. It takes the value on its right-hand side and assigns it to the variable on its left-hand side.

    Example
    int a, b;
    a=5;    // value 5 is assigned to variable a
    b=a;    // value of variable a which is 5 is assigned to the variable b

    As you can see the value 5 which is on the right side of the operator = has been assigned to the left in the variable a. In the same way value of the variable a (5) which is on the right side of the operator = has been assigned to the left in the variable b

    Note

    The lines started with // are called comments which are used to provide information about lines of code. We will learn more about comments in our upcoming lessons.

    Add and Assign Operator +=

    The Add and Assign Operator += is a combination of + and = operators. This operator adds the value of the right-hand operand to the left-hand operand and then assigns the result back to the left-hand operand. It is a shorthand way to perform addition and assignment in a single step.

    Example
    int a=2, b=10, c=5;
    a+=2;    // can be written as a=a+2 (a=2+2) result is a=4
    b+=c;    // can be written as b=b+c (b=10+5) result is b=15

    Subtract and Assign Operator -=

    The Subtract and Assign Operator -= is a combination of - and = operators. This operator subtracts the value of the right-hand operand from the left-hand operand and then assigns the result back to the left-hand operand. It is a shorthand way to perform subtraction and assignment in a single step.

    Example
    int a=6, b=10, c=5;
    a-=2;    // can be written as a=a-2 (a=6-2) result is a=4
    b-=c;    // can be written as b=b-c (b=10-5) result is b=5

    Multiply and Assign Operator *=

    The *= operator is a combination of * and = operators. This operator multiplies the value of the left-hand operand by the right-hand operand and then assigns the result back to the left-hand operand. It is a shorthand way to perform multiplication and assignment in a single step.

    Example
    int a=7, b=10, c=5;
    a*=2;    // can be written as a=a*2 (a=7*2) result is a=14
    b*=c;    // can be written as b=b*c (b=10*5) result is b=50

    Divide and Assign Operator /=

    The /= operator is a combination of / and = operators. This operator divides the value of the left-hand operand by the right-hand operand and then assigns the result back to the left-hand operand. It is a shorthand way to perform division and assignment in a single step.

    Example
    int a=18, b=10, c=5;
    a/=2;    // can be written as a=a/2 (a=18/2) result is a=9 (quotient)
    b/=c;    // can be written as b=b/c (b=10/5) result is b=2 (quotient)

    Modulus and Assign Operator %=

    The %= operator is a combination of % and = operators. This operator calculates the remainder of the division of the left-hand operand by the right-hand operand and then assigns the result back to the left-hand operand. It is a shorthand way to perform modulus and assignment in a single step.

    Example
    int a=6, b=10, c=8;
    a%=2;    // can be written as a=a%2 (a=6%2) result is a=0 (remainder)
    b%=c;    // can be written as b=b%c (b=10%8) result is b=2 (remainder)

    Test Your Knowledge

    Attempt the questions in the exercise to check whether you have understood the lesson clearly.

    Test Your Knowledge
    Mentora Question Asked: 0 / 20

    Hi, I’m Mentora!

    I’m your personal AI tutor from Dremendo. Ask me anything, and I’ll help you understand it in a simple and easy way.