aspose file tools*
The moose likes Beginning Java and the fly likes Regarding break LABEL 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 » Java » Beginning Java
Bookmark "Regarding break LABEL" Watch "Regarding break LABEL" New topic
Author

Regarding break LABEL

Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418

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

Joined: Apr 08, 2004
Posts: 130
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

Joined: Apr 15, 2006
Posts: 418
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

Joined: Jul 11, 2001
Posts: 14112
The first code is semantically equivalent to the following code:



Does that help?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
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

Joined: Apr 20, 2006
Posts: 3344


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?


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
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.
 
Don't get me started about those stupid light bulbs.
 
subject: Regarding break LABEL