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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "labeled continue statement problem" Watch "labeled continue statement problem" New topic
Author

labeled continue statement problem

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583


Output:00000


Output:00000

I think,it should print 01234

Reference :

Output:0123456789


Tell the difficulties that i am difficult.
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186

Output:00000
It is correct.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Gaurangkumar Khalasi wrote:

Whatever you told nothing new my indirect question was j++ should execute after continue as you can see at reference code.i got that also but it looks bizarre if compare with reference code,quite tricky.
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
The below program uses Labeled Continue statement.That statement is used to continue operation with labeled loop.


The below program uses unlabeled continue statement which is always continue the operation of the loop from which it is executed.


The continue keyword is used to stop just the current iteration of the loop.
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
saloni jhanwar wrote:
Whatever you told nothing new my indirect question was j++ should execute after continue as you can see at reference code.i got that also but it looks bizarre if compare with reference code,quite tricky.


The continue keyword is used to stop just the current iteration of the loop. So, whenever control reach at "continue OUTER" (the value of j=0 because of first iteration ) but because of Labelled Continue statement which tells the JVM to stop just the current iteration of the OUTER (loop), so we will out of the inner for loop and go with next iteration of OUTER.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Gaurangkumar Khalasi wrote:



I didn't find such statement in book that for unlabeled type circumstance j++ should execute while for labeled shouldn't execute.
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
I do not try to say that...

Please try out the following:

You will get 01222
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

No I am not satisfied continue means just skip the current iteration of loop but not current iteration expression as i can see in book.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

Gaurangkumar Khalasi wrote:I do not try to say that...

Please try out the following:

You will get 01222


Wrong the output would be 012012012012012. though whatever you said regarding labelled continue statement is true.


OCPJP 6(100 %) OCEWCD 6(91 %) OCPJBCD(93%)
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

saloni jhanwar wrote:No I am not satisfied continue means just skip the current iteration of loop but not current iteration expression as i can see in book.



yes you are right and it DOES NOT skip the iteration expression. when you encounter continue statement labelled or not ,it stops executing/skip the statements after the continue statement and goes straightway to next iteration i.e the iteration expression is evaluated , and then the test expression. where do you have problem. it is exactly behaving the way you said above
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

how are you posting i am not getting at all your post itself has contradiction.
you said
gurpeet singh wrote:
yes you are right and it DOES NOT skip the iteration expression.

gurpeet singh wrote:
when you encounter continue statement labelled or not ,it stops executing/skip the statements after the continue statement and goes straightway to next iteration


without iteration expression goes straightway to next iteration ??? what you wrote ?

and where is problem? see my question then you will get.

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

saloni jhanwar wrote:how are you posting i am not getting at all your post itself has contradiction.
you said
gurpeet singh wrote:
yes you are right and it DOES NOT skip the iteration expression.

gurpeet singh wrote:
when you encounter continue statement labelled or not ,it stops executing/skip the statements after the continue statement and goes straightway to next iteration


without iteration expression goes straightway to next iteration ??? what you wrote ?

and where is problem? see my question then you will get.




okay let me put in a simpler way.

when you encounter a continue statement , it skips any statement(s) after the continue statement. this is simple right. and then the control goes to the iteration expression. Simple.

Incase of labelled continue, the control goes to iteration expression of the LABELLED loop. e.g if i do continue outer; then it will go to the say for loop preceded with label outer: AND (this is the part where i think you are confused.) it will execute the iteration expression of the labelled loop.

Did you get it now ?
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

If we resume outer "for loop" then that outer "for loop" itself take care for its iteration expression, should it depend on inner loop or continue statement ? if we wont use continue in inner loop then i++ wont happen ?
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

saloni jhanwar wrote:If we resume outer "for loop" then that outer "for loop" itself take care for its iteration expression, should it depend on inner loop or continue statement ? if we wont use continue in inner loop then i++ wont happen ?



if we don't use continue in inner loop then i++ won't happen and the inner loop will keep on executing till its test expression returns true.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

gurpeet singh wrote:
saloni jhanwar wrote:If we resume outer "for loop" then that outer "for loop" itself take care for its iteration expression, should it depend on inner loop or continue statement ? if we wont use continue in inner loop then i++ wont happen ?

if we don't use continue in inner loop then i++ won't happen and the inner loop will keep on executing till its test expression returns true.


I meant in straightway it wont execute its iteration expression and just resume the outer for loop.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3943
    
  17

If you have this for loop:



The for loop's procedural code looks something like this:



So, steps 1&2 handle the "int i = 0" part. We then mark the 'top of the loop'. Steps 4&5 handle the comparison "i < 5" and if it is false, makes the code jump to the line just after the loop. If that doesn't happen then the loop code is executed. Then the end of the loop block is defined (conceptually) at step 7. Here is the key: the increment step is handled at the bottom of the loop, in steps 8&9, after which the loop is sent back to the top.

When you call continue, it moves execution to the BOTTOM_OF_LOOP1, which means it goes through the increment steps before jumping back to the top and the comparison.


Steve
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3943
    
  17

So if we go back to your original code: there are two differences between what I posted before and the procedural code I wrote:
1) it inserts a second loop into line #6
2) it gives an explicit name (OUTER) to the BOTTOM_OF_LOOP1.

The code would be like:
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3943
    
  17

Ok, and I see the original question again it isn't about incrementing the outer loop it is about the inner loop incrementing. If you follow the procedural equivalent I posted earlier, it should answer that question as well: Yes when you continue to the OUTER loop, you skip the inner increment. But that isn't the only issue:


Right there, you re-assign j to 0 at the start of the loop, so you would not be able to see if the j++ was skipped or not - you would always be resetting to 0.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Hi Steve Luke thank you
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Steve Luke wrote:
Right there, you re-assign j to 0 at the start of the loop, so you would not be able to see if the j++ was skipped or not - you would always be resetting to 0.




I've checked J doesn't increment in above case also,as i expect it should be.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3943
    
  17

saloni jhanwar wrote:I've checked J doesn't increment in above case also,as i expect it should be.

Right - that is a proper test to see if j increments or not, and you can tell from your code that it does not, and you can see from my p-code above as to why it doesn't.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7032
    
  16

Gaurangkumar,

I broke up those long comments of yours. Please re-read the UseCodeTags page.

Thanks

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: labeled continue statement problem
 
Similar Threads
For Stament with continue
labeled continue statement?
Use of labelled statement in java ?
for loops
Label (Continue)