| Author |
How do we get this output ?
|
Mohnish Khiani
Ranch Hand
Joined: May 17, 2010
Posts: 65
|
|
Output : 0
How do we get output as 0?
|
 |
Hunter McMillen
Ranch Hand
Joined: Mar 13, 2009
Posts: 490
|
|
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:
|
"If the facts don't fit the theory, get new facts" --Albert Einstein
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12953
|
|
|
See the following page in our FAQ: Post Increment Operator And Assignment
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
|
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
Joined: Jul 26, 2007
Posts: 8566
|
|
Just for fun, change
j=++j;
to
j+=1;
|
[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
|
or just to j++;
|
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
|
 |
 |
|
|
subject: How do we get this output ?
|
|
|