Velocity Reviews > C++ > evaluate it

# evaluate it

ashim.seth@gmail.com
Guest
Posts: n/a

 07-05-2005
hey people i was just testin this:
int i=5,j;
j=i++ + ++i;
cout<<j;

and then...

int i=5;
int j= i++ + ++i;
cout<<j;

both givin different results
y?
plz help

Victor Bazarov
Guest
Posts: n/a

 07-05-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> hey people i was just testin this:
> int i=5,j;
> j=i++ + ++i;
> cout<<j;
>
>
> and then...
>
> int i=5;
> int j= i++ + ++i;
> cout<<j;
>
>
> both givin different results
> y?
> plz help

V

Peter Julian
Guest
Posts: n/a

 07-05-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> hey people i was just testin this:
> int i=5,j;
> j=i++ + ++i;
> cout<<j;
>
>
> and then...
>
> int i=5;
> int j= i++ + ++i;
> cout<<j;
>
>
> both givin different results
> y?
> plz help
>

The results are irrelevent. Since you are refering to the same variable in
that compound statement, the standard does not guarentee when, how or
whether the variable i gets incremented. In fact, the standard has a
requirement that the said variable be only modified once in between sequence
points.

Try:

#include <iostream>

int main()
{
int i = 5;

std::cout << "i = " << i << "\n";

int j(i++);

std::cout << "i = " << i << "\n";
std::cout << "j = " << j << "\n";

j += (++i);

std::cout << "i = " << i << "\n";
std::cout << "j = " << j << "\n";

return 0;
}

/*
i = 5
i = 6
j = 5
i = 7
j = 12
*/