aspose file tools*
The moose likes Beginning Java and the fly likes break statement question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "break statement question" Watch "break statement question" New topic
Author

break statement question

Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
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.


"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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 ]

[Jess in Action][AskingGoodQuestions]
Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
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

Joined: Mar 13, 2004
Posts: 1272
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.


Mike Gershman
SCJP 1.4, SCWCD in process
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: break statement question