[Logo] JavaRanch » Big Moose Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Programmer Certification (SCJP)
 
RSS feed
 
New topic
Author

post increament

vuthlarhi donald
Ranch Hand

Joined: Jul 31, 2006
Messages: 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
Messages: 651

Hi Vuthlarhi
You know the answer already

SCJP 5.0 97% JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - API - Generics FAQ - JLS - JVM Spec - Java FAQs
Madhu Desai
Ranch Hand

Joined: Jun 14, 2009
Messages: 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
Messages: 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
Messages: 5741

so... do you have an actual question?
vuthlarhi donald
Ranch Hand

Joined: Jul 31, 2006
Messages: 76

the question is I don't understand the output

0,2,4
Eduardo Bueno
Ranch Hand

Joined: Jun 04, 2009
Messages: 150

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.

This message was edited 1 time. Last update was at by Eduardo Bueno

Sridhar Gudipalli
Ranch Hand

Joined: Nov 02, 2005
Messages: 65

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

Thank you,
Sridhar Gudipalli
Preparing SCJP (310-065) | SCJP FAQ | SCJP Mock Tests | Java Spec
Madhu Desai
Ranch Hand

Joined: Jun 14, 2009
Messages: 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




Thanks
Preparing for SCJP 6
Madhu Desai
Ranch Hand

Joined: Jun 14, 2009
Messages: 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)

Thanks
Preparing for SCJP 6
vuthlarhi donald
Ranch Hand

Joined: Jul 31, 2006
Messages: 76

thank you guys..now I understand it...
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Programmer Certification (SCJP)
 
RSS feed
 
New topic
hibernate profiler