Re: i++ or ++i ?
John Brawley schrieb:
> Please pardon a maybe very stupid question?
> I've read and reread Bjarne Stroustrup on this, and I still don't "get it,"
> so I wrote this to me understand, and now I _really_ don't "get it."
> Why, if one increments before, and the other after, do these snippets output
> exactly the same?
>
>
> #include <iostream>
>
> int main() {
> for(int i=0; i<10; i++)
> std::cout<<i<<", ";
> std::cout<<"\n";
> for(int i=0;i<10;++i)
> std::cout<<i<<", ";
> std::cout<<"\n";
> int b=7;
> b++; std::cout<<b;
> int c=7;
> ++c; std::cout<<"\n"<<c;
> //????????? why? same outputs!
> return 0;
> }
>
> _Same_output_.
> What's the difference?
> (I use these in a program, which doesn't crash....)
>
>
Maybe it will you do define the difference between ++i and i++;
The statements do the same with one difference:
The value of the statement "++i" is i+1
while the value of i++ is i:
#include <iostream>
using namespace std;
int main()
{
int i=6;
cout << "i++: " << i++ << endl; //Will return i = 6
int j=6;
cout << "++i: " << ++i << endl; //Will return i+1 = 7
return 0;
}
I hope this will you!
Kind regards, Hans
|