• 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

Boolean in for loop??

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I would have thought that the following program would not compile..but it does..any thoughts on how the for loop works?


Output:
XYAZYAZY
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first and third parts of the "for" loop control line can be more or less any expressions, including none. The second one, though, has to be a boolean. There's no restriction that there has to be an integer loop counter.
I personally write this code all the time:
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's what the Java Language Specification says about the for loop:
"
The for statement executes some initialization code, then executes an Expression, a Statement, and some update code repeatedly until the value of the Expression is false.
ForStatement:
for ( ForInit ; Expression ; ForUpdate )
Statement
ForStatementNoShortIf:
for ( ForInitopt ; Expressionopt ; ForUpdateopt )
StatementNoShortIf
ForInit:
StatementExpressionList
LocalVariableDeclaration
ForUpdate:
StatementExpressionList
StatementExpressionList:
StatementExpression
StatementExpressionList , StatementExpression
The Expression must have type boolean, or a compile-time error occurs.
"
So the only real constraint is that the "Expression" part must return a boolean while the "ForInit" and "ForUpdate" can be valid Java statement(s) separated by commas... IN fact even the following works though you would not want to use it :-)
for (; ;) {
System.out.println("Never ending....");
}
[ October 20, 2003: Message edited by: Vinod Venkatasubramanian ]
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cathy, why do you think the program would not compile?
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because I thought the first part of the for loop (if present) was initialization and third part (if present) was some expression.
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cathy Song:
Because I thought the first part of the for loop (if present) was initialization and third part (if present) was some expression.


the only thing to remember about the first part is that it is going to be executed *only once*. the third part is going to be executed at the end of every iteration of the loop.
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Cathy ...
And one more point:
In the first part of the 'for' loop, comma-separated declaration statement(s) cannot be mixed with expression(s).
The following code fragment, would not compile:

whereas, the following would work just fine:


Cheers
Harwinder
[ October 20, 2003: Message edited by: Harwinder Bhatia ]
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Thanks for your inputs. Harwinder, that an interesting piece of code.
 
reply
    Bookmark Topic Watch Topic
  • New Topic