• 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

For Loop Iteration Expression

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Consider these two for loops:



When I run this, the output is the same from both:

0 in ++loop
1 in ++loop
0 in loop++
1 in loop++

The iteration part of the for loop is evaluated after the body has been executed. So my question is this:

Does having ++i or i++, in the iteration of a for loop, make a difference to the execution of the body ?


Thanks in advance
Finner
[ July 04, 2006: Message edited by: Finner Jones ]
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your case ("for(int i=0; i<2; ++i)" and "for(int i=0; i<2; i++)"), they are the same.
[ July 04, 2006: Message edited by: wise owen ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. But you will notice a difference if you use the expressions as part of the condition of the loop.
 
Author
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keith is correct remember when used as part of an expression- the side effects take place...
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even if I use S.O.P in the expression then also no difference

class testing{
public static void main(String args[]){

for(int i=0;i<3;i++,System.out.println(i))
System.out.println("i value in Loop" + i);

for(int j=0;j<3;++j,System.out.println(j))
System.out.println("j value in loop" + j);
}

}

Both display same values any idea ???
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Susan, if you place the expressions in the condition, you will notice a difference. Consider the following.


[ July 04, 2006: Message edited by: Keith Lynn ]
 
James Chegwidden
Author
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Keith's example the side effects take place..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic