aspose file tools*
The moose likes Beginning Java and the fly likes For loop within a for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "For loop within a for loop" Watch "For loop within a for loop" New topic
Author

For loop within a for loop

Tony Virdee
Greenhorn

Joined: Jan 21, 2003
Posts: 6
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 ]
raimondas zemaitis
Ranch Hand

Joined: Feb 23, 2001
Posts: 104
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.
Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
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..

That is the same as this..
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
In summary (perhaps), the inner loop allows itself to iterate to give j a value of 3 because that's when the for-loop's boolean test (j<3) returns false.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
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.


giddee up
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: For loop within a for loop