Cpp operators

From wikinotes

Operators in C++ are exactly what you would expect, except for a handful of exceptions.

basics

int x = 1 + 1;
int x = 1 - 1;
int x = 1 / 1;
int x = 1 * 1;
int x = 1 % 1;


if ( 1 == 1 ) && ( 2 == 2 ) { cout << "true"; }
if ( 1 == 1 ) || ( 2 == 2 ) { cout << "true"; }

increment/deincrement

int x = 1;
int y = 1;

x++;			// 1 is added to x
++x;			// 1 is added to x

y = x++;		// y is equal to x _THEN_ x is inremented       (y=1, x=2)
y = ++x;		// x is incremented then y is made equal to it  (y=2, x=2)

untrue expression

if !( 1 == 2 ) {
	cout << "untrue" << endl;
}

conditional ternary

// if 1==1, then variable 'success' receives the value "true"
// otherwise it receives the value "false"
string success = (1 == 1) ?   "true" : "false";

comma operator

The comma operator allows you to perform multiple expressions where only one is expected.

// increment x,
// then multiply it by 4
int x = 1;
a = ( x++, x * 4 )