Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A few questions

 
Tom Liu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me the difference between i++ and ++i (assuming i has a value of 10)

Also, is it true that when a block is done, any local variables are "deleted"?

Finally, in a statement such as

while(array[i++]%2==0);

is i++ part executed without regard for the conditional statement, or will i++ be executed unless the equation returns true? Which comes first?

Thanks!
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Tom,

My answer will sound cruel BUT try it!

Just write a quick bit of code....

int x = 10;
int y = 20;
x++;
++y;
System.out.println("x++ is: " +x + "++y is: " +y);

Try the same for the while statement.

(My lecturer David Marks would be so proud to hear me say his words!! BUT it works - TRY IT).

Remember though that the condition is checked before ++i
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question the first:

There is no difference between ++i and i++. It used to be that ++i was a little bit faster than i++, but I think that modern compilers optimize this out. However, there is a difference between a = i++ and a = ++i; In the former, a is assigned the value of i and then i is incremented (!-see comment). In the latter, i is incremented and then a is assigned the value of i.

!- note that the assignment happens after the incrementation; that is, the value of i is obtained (call it tempI), i is incremented, and then a is assigned the value of tempI. This means that the statment i = i++ actually accomplishes nothing!

Question the Second:

It depends, and Java says that you don't really need to know. What you do need to know is that these variables have gone out of scope and the compiler won't let you reference them outside the block. However [and this is advanced Java], I think that the compiled code retains these variables until the method concludes. Of course, all this is implementation specific; if you use a compiler other than javac, you may get different byte-code output.

Question the Third:

the conditional is evaluated before the ++, but the ++ still occurs, whether or not the conditional is true. (that is, the order of operations is: conditional expression is evaluated, ++ occurs, while statement operrates on the value of the conditional)

See for yourself!

Change the value of i from odd to even and see the resulting change in the behaviour.
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On your question about local variables being declared within a block being deleted you are sort of right.... They are not deleted as such, they just are not reachable from outside the block i.e. if you had a while or for loop and declared a varable locally within this block then they are not reachable outside these loops. That's ok if you want to use them within the block but if you wish to use them outside i.e. in more than one method then declare them at the top of the class.
 
Tom Liu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies! I got it all sorted out now
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic