Results 1 to 4 of 4
  1. #1
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default using from lable with break and continue

    Java Code:
    public class Break_Loop
    {
    	public static void main(String args[])
    	{
    		outer:
    		for(int i=0;i<3;i++)
    		{
    			System.out.println(i);
    			for(int j=0;j<100;j++)
    			{
    				if(j==10) break outer;
    			}
    			System.out.println("This will not print");
    		}
    		System.out.println("Loops complete");
    	}
    }
    Java Code:
    public class Break_Loop
    {
    	public static void main(String args[])
    	{
    		for(int i=0;i<3;i++)
    		{
    			System.out.println(i);
    			for(int j=0;j<100;j++)
    			{
    				if(j==10)
    				{
    					i=2;
    					break;
    				}
    			}
    			if(i==2)
    			break;
    			System.out.println("This will not print");
    		}
    		System.out.println("Loops complete");
    	}
    }
    Hello
    Both programs have the same result, but the first program uses from lable and the second does not use.The first program is simpler than the second program.
    which one is better? truly, using from lable with break and continue is good or bad or..?

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    ...which one is better?
    Thats a pretty subjective question really - both are fine. When loops get really big, complex and nested, then sometimes label makes more sense since it actually provides a 'label' for the different break points. Some might argue that its a cheap java goto, but it has some advantages.

    I'd say if it makes your coding life easier, go for it. It has the benefit of allowing you to avoid creating additional operations and variables for achieving the same effect, which might be useful in memory or performance intensive loops.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by kcsatheesh View Post
    Pls remind!
    Labels are discouraged in java
    Says who?

    kind regards,

    Jos

  4. #4
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Hello
    From Bruce[1].Eckel.Thinking.In.Java.4th.Edition.Dec.2007.eBook-BBL:

    The problem is not the use of goto, but the overuse of goto.in rare situations goto is actually the best way to structure control flow.
    Although goto is a reserved word in Java, it is not used in the language.Java has no goto.
    However, it does hava something that looks a bit like a jump tied in with the break and continue keywords. It is not jump but rather a way to break out of an iteration statement.
    The reason it is often thrown in with discussion of goto is because it uses the same mechanism: a lable.
    The only place a label is useful in Java is right before an iteration statement. And that means right befor-it does no good to put any other statement between the label and the iteration.
    And the sole reason to put a label before an iteration is if you are going to nest another iteration or a switch inside it. That is because the break and continue keywords will normally interrupt
    only the current loop, but when used with a label, they will interrupt the loops up to where the label exists:
    Java Code:
    label1:
    outer-iteration{
      inner-iteration{
      //...
      break;
      //...
      continue;
      //...
      break lable1;
      //...
      continue label;
     }
    }
    question: may you instance an example that uses from label more that this cause bad use from labels? that is overuse.

Similar Threads

  1. Program doesn't continue...
    By Reiyn in forum New To Java
    Replies: 7
    Last Post: 10-07-2008, 10:28 PM
  2. press any key to continue
    By dotnet007 in forum New To Java
    Replies: 3
    Last Post: 05-11-2008, 05:19 AM
  3. Demonstration of break and continue keywords
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:47 PM
  4. How to use Continue
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •