C++ Compound Assignment Operators

Introduction

Compound assignment operators allow us to perform an arithmetic operation and assign a result with one operator:

+= // compound addition 
-= // compound subtraction 
*= // compound multiplication 
/= // compound division 
%= // compound modulo 

Example:

#include <iostream> 

int main() /*from  www  .j  a v  a  2 s  .  com*/
{ 
    int x = 123; 
    x += 10;    // the same as x = x + 10 
    x -= 10;    // the same as x = x - 10 
    x *= 2;     // the same as x = x * 2 
    x /= 3;     // the same as x = x / 3 
    std::cout << "The value of x is: " << x; 
} 



PreviousNext

Related