• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use Labeled break

 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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) ===|
 
Henry Wong
author
Marshal
Pie
Posts: 21493
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34965
378
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21493
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
author & internet detective
Marshal
Posts: 34965
378
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
2
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic