File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes labeled continue statement? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "labeled continue statement?" Watch "labeled continue statement?" New topic
Author

labeled continue statement?

Ryan Wilson
Ranch Hand

Joined: Apr 16, 2003
Posts: 65
This is modified code from the K & B book

The output is
i = 0, J = 0
Hello
i = 1, J = 0
Hello
i = 2, J = 0
Hello
i = 3, J = 0
Hello
i = 4, J = 0
Hello
i = 5, J = 0
Hello
i = 6, J = 0
Hello
i = 7, J = 0
Hello
i = 8, J = 0
Hello
i = 9, J = 0
Hello
Good-Bye

Is j being re-intialized to 0, or is the increment of the loop never being reached?
This is also from the k&B book
The declaration and initialization happens before anything else in a for loop. And whereas
the other two parts�the boolean test and the iteration expression�will run with
each iteration of the loop, the declaration and initialization happens just once, at the
very beginning.

[QUOTEIteration Expression
After each execution of the body of the for loop, the iteration expression is executed.
This part is where you get to say what you want to happen with each iteration of
the loop. Remember that it always happens after the loop body runs!
The book also states that the only time the iteration won't be avaluated is if there is a break, return or System.exit().
Could someone please explain why the code is behaving the way it is?
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Ryan
You are getting 0 because the inner loop's continue causes the var j to be re-intialized to 0. Secondly even if the variable had not been defined in the for loop then also the value of the variable won't increase because the continue would cause the outer loop's var to increase. The code below shows that the iteration part of the inner for loop is not being executed.
Rory French
Ranch Hand

Joined: Apr 03, 2003
Posts: 97

Is j being re-intialized to 0, or is the increment of the loop never being reached?

The increment of the inner for loop is never reached. The inner loop is in effect "abandoned" without any further processing because of the call to continue outer;. When this call is made, only the iteration expression of the outer loop is processed.
There are questions exactly like this - i.e. containing nested labels, loops and continue statements - in Dan Chisholm's mock exams, so check 'em out because they're really good practice.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I added a line to your code snippet in an attempt to make the behavior more clear:

As you can see from this, the continue statement is causing the outer loop to advance to its next iteration. In each iteration of the outer loop, the inner loop is started from scratch (j = 0). Because of this, the increment statement of the inner loop isn't gettnig a chance to execute. Prior to completion of the first pass of that loop, execution passes out of that loop back to the outer one.
I hope that helps,
Corey


SCJP Tipline, etc.
Ryan Wilson
Ranch Hand

Joined: Apr 16, 2003
Posts: 65
Rory
Thanks for your response. So the continue will also cause the iteration of the for loop to be ignored (along with break, return and System.exit()). j is only intitialized to 0 once! The iteration is ignored that is why 0 is always printed.
Once again thanks, I understood the output but justed to get some clearification as to what one of the two were happening ie re-initializing to 0 or the iteration being ignored.
Thanks for your help
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
In your code j is always intialized to 0. Moreover in your code j is even being redeclared with each iterartion of the outer loop. Secondly the inner loop iteration statement is not being run. See, when the inner loop causes the execution to be transfered to the outer loop the inner loop has finished. So all the variables that were declared in the loop are now lost. When the inner for loop is re-entered then they are again created.
So in your code the var is being redeclared(int j=0), reinitialized (int j=0)
and not being incremented.
[ May 19, 2003: Message edited by: Anupam Sinha ]
Ryan Wilson
Ranch Hand

Joined: Apr 16, 2003
Posts: 65
Anupam,
you are correct. I modified the code to look like this

This code shows that j is reset to zero and the iterartion of the loop is never reached. Thank You
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: labeled continue statement?