File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes post increament Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "post increament" Watch "post increament" New topic
Author

post increament

vuthlarhi donald
Ranch Hand

Joined: Jul 31, 2006
Posts: 76
public class Q {

public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.println(i++); // I am loosing it here..if i=0 ; j = i++; that I know that j will be zero, but 4 that I am loosing it
}
}
}
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 754

Hi Vuthlarhi
You know the answer already


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Madhu Desai
Ranch Hand

Joined: Jun 14, 2009
Posts: 42
vuthlarhi donald wrote:// I am loosing it here..if i=0 ; j = i++; that I know that j will be zero, but 4 that I am loosing it


where did j come from?


Thanks
Preparing for SCJP 6
vuthlarhi donald
Ranch Hand

Joined: Jul 31, 2006
Posts: 76
Just wanted to ilustarte that I know how the post increament works, but I don't get it when we return back to the loop..so you can ignore the comments..
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 8428

so... do you have an actual question?


Never ascribe to malice that which can be adequately explained by stupidity.
vuthlarhi donald
Ranch Hand

Joined: Jul 31, 2006
Posts: 76
the question is I don't understand the output

0,2,4
Eduardo Bueno
Ranch Hand

Joined: Jun 04, 2009
Posts: 151
You're always in the same scope. When you use i++ in the println(), it prints 0 and after that i becomes 1, returning to the beggining of the loop with this value. The for statement will then increment i, which becomes 2, prints 2, and so on.
Sridhar Gudipalli
Ranch Hand

Joined: Nov 02, 2005
Posts: 120
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.println(i++);
}
}
}

iteration 1:
--------------
i value is 0; (0<5 test is pass)
prints 0 then increments i value (i is now 1) //output is: 0
i is incremented in for loop (i is now 2)

iteration 2:
------------
i value is 2; (2<5 test is pass)
prints 2 then increments i value (i is now 3)//output is: 0 2
i is incremented in for loop (i is now 4)

iteration 3:
------------
i value is 4; (4<5 test is pass)
prints 4 then increments i value (i is now 5) //output is: 0 2 4
i is incremented in for loop (i is now 6)

iteration 4:
------------
i value is 6; (6<5 test is fail)

So the final output is: 0 2 4


Sridhar Gudipalli|SCJP 6.0
SCWCD objectives
Madhu Desai
Ranch Hand

Joined: Jun 14, 2009
Posts: 42
vuthlarhi donald wrote:the question is I don't understand the output

0,2,4


Its seems you have confused how the for-loop works.. I will explain... see if you too think the same.


int i = 0;

i < 5 (0 < 5: true)
Print 0 //and then increment it to 1 (i++ in Print statement)
i++ (1++ = 2, the one in increment part of for-loop)

i < 5 (2 < 5: true)
print 2 //and then increment it to 3 (i++ in Print statement)
i++ (3++ = 4, the one in increment part of for-loop)

i < 5 (4 < 5: true)
print 4 //and then increment it to 5 (i++ in Print statement)
i++ (5++ = 6, the one in increment part of for-loop)

i < 5 (6 < 5: false)
exit from the for-loop

So the answer is 0 2 4

Hope its clear



Madhu Desai
Ranch Hand

Joined: Jun 14, 2009
Posts: 42
Wow!!! Sridhar Gudipalli you are too fast... before i write and submit, your post was already posted..

vuthlarhi donald, you can ignore my post(its same as one with Sridhar Gudipalli)
vuthlarhi donald
Ranch Hand

Joined: Jul 31, 2006
Posts: 76
thank you guys..now I understand it...
 
IntelliJ Java IDE
 
subject: post increament
 
Threads others viewed
why does 'break' traverse one more time
spiral traversal of mxn matrix
post(not)incremented?
Array Indexing
What must be done in for loop.
developer file tools

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture