This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Why is this printing 1,0 instead of 1,1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why is this printing 1,0 instead of 1,1" Watch "Why is this printing 1,0 instead of 1,1" New topic
Author

Why is this printing 1,0 instead of 1,1

Punya Pratap Singh
Ranch Hand

Joined: Nov 23, 2010
Posts: 80
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Can you tell us the reason why it should print 1,1? So that we can help you out?


Mohamed Sanaulla | My Blog
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Please use code tag, when you post your code.
you are not a newbie!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

The problem is in the line with all the i's. So let's dissect that:
The reason is that i++ returns the old value of i, which is 0. i is incremented, yes, but that incrementing is then discarded because of the assignment.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
N Sahni
Ranch Hand

Joined: Jul 07, 2011
Posts: 55

At line no: 10 instead of using i++, prefix increment operator will give the required results.



Since, there is assignment operation taking place, following rule applies:
  • When a prefix expression is used, the value returned is the value calculated after the prefix operator is applied.
  • When a postfix expression is used, the value returned is the value calculated before the postfix operator is applied.


  • Thanks and Regards,
    Nilesh Sahni | nsahni@infocepts.com | www.infocepts.com
    Wouter Oet
    Saloon Keeper

    Joined: Oct 25, 2008
    Posts: 2700

    Therefore it is wise not the use the post/prefix incrementors in combination with other statements.


    "Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
    Please correct my English.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Why is this printing 1,0 instead of 1,1
     
    Similar Threads
    why output is 1,1 ?
    What is the follow of this code?
    Prime Factor
    order required
    Why the result is 1,0