| Author |
Boolean in for loop??
|
Cathy Song
Ranch Hand
Joined: Aug 24, 2003
Posts: 270
|
|
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
Joined: Jul 08, 2003
Posts: 24061
|
|
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:
|
[Jess in Action][AskingGoodQuestions]
|
 |
Vinod Venkatasubramanian
Greenhorn
Joined: Oct 19, 2003
Posts: 13
|
|
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
Joined: Nov 27, 2001
Posts: 1561
|
|
|
Cathy, why do you think the program would not compile?
|
I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
|
 |
Cathy Song
Ranch Hand
Joined: Aug 24, 2003
Posts: 270
|
|
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
Joined: Nov 27, 2001
Posts: 1561
|
|
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
Joined: Oct 17, 2003
Posts: 150
|
|
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
Joined: Aug 24, 2003
Posts: 270
|
|
Hi All, Thanks for your inputs. Harwinder, that an interesting piece of code.
|
 |
 |
|
|
subject: Boolean in for loop??
|
|
|