my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Flow Control : Labels Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Flow Control : Labels" Watch "Flow Control : Labels" New topic
Author

Flow Control : Labels

kumar bangali
Greenhorn

Joined: Dec 01, 2001
Posts: 27
Hi
This clarification is regarding the use of labels in flow control. I was under the impression that labels could be used to pass control to any statement and not necessarily to the start of the loop alone. But in the following examples the code does not compile in (1) and (2) but compiles fine in (3)
(1)

2

3

Thanx in advance


Bos Indicus
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
you cannot forward reference a label, it must be declared before it is used with continue or break, also the label tag must immediatly proceed the enclosed statement.
the code example below will not compile since the label "here:" does not immediatly proceed the for block

here:
int j=0;
for(int i=0; i<10; ++i) {
if(i==5) break here;
}
if you swap here: and int j=0; then the code will compile


<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
you could do such a thing in C language. basically java does not have a goto statement.
because break and continue are nothing but transfer of control, just like goto in procedural languages.
In java break and continue are to be used with
for, while and do-while loops. And second the labels have to just before where the for,while statements are. You cannot place your labels anywhere you want.

Originally posted by kumar bangali:
Hi
This clarification is regarding the use of labels in flow control. I was under the impression that labels could be used to pass control to any statement and not necessarily to the start of the loop alone. But in the following examples the code does not compile in (1) and (2) but compiles fine in (3)
(1)

2

3

Thanx in advance
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Flow Control : Labels
 
Similar Threads
Flow Control
Mock Exams
Flow Controls
From JQ+
Label???