Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: what is this? Variable?

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default what is this? Variable?

    public class BreakingLoop {
     
         public static void main(String[] args) {
     
             int count = 0;
             loop_one:
     
                 for (int k=5;k < 100;k++) {
     
                     for (int j=3; j < 100; j++) {
     
                         if (j % 2 == 0) {
     
                             break;
                         }
     
                         if (k == 67) {
     
                             break loop_one;
     
                         }
                         count ++;
                     }
                 }
     
             System.out.println(count);
         }
     }

    i was searching some mild exercises on the net and i saw this one.. and one part intrigues me.
    what is that
    loop_one:
    ? the only thing i can notice and i dont know if my assumption is right..is, does it refer to the loops? and how come?
    when i removed it.. my IDE gave me an error -
    undefined Label

    can anyone kindly give me some explanation about that.. i never encountered such one before
    thanks


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: what is this? Variable?

    It's called a label. It's an old relic from way back when where you could simply "jump" to another section of code and start executing from there (via the infamous goto keyword). In Java, labels function a little differently. Technically goto is a defined keyword, but it doesn't do anything (and I think actually gives you a compile error). However, you could break/continue to a label. What that allows you to do is break or continue up multiple blocks.

    label1:
    for(int i = 0; i < 5; ++i)
    {
        for(int j = 0; j < 5;++j)
        {
            break label1; // breaks out of both loops, not just the inner loop
        }
        System.out.println("this will never execute");
    }

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (May 7th, 2011)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: what is this? Variable?

    thanks for the clear answer

Similar Threads

  1. variable might not have been initialized
    By SV25 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 25th, 2011, 10:58 AM
  2. Must I add a Class Variable?
    By maress in forum Java Theory & Questions
    Replies: 1
    Last Post: February 24th, 2011, 04:40 AM
  3. variable not recognised
    By gochi7 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 20th, 2011, 08:35 PM
  4. Variable might not have been initialized?
    By n56 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 30th, 2010, 03:03 PM
  5. How to use the same variable in various JPanel?
    By danielpereira in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 19th, 2010, 12:08 PM