• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do we get this output ?

 
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
Saloon Keeper
Posts: 15459
42
Android IntelliJ IDE Java Scala Spring
 
Campbell Ritchie
Sheriff
Pie
Posts: 49865
71
  • 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
Saloon Keeper
Posts: 11096
13
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
Saloon Keeper
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