• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do we get this output ?  RSS feed

 
Mohnish Khiani
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Output : 0
How do we get output as 0?
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
j++ means that the value of j will increase by 1 after the statement is processed.

So when you do:

You are really doing:


So j gets assigned 0, THEN the value on the right hand side turns into 1, but at that point it doesnt matter because the loop executes again and the right hand side is lost.

try this:
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the following page in our FAQ: Post Increment Operator And Assignment
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please search this forum for "postincrement" and "preincrement" because lots of people get confused about this very same question and there are frequent posts about it.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for fun, change
j=++j;
to
j+=1;
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or just to j++;
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!