• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

expression

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
shahrine99
Asked:
shahrine99
  • 4
  • 4
  • 2
  • +3
2 Solutions
 
BogoJokerCommented:
Hi shahrine99,

i = 5;
j = 5 - 6;

In the end j is -1, i is 7

Joe P
0
 
BogoJokerCommented:
Oh actually I agree. 0
j = 6-6
i = 7

j should be 0 ur right =)

Joe P
0
 
BogoJokerCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
HonorGodCommented:
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
 
shahrine99Author Commented:
ok cool thats in Java...what about in c++?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
>> what about in c++?

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
 
sciuriwareCommented:
This is homework, isn't it?

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

;JOOP!
0
 
BogoJokerCommented:
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
 
WebstormCommented:
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
 
WebstormCommented:
B-grade ?
0
 
sciuriwareCommented:
B-Homework.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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
 
Mayank SAssociate Director - Product EngineeringCommented:
>> Expect it to evaluate

Meaning expect it to 'change the value'
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 4
  • 4
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now