Composite Assigment Operators. (Operadores de Asignación en C++. )

The standard assignment operator in C++ is the equals sign =. In addition to this operator, C++ also includes the following composite assignment operators: +=, -=, *=, /=, and %=.

We make an example to show the functionality

//: Date        :July 2011
//: Author      :"Guitar-Player." aol.com.mx>
//: Version     :
//: Description :Applying composite arithmetic assignment operators.
//: Options     :Nothing.
 
///////////////////////////////SOURCE CODE////////////////////////////////
 
//Headers.
#include
#include
using namespace std;
 
//Body program.
int main()
{
int n=22;
cout << "n = " << n << endl;
n += 9; // adds 9 to n
cout << "After n += 9, n = " << n << endl;
n -= 5; // subtracts 5 from n
cout << "After n -= 5, n = " << n << endl;
n *= 2; // multiplies n by 2
cout << "After n *= 2, n = " << n << endl;
n /= 3; // divides n by 3
cout << "After n /= 3, n = " << n << endl;
n %= 7; // reduces n to the remainder from dividing by 7
cout << "After n %= 7, n = " << n << endl;
 
return 0; //is optional for the main().
}

Etiquetas: ,

Deja un comentario