/* */

Tuesday 21 August 2012

operators


  X.Y the member Y of object X
X[Y] the element in object X indexed by Y
x++ increments x returning the original value of x
x-- decrements x returning the original value of x
----------------------------------------------------------------------------------------
++x increments x, returning the incremented value
--x decrements x, returning the incremented value
!x logical negation. If x is true then !x is false
----------------------------------------------------------------------------------------
x * y product of x and y
x / y quotient of x and y. if both operands are integers, the implementation
chooses whether to round toward zero or - 8
x % y remainder of x divided by y, equivalent to x - ((x/y) *y)
----------------------------------------------------------------------------------------
x + y sum of x and y
x - y result of subtracting y from x
----------------------------------------------------------------------------------------
x >> y For integral x and y, x shifted right by y bits; y must be non-negative.
If x is an istream, reads from x into y
x << y For integral x and y, x shifted left by y bits; y must be non-negative.
If x is an ostream, writes y onto x.
----------------------------------------------------------------------------------------
x relop y Relational operators yield a bool indicating the truth of the relation.
The operators (<, >, <=, and >=) have their obvious meanings.
----------------------------------------------------------------------------------------
x == y Yields a bool indicating whether x equals y
x != y Yields a bool indicating whether x is not equal to y
----------------------------------------------------------------------------------------
x && y Yields a bool indicating whether both x and y are true.
Evaluates y only if x is true.
----------------------------------------------------------------------------------------
x || y Yields a bool indicating whether either x or y is true.
Evaluates y only if x is false.
----------------------------------------------------------------------------------------
x = y Assign the value of y to x, yielding x as its result.
x op= y Compound assignment operators; equivalent to x = x op y,
where op is an arithmetic or shift operator.
----------------------------------------------------------------------------------------
x ? y1 : y2 Yields y1 if x is true; y2 otherwise.
Evaluates only one of y1 and y2.


Incremental (good for loops)

The '++' is equvalent to saying i = i +1 and can be expressed in two ways; 
1. ++i
2. i++
When the ++ comes before the variable (first example) the operation is first performed and 
the resulting value produced.
After the variable (second example), the current value is produced first and then the operation 
is performed. For example:

#include
using namespace std;

int main(void)
{
int a = 1;
int b = 1;
cout << a++ << endl; //shows post increment
cout << ++b << endl; //shows pre increment
cout << a-- << endl; //shows post decrement
cout << --b << endl; //shows pre decrement
return 0;
}


The decremental operators (--) work in the same way but opposite - they subtract 1 rather 
instead of add. 

No comments:

Post a Comment