File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

break statement question

 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This looks simple, still not sure.Please help to understand..

class tst
{
public static void main(String ara[])
{
for(int i=0; i<5; i++)
{
one:
// System.out.println("At ONE:"+i+" ");

for(int j=0; j<5; j++)
{
if(j==2) break one;
System.out.println(i+" "+j);
}
}
}
}

Looks like following code works like-
If commented line is kept unchanged, it considers inner "for loop" for j as a part of label one:

If comment is removed, it works like- println is a part of label one:

If comment is removed, code do not identify label one: I am getting message "undefined label: one.

Here, in book i found, that label can not be a part of a loop or switch.

My questions are:
1. Why code gives error if comment is removed.
2. Why code works if comment is kept. (label is a part of outer loop for i)

Thank you for your time.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java labels can be attached to loops only -- i.e., having a label followed by a System.out.println() is illegal code, plain and simple.

In "C", labels can be attached to any statement, and can be the target of a "goto". In Java, the only purpose for a label is to indicate which loop a "break" or "continue" should apply to -- so labels attached to non-loops are useless, anyway, even if they were allowed.

Finally, I don't know what book says that you can't have a label inside a loop, but in any case, the book is simply wrong -- you can label a loop nested inside another loop.
[ October 09, 2004: Message edited by: Ernest Friedman-Hill ]
 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply. Tried another program having break for a if condition. Looks it works for if condition. I think break is not only for loop. Please try following code (also try removing commented line)

class tst
{
public static void main(String ara[])
{
int i = 0;
one:
if(i<2)
{
System.out.println("A");

two:
for(int j = i; j != i+2; j++)
{
System.out.println("B");
// break one;
}
i++;
System.out.println("C");
}
}
}
 
Mike Gershman
Ranch Hand
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The label "one:" effectively labels the block controlled by "if(i<2)"

You can label most statements except declarations, but there is no point in labeling a statement that does not contain or enclose a break or continue statement referencing that label.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic