POST & PRE OPERATOR
An operator that require only one operand or data item are called unary operators. C++ supports unary minus(-), ++ (Increment), -- (Decrement) on airthmatic operands.
In C++ the other unary operator are ++ (Increment), -- (Decrement). These operator can be used before or after the variable, to give condition type increment and decerement.
INCREMENT OPERATOR
++ Increases the value by one. If it is used before or after the variable.....
Code:
i++ means i=i+1;
++i means i+1=i;
Both the formula are very diff and can the whole output, i=i+1 means that incrementation is done after the value is called. But i+1=i; means incrementation is done before the value is called.
For e.g. :-
Code:
int i;
i=10;
i=i++;
cout<< i;
Here first the value is incremented then the value is printed and the output will come 11. But if the same program is written in a way like this:-
Code:
int i;
i=10;
cout<< i++;
Then the output will be 10 no incrementation will be done beacuse as I already told i++ means i=i+1
so the compiler will first print the value of i then it will increament.
Also the same program with diff result:--
Code:
int i;
i=10;
cout<< ++i;
Here the output will be 11 incrementation will be done beacuse as I already told ++i means i+1=i
so the compiler will first increament the value then it will print the value of i.
Note:- The following topic is important if U have any problem then please ask...
For e.g: -
Code:
int i,j;
i=10;
j=i++;
cout<< i<<" "<< j;
Here the output will be 10 11 . Here the value of i is assigned to j then in the value of i there is post incrementation of 1 so the final value of i remains same and j is 11.
MULTIPlE INCREMENTS
SO after simple increment and decrement move into the second chamber of complicated increments this type of increment are rarely used in programs but they can be asked in the outputs so as a guider I have to tell everything .
These type of increments and decrements are used in same line and with same integar or diff integar..
For e.g. :-----
Code:
void main()
{
clrscr();
int x;
x=10;
cout<< x++<<" "<< ++x;
getch();
}
In the above program there are 2 output in the same line and the integer is same so the output will go from left to right but the assigning of numbers will be from right to left..
As the initial value of x is 10 and for assigning heading from right the first thing is ++x means x+1=x so the value become 11. Then there is x++ means x=x+1; so the comp will 1'st print the value then it will increment it so the value remains same as 11.The final output is 11 11.
I am not going into deep of this because this is not too much important as far as I know. SO lets start the loop.
No comments:
Post a Comment