What is the output?

R

ryanvade

Guest
Without actually running the program, what is the output from the following? (Java)
Code:
int a = 4;
a += a * 2 + (a++);
System.out.println(a);

Explain your reasoning!!! ;)
 


16.

Knowing that a = 4;

and
a += a * 2 + (a++);

then:
a = 4 + 4 * 2 + 4;
thus a= 16;

a++ means the increment must happen to a given variable only after the current assignment operation (doesn't matter whether it's within brackets).

Ok, then why not 17?

Since the variable is going through assignment already, the trick is that it won't be incremented after the whole operation... And only "before" it:

For instance, assuming a=4;
and
a += a * 2 + (++a);

Then:
a += 4 + 4*2 + 5;
in this case a=17;

When a variable increment happens during the assigment of the same variable, let's name it self-increment - it will only work with ++ placed before the variable.



Now, let's play a bit, assuming a=4 and b=4;
a += a * 2 + (b++);

Then:
a = 4 + 4 * 2 + 4;
a=16 likewise;

and after the assignment above happened:
b=4+1;
b=5;

On the other hand, assuming a=4 and b=4 again:
a += a * 2 + (++b);
thus:
a=17;
b=5;
 
17. Because that variable is incremented by a++ on its own assignment in C++, I guess... This one I had to run though :p


Now I've got a question about the following C++ code:
Code:
int a=4;
a+=a*2+(++a);
cout << a;
Why is the output 18?

The only equivalences I could come up with for that assignment are, so far:
a = 4 + 5*2 + 4;
or
a = 5 + 4*2 + 5;

But it doesn't feel very logical.
 
Last edited:
In C and C++ you need to be careful when using pre and post increment operators in this way.

The 'strange behaviour' is because there are several manipulations being applied to the same variable on a single line, including a pre/post-increment operation, which isn't evaluated in the way you'd think.

Typically pre/post increment operations should be the only manipulation performed on a single variable in any given line..... If that makes sense?! Otherwise you need to be really careful!

For anybody who doesn't know about the pre and post increment operators in C/C++ (and Java):
The pre-increment operator ++n sets the value of n to n+1 and returns the resultant value of n+1.
Whereas the post-increment operator n++ sets the value of n to n+1 but returns the original value of n. Java implements the same behaviour for these operators.

So in your pre-increment example:
int a=4;
a += a * 2 +(++a);

Logically you would expect to see the same evaluation as the java example:
a += 4*2+(++4)
Therefore you would expect the new value of a to be evaluated like this:
a = 4 + 8 + 5
a = 17

But in C++, this is not the case. So how are we getting to 18?
Here's the reason:

According to the rules of precedence in C++; the multiplication operation has greater precedence, so it is the first operation that is performed. Then the pre-increment occurs (which modifies the value of the variable), and the addition is performed last.

So in your pre-increment example, given that the initial value of a is 4, you get this:
a += a * 2 + (++a)

First operation is a*2. Value of a is 4, therefore a*2=8.
Substitute that into the example:
a += 8 + (++a)

then the pre-increment is performed giving:
a += 8 + 5
NOTE: the value of a is now 5.

Finally the addition is performed, yielding this:
5 += 8 + 5
Therefore a=18.

The same applies to the post-increment example.
Again, assuming the initial value of a is 4:
a += a*2+(a++);

The multiplication is performed first yielding:
a+=8+(a++)

Then the increment:
a += 8 + 4
Note: In the above, the post increment sets the value of a to 5 and returns the original value of a (which was 4).

Yielding this for the final addition:
5 += 8 + 4

Therefore a = 17.

Clear? :)
 
Last edited:
Logically you would expect to see the same evaluation as the java example:
a += 4*2+(++4)
Therefore you would expect the new value of a to be evaluated like this:
a = 4 + 8 + 5
a = 17
You mean a=16 right? Because in Java it is 16.
 
No, because we are using pre-increment, so it would be 17 in Java:
a+=4*2+(++4)
Therefore:
a = 4+8+5 = 17

If we were using post-increment, then the result would be 16 in Java:
a+=4*2+(4++)
Therefore:
a = 4+8+4 = 16

But as explained in my other post; in C++ the order of evaluation is different!
 
Last edited:
No, because we are using pre-increment, so it would be 17 in Java.
If we were using post-increment, then the result would be 16 in Java!
I didn't notice it was pre and not post-increment. :oops:
 
Any information that has been processed by and sent out from a computer or similar device is considered output. A simple example of output is anything you view on your computer monitor. The bottom half of the image shows data being sent from the computer to a printer. That data is then printed onto a piece of paper; both are forms of output.
 
Any information that has been processed by and sent out from a computer or similar device is considered output. A simple example of output is anything you view on your computer monitor. The bottom half of the image shows data being sent from the computer to a printer. That data is then printed onto a piece of paper; both are forms of output.
Alright, captain... But we're not machines ourselves. Let's consider a very functional linguistic attribute (humanly speaking) generally regarded as context ;)
 


Top