Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes How do we get this output ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do we get this output ?" Watch "How do we get this output ?" New topic

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: 492

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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15081

See the following page in our FAQ: Post Increment Operator And Assignment

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
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: 10817

Just for fun, change

[How to ask questions] [Donate a pint, save a life!] [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.
I agree. Here's the link:
subject: How do we get this output ?
It's not a secret anymore!