Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Boolean in for loop??

 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • 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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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:
 
Vinod Venkatasubramanian
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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 ]
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Hi All,
Thanks for your inputs. Harwinder, that an interesting piece of code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic