Ok, So I got into math functions in C++ and it showed this which is totally confusing me. I'm just not comprehending what it does.
I'm just not getting what that is meaning. Any help?Code:++count;
//OR
total = 6 + count--;
Printable View
Ok, So I got into math functions in C++ and it showed this which is totally confusing me. I'm just not comprehending what it does.
I'm just not getting what that is meaning. Any help?Code:++count;
//OR
total = 6 + count--;
Increases count by 1.Code:++count;
Decreases count by 1.Code:count--;
There are post and pre incremenet/decrement operators....
which increment or decrement the variables before or after their usage.....
Here is an example:
Code:#include <stdio.h>
int main()
{
int i=1;
printf("i=%d\n",i); /*i=1*/
i++;
printf("i=%d\n",i); /*i=2*/
printf("i=%d\n",i++); /*i=2*/
printf("i=%d\n",i); /*i=3*/
printf("i=%d\n",++i); /*i=4*/
return 0;
}
I'm still not understanding and CodingMaster I'm using C++ not C.
It really doesn't get much simpler.
int c = 5;
sets "c" to 5.
c++
makes "c" equal 6.
c--
makes "c" equal 5 again.
Here's codingmasters code in C++...
Code:// include stream header files here...
int main()
{
int i=1;
cout << i; /*i=1*/
i++;
cout << i; /*i=2*/
cout << i++; /*i=2*/
cout << i; /*i=3*/
cout << ++i; /*i=4*/
return 0;
}
:PCode:#include <iostream>
using namespace std;
int main()
{
int i=1;
cout << i; /*i=1*/
i++;
cout << i; /*i=2*/
cout << i++; /*i=2*/
cout << i; /*i=3*/
cout << ++i; /*i=4*/
return 0;
}
To expound on Elixa's message... if the ++ is before the variable, it is incremented by one BEFORE it is used. If it is after, it gets incremented AFTER it is used. Same with --.
ok so 2 negatives minuses it by 1. opposite with positive. and its before or after depending on position.
mhmm.
heh, _Elixia_ did the work for me..... ;)
int x=5; // set x the value of x to 5
u use ++ for incrementation and -- for decrementation
++ is equal to +1
-- is equl to -1
so, u could write instead of ++ or -- for post incrementation / decrementation
x-=1; // equal to x=x-1
x+=1;// equal to x=x+1
when u use pre incrementation / decrementation.....
the compiler increments / decrements the variable first and then output its......
e.g.:
x--; // is equal to --x;
but.... have a look at this code:
Decremenation works like incrementation.....Code:#include <iostream>
using namespace std;
int main()
{
int x=5;
cout << x++ << endl; // cout: the value of x is 5 and after it increments it.....
cout << x << endl; // cout: the value of x is 6
cout << ++x <<endl; // cout: !!!now!!! we use pre incrementation: cout: the value of x is 7
return 0;
}
Dec: -- (=-1)
Inc: ++ (=+1)
Don't forget the difference between postfix and prefix:
Code:int x=1;
int c=++x; //c will be 2 since it increments and then returns the value
x=1;
int y=x++; //y will be 1 since it returns the value then increments
Ok cool thanks that helped me a bunch.