Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

labeled continue statement problem

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Output:00000


Output:00000

I think,it should print 01234

Reference :

Output:0123456789
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Output:00000
It is correct.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not try to say that...

Please try out the following:

You will get 01222
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve Luke thank you
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10254
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurangkumar,

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

Thanks

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic