Solved

# expression

Posted on 2006-04-25
229 Views
here is the following statement

i = 5;
j = i++ - ++i;

What are the possible values of j in Java?

My guess would be 0 because if I increase i then subtract i then i should get 0, am I right? It should be the same as in C++
0
Question by:shahrine99

LVL 17

Expert Comment

Hi shahrine99,

i = 5;
j = 5 - 6;

In the end j is -1, i is 7

Joe P
0

LVL 17

Expert Comment

Oh actually I agree. 0
j = 6-6
i = 7

j should be 0 ur right =)

Joe P
0

LVL 17

Expert Comment

Your right because ++i increments i before the expression is evaluated, and since they are both i they both become 6 then you get 6-6.  The i++ then takes affect after the expression is evaluated.

Joe P
0

LVL 41

Expert Comment

Well, since i++ should increment after the reference, and ++i should increment before the usage:

i = 5;
j = 5 - 5;

j = 0;

whereas i == 5

I agree...
0

Author Comment

ok cool thats in Java...what about in c++?
0

LVL 30

Assisted Solution

I think in Java, it should be -2. Because in i++, the value of 'i' which will be used will be 5. Then 'i' will be incremented to 6. Then in ++i, 'i' will be incremented again to 7 and then used. So you get 5 - 7 = -2.
0

LVL 30

Expert Comment

It will vary across platforms. In some cases it will be -2, in some cases it might be 0 because C++ might use stacks for this and the ++i might get evaluated before i++ in certain cases. We are not supposed to answer that ;-) ask in the C++ topic area.
0

LVL 24

Expert Comment

This is homework, isn't it?

And, why didn't you simply try this yourself?

;JOOP!
0

LVL 17

Expert Comment

mayankeagle:
I was taught that ++i inrecrements i Before the statement is evalutated.
I was taught that i++ inrecrements i After the statement is evaluated.
When you said -2 that implies that it happens in real time moving through the statement and I am not too sure that is right.
I of course have not coded this, I'm resisting all urges =)
0

LVL 13

Accepted Solution

mayankeagle is right :

i = 5;
j = i++ - ++i;

in Java is eqv to (java byte code use stack)
// i++
push value of i      (stack: 5 )
increment i  (i=6)
// ++i
increment i  (i=7)
push value of i      (stack: 5 7 )
// -
substract           (stack: -2)
// j=
store into j

It should be the same in most C++ compiler.
You can get different result if you use older C++ compiler or non-standard ones.
0

LVL 13

Expert Comment

0

LVL 24

Expert Comment

B-Homework.
0

LVL 30

Expert Comment

>> I was taught that i++ inrecrements i After the statement is evaluated.

Not the entire statement, just the expression or the sub-expression where it is used. Expect it to evaluate after the first usage.
0

LVL 30

Expert Comment

>> Expect it to evaluate

Meaning expect it to 'change the value'
0

## Featured Post

### Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…