• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

labeled continue statement?

 
Ryan Wilson
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1090
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 97
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ryan Wilson
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1090
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic