Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding break LABEL

 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When i am writing like this ,output is:
0
1
2
3
4
6
7
8



When i am writing like this,output is:
0
1
2
3
4

I have debugged the code also,but i am unable to conclude the behavior of labelled break.
Please let me know.
 
John Dell'Oso
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the first example, you're not breaking out of the for loop (you're just breaking out of the statement body), whereas in the second example you are breaking right out of the for loop.

Regards,
JD
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi JD,
Thanks for your reply.
//
In the first example, you're not breaking out of the for loop (you're just breaking out of the statement body),
//

I know this may be a simple issue.
But,from the location of COME in first example,i wonder,how the cursor goes again to for loop to print 6,7,8.
Please clarify.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first code is semantically equivalent to the following code:



Does that help?
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ilja for your reply.
But,the way you have given,i think it will become an infinite check because it will check if i==5,then will go to come if i==5,then again into same block ,and again that checking and so on.
In this case when will cursor come out of block before which COME is written.



If you would have given the above way,i would have definitely agreed!!
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But,the way you have given,i think it will become an infinite check because it will check if i==5,then will go to come if i==5,then again into same block ,and again that checking and so on.


The "break" statement causes the current iteration to be skipped and continues with the next iteration if its used in a loop.

To learn about "break" more, please have a look at this link .

Does this offer some more hints?
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raghavan,
Thanks for your reply.

The "break" statement causes the current iteration to be skipped and continues with the next iteration if its used in a loop.

This is true when label comes after line of for loop and before opening braces.When break is written after opening braces,it is ignored and equivalent to writing no break.
I think i will have to memorize this thing.
But,the main thing of break is done by unlabelled break,so i think there is no use of labelled break.
And i have seen the link given by you.From that i think labelled continue is also of no use.
Please correct me if i am wrong somewhere.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic