This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
The question relates to this simple piece of code - with one for loop within another. I am astounded by the answer and am hoping someone can help me understand the output.
Now here are the answers: answers: i = 0, j=3 i = 1, j=3 I am happy with the first line of the answer. But I can't seem to understand why the inner loop allows itself to iterate to give j a value of 3? Is it because of the post-increment of j? I have changed it to pre-increment but the answer is still the same. [ edited to surround code with the [code] and [/code] UBB Tags -ds ] [ January 21, 2003: Message edited by: Dirk Schreckmann ]
This is excerpt from JLS v2.0 14.13 The for Statement 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 ( ForInit ; Expression ; ForUpdate ) StatementNoShortIf ForInit: StatementExpressionList LocalVariableDeclaration ForUpdate: StatementExpressionList StatementExpressionList: StatementExpression StatementExpressionList , StatementExpression <...> So after executing Statement it executes ForUpdate and then Expression, since when j=3 Expression evaluates to false it jumps out.
Originally posted by Tony Virdee: I am happy with the first line of the answer. But I can't seem to understand why the inner loop allows itself to iterate to give j a value of 3? Is it because of the post-increment of j? I have changed it to pre-increment but the answer is still the same.
I'm not sure what the other guy was saying... but here is how I think of the for loop and how it works. Think of a for loop as a mis-written while loop. The first statement would occur just before the while loop was entered. Remember this means that the expression is tested to be true or it will not enter the loop even once. The center expression is what would be contained in the while(j < 3) of the while loop. The final statement occurs at the end of it. It always gets performed at the end of the loop just before it repeats (or doesn't). Since it is it's own statement, being ++loop; or loop++; does not matter. Since the loop does not fail until that center expression is provel false, j must increment to be greater than or equal to 3. Following my method of explanation, I hope you can understand how a for loop works. I think you would better understand if you did this..
Originally posted by Tony Virdee: Is it because of the post-increment of j? I have changed it to pre-increment but the answer is still the same.
yes. the increment, whether post or pre, gets done at the end of the for loop. So j gets to be 3 at the end of the innerloop and busts out. there is the print statement yet to happen in the outer for loop ... i actually makes it to 2, but when it does, it busts out of the loop, and there is no print statement to print its value then.