• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

using i=i++

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While going through some mock exams I came across the following question in Naveen's Javaprepare.com test


according to the test the answer is 2, but when I run this in MyEclipse the answer comes back as 0.

if I change the i=i++ to i++ the answer comes out as 2.

I guess my question is does i= in front of the i++ affect the outcome or not?

My thought is that the first time through the loop on the line i=i++ i would be = 0 but the next line i would be incremented to 1 and then similar for the second time through i should be equal to 2
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it matters. Think carefully about when "i" is incremented and/or
assigned in the loop. Putting ++ before or after "i" is very different.

Jim ... ...

(P.S. And welcome to JavaRanch. You will learn a lot here.)
 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


For the above code you would get 2 0.





FOR THE ABOVE CODE YOU WOULD GET 0 0

Explanation :-

++j or j++ it essentially means

j(new)=j(old)+1;

Now which j is assigned to left (if any of assignment operator) is the issue.

Now, suppose we have
x= ++j then j(new) would be used.

if we have x=j++ then j(old) would be used.

Important is , j would be incremented first irrespective of x=++i or x=i++;

Ex1 := x=i++;

This translates to
i(new) = i(old) +1;
x=i(old)

Ex2:- x=++i

This essentially translates to
i(new) = i(old)+1
x=i(new)

Now just replace x with i , use the above sequence , you would get the above mentioned answers.

Thanks !!!
 
Ken Kallaur
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys for the quick replies

I think I'm getting it now.

I had it in my head that

i=i++ would still increment

I understand that
x=i++ equals the old value of i

but my thinking was
i=old value of i now increment i


The thing that throws me off is that where
x=i++
i does increment
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://faq.javaranch.com/java/PostIncrementOperatorAndAssignment
 
I do some of my very best work in water. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic