# Thread: Side effect of various operations

1. ## Side effect of various operations

I know you guys dont like answering h.w. questions but im trying to study for my test and cant find the answers to this in my notes or any book. My teacher wants us to know what some common expressions that are used in C++ more for the side-effect than the returned value. I dont understand what he means, like how i++ returns a temporary value or something? Also whats the side effect of evaluating X = 4, and whats the value of the expression? Thanks for the help

2. i++

++ returns a 1 and adds it to the original int.

x=4

the value of x is 4 and any value that was initially in x is overwritten.

3. Well I know that whatever is in x is overwritten, but I dont think that is what he is asking. I remember reading somewhere a long time ago that = returns a lvalue or something but i cant remember where i read that too many damn books!

4. He could be refering to this:

i++ is a post expression operator (not sure if that's the right term) anyways the increment is done after the use of the variable.

so
Code:
```int num = 5;

cout << num++ << endl;```
outputs 5 and leaves num with a value of 6.
A better example.
Code:
```int num = 5;
int sum = 0;

sum = num + num++;```
sum is 10 when the expression is evaluated and num is 6 if we use it later.

++i on the other hand is done before the expression is evaluated, so it's the opposite.

Now the x=4 thing.
When comparing variables in boolean expressions it is a common and hard to find error if you use = instead of ==. The problem lies in the fact that
if( x = 4 )
is allowable syntax but what it does is assign 4 to x and then evaluate x. In C/C++ 0 (or negative number) is false and a positive number is true, which could result in a problem like this:
Code:
```
if( x = 4 ) {
cout << "This statement will always run\n";
}
else {
cout << "This statement is never run\n";
}```

Popular pages Recent additions