C++ Assignment Operators

Advertisements
Advertisements

The assignment operators assign new values to the variable. The variable may already be holding a value ,but the assignment will change the current value to a new one.

It can appear anywhere within an expression, with the sole purpose of assigning the right-hand-side (RHS) value to the left-hand-side (LHS) value.

Here is the list of valid assignment operators.

Assignment OperatorDescription
=LHS is assigned RHS.
+=LHS is added to RHS and then assigned to LHS again.
-=LHS is subtracted to RHS and then assigned to LHS again.
*=LHS is multiplied to RHS and assigned to LHS again.
/=LHS is divided to RHS and assigned back to LHS again.
%=LHS is assigned a remainder value after division between LHS and RHS.
>>=Right shift and then LHS is assigned a value.
<<=Left shift and then LHS is assigned a value.
&=Perform bitwise AND and assign to LHS.
|=Perform bitwise OR and assign to LHS.
~=Perform complement and assign to LHS.

Example Program

In the following example, we will perform all types of assignments and view the results.

#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
    // Variable Declarations
    
    int number1, number2, number3;
    
    
    //Variable initialization uses Assignment Operators
    
    number1 = 23;
    number2 = 34;
    number3 = 12;
    
    cout << "Number1 =" << number1 << endl;
    cout << "Number2 =" << number2 << endl;
    cout << "Number3 =" << number3 << endl;
    
    
    //This is equal to number1 = number1 + 3
    
    number1 += 3; 
    cout << "(Number1 += 3)Equation=" << number1 << endl;
    
    //This is equal to number2 = number2 - 1
    
    number2 -= 1;
    cout << "(Number2 -= 1)Equation=" << number2 << endl;
    
    //This is equal to number3 = number3 * 2
    
    number3 *= 2;
    cout << "(Number3 *= 2)Equation=" << number3 << endl;
    
    //This is equal to number1 = number1 / 2
    
    number1 /= 2;
    cout << "(Number1 /= 2)Equation=" << number1 << endl;
    
    //This is equal to number2 = number2 % 5
    
    number2 % 5;
    cout << "(Number2 %= 5)Equation=" << number2 << endl;
    
    /* Assignment Operators with Shift Operators */
    
    
    number1 = number1 >> 1;
    cout << "(Number1 >>= 1)Equation=" << number1 << endl;
    
    number2 = number2 << 1;
    cout << "(Number2 <<= 1)Equation=" << number2 << endl;
    
    /* Assignment Operators with Logical Operators */
    
    number1 = number1 & 2;
    cout << "(Number1 &= 2)Equation=" << number1 << endl;
    
    number3 = number3 | 5;
    cout << "(Number3 |= 5)Equation=" << number3 << endl;
    
    number2 = ~number2;
    cout << "(Number2 ~= Number2)Equation=" << number2 << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Output:

Number1 =23
Number2 =34
Number3 =12
(Number1 += 3)Equation=26
(Number2 -= 1)Equation=33
(Number3 *= 2)Equation=24
(Number1 /= 2)Equation=13
(Number2 %= 5)Equation=33
(Number1 >>= 1)Equation=6
(Number2 <<= 1)Equation=66
(Number1 &= 2)Equation=2
(Number3 |= 5)Equation=29
(Number2 ~= Number2)Equation=-67

Bestseller

Let Us C++

One of the best selling book in C++ programming language. A perfect book for beginners and people who want to prefer self-study in step by step manner. Simple and easy narration style made this book very popular. The book assumes that you don’t have programming experience and starts with basic to more advanced topics.


Advertisements
Advertisements
Advertisements