• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

post increament

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
}
}
}
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vuthlarhi
You know the answer already
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
vuthlarhi donald
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so... do you have an actual question?
 
vuthlarhi donald
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the question is I don't understand the output

0,2,4
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Madhu Desai
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you guys..now I understand it...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic