• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Punya Pratap Singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell us the reason why it should print 1,1? So that we can help you out?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tag, when you post your code.
you are not a newbie!
 
Rob Spoor
Sheriff
Pie
Posts: 20671
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
N Sahni
Ranch Hand
Posts: 55
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  •  
    Wouter Oet
    Saloon Keeper
    Posts: 2700
    IntelliJ IDE Opera
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Therefore it is wise not the use the post/prefix incrementors in combination with other statements.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic