• 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

 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please take a look at this code
boolean flag = true;
for(int i = 0, System.out.println("run"); flag; i++) {
//loopbody
}
This wont compile....
But
boolean flag = true;
int i ;
for(i = 0, System.out.println("run"); flag; i++) {
//loopbody
}
This would compile..
Why?
 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the ans lies in the way variables are declared ,
when we have to declare a more than one int varible we do it this way
int i , j ;
until the compiler sees a ';' , it expect only a identifier-name after a comma

for(int i = 0, System.out.println("run"); flag; i++) {
//loopbody
}

in this code , wht is happening is sort of ;
int i = 0, System.out.println("run");
this isnt okay wid compiler , becoz it expects , second variable name after comma(,) .And compiler can expect any other statement only after a semi-colon( ; ) , but a semi-colon in this case will effectively mark end of intialization part of for !!
for more things on statements within for-loop-expressions , check my another post in this forum at ; http://www.javaranch.com/ubb/Forum24/HTML/011538.html
------------------
Gagan (/^_^\)
[This message has been edited by Gagan Indus (edited August 16, 2001).]
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ragu
The rule is that you can't mix declarations and expression statements in the initialization section of the for loop.
The initializer can contain either declarations or a list of statements, but not both.
Siobh�n
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic