permaculture playing cards*
The moose likes C / C++ and the fly likes How to use  Labeled break Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Languages » C / C++
Bookmark "How to use  Labeled break" Watch "How to use  Labeled break" New topic
Author

How to use Labeled break

Mahtab Alam
Ranch Hand

Joined: Mar 28, 2012
Posts: 249

I am getting error in compiling this code


C:\Users\Kavya\Desktop\La.c||In function `main':|
C:\Users\Kavya\Desktop\La.c|14|error: syntax error before "Outer_loop"|
C:\Users\Kavya\Desktop\La.c|14|error: `Outer_loop' undeclared (first use in this function)|
C:\Users\Kavya\Desktop\La.c|14|error: (Each undeclared identifier is reported only once|
C:\Users\Kavya\Desktop\La.c|14|error: for each function it appears in.)|
||=== Build finished: 4 errors, 0 warnings (0 minutes, 0 seconds) ===|

Oracle Java Programmer , Oracle SQL Expert , Oracle Java Web Component Developer
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

Mahtab Alam wrote:
C:\Users\Kavya\Desktop\La.c||In function `main':|
C:\Users\Kavya\Desktop\La.c|14|error: syntax error before "Outer_loop"|
C:\Users\Kavya\Desktop\La.c|14|error: `Outer_loop' undeclared (first use in this function)|
C:\Users\Kavya\Desktop\La.c|14|error: (Each undeclared identifier is reported only once|
C:\Users\Kavya\Desktop\La.c|14|error: for each function it appears in.)|
||=== Build finished: 4 errors, 0 warnings (0 minutes, 0 seconds) ===|


If memory serves, labelled break / continue were included in Java because Java doesn't support the goto statement (from C/C++ language that Java was based on)... In other words, with C, there isn't a label break -- you will have to use the goto statement instead.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30383
    
150

According to MSDN, C does in fact have a break statement. (I knew C++ did so looked up if C did). It's not a labeled break. But you don't need a labeled break there as you are breaking out of a single loop. Just like the MSDN example.

In consider labeled breaks to be bad coding practice and never use them in Java. They make the card harder to follow - just like the go to statement. Whereas the break statement used judiciously is clear because it is always breaks out of a loop. Again, like in your example.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40


Yes. C has a break / continue. I use it all the time. It is the labelled break / continue that doesn't exist in C.

Interestingly, IMO, one of the reasons Java didn't adopt the goto keyword, was the hate against it in C/C++. The labelled break / continue was supposed to solve that -- to provide the important goto functionality without providing the goto functionality that seemed to be hated. I don't think that it was successful though, as many Java developers don't seem to like labelled break / continue either.

Henry
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30383
    
150

Henry Wong wrote:. I don't think that it was successful though, as many Java developers don't seem to like labelled break / continue either.

I think that's because Java developers are used to smaller methods where the goto/label isn't needed at all. I'm having trouble thinking of a use for goto/labeled break that would be better solved by refactoring to smaller methods.
Luke Leber
Ranch Hand

Joined: Sep 20, 2013
Posts: 41
    
    2

Henry Wong wrote:Java didn't adopt the goto keyword


goto is a reserved keyword in the Java specification. I don't even like the fact that they even reserved the little demon.

Just thought to inject this to keep things 100% correct.

*The only time that I've seen such labels in Java is when reconstructing decompiled code...which is never a fun task.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to use Labeled break