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 9 of 9

Thread: My break seems to be broken.

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My break seems to be broken.

    I'm attempting to use the break <label>; as a goto. But it doesn't want to go.
    I placed a few prints in the code to understand what might be the problem.

    I get a clean compile.
    When I run the class I get:

    A Hello, then a Looping, then a Breaking and then a clean exit.

    I was expecting the break to go to the label HERE: and print another Hello, etc.

    Can someone tell me what I haven't understood? (besides that fact that I shouldn't code GOTO's )

    public class HelloWorld {
     
      public static void main (String[] args) {
     
         int cnt = 0;
         HERE:
         {
           System.out.println("Hello, World");
           if (cnt>2){
             System.out.println("Goodbye, World");
             return;
           }
           System.out.println("Looping");
           cnt+=1;
           while(true){
             System.out.println("Breaking");
             break HERE;
           }
         }
      }
    }
    Last edited by papadilbert; April 11th, 2014 at 09:51 AM.


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: My break seems to be broken.

    break can only be used in loops.
    I'm not sure in older version of java. but in java 6 and 7, the label only works for loops so those labels can only use by break and continue keyword in java. goto exists but its implementation is not.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: My break seems to be broken.

    Why would you want to do this? With the way OO programming works, you should find methods far more useful than gotos.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: My break seems to be broken.

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My break seems to be broken.

    I changed the if to a for infinite loop so the break would be inside a loop as suggested. (See updated original post). It still doesn't break.

    Is there something else I'm missing?

    And yes, I agree that this poor coding.

    --- Update ---

    I then change the for( to a while(true). (See edited example)

    Same result. no goto on the break.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: My break seems to be broken.

    Yes, I think you're coding with a misunderstanding about how the break and continue with label feature is designed to work, and I don't think you're alone. This feature is often mentioned when someone asks about "goto in Java," and I'm not sure why. I think of the feature as more of a 'scope expander' (my lame term) than a goto.

    Please read (or reread) the tutorial section on labeled breaks. Pay particular attention to this sentence: "An unlabeled break statement terminates the innermost switch, for, while, or do-while statement, but a labeled break terminates an outer statement."

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: My break seems to be broken.

    You seem like you have an idea of what parts you need, but you are unsure how to put it all together. I will provide you with how to do it, as well as an explanation for how your code is different.
    public class HelloWorld {
    	public static void main (String[] args) {
            /* This is a for loop. Between the parentheses of the for loop,
             * we provide 3 pieces of information. The first part is the
             * dependent variable for the loop. The second part is the condition
             * where the loop will break. The third part is an update to the
             * dependent variable.
             */
    	for(int cnt=0;cnt<=2;cnt++) {
                // These are your print statements.
                System.out.println("Hello, World");
                System.out.println("Looping");
            }
            // This will be called after the break
            System.out.println("Goodbye, World");
        }
    }
    In your code, cnt was the "dependent variable". A dependent variable is the variable in which the loop is dependent on. This variable is often updated between loop iterations until an "exit case" is reached. So you were creating cnt outside of your "HERE block", you were using that variable to check if the program should be ended (when cnt>2), and you were incrementing the variable towards the end of each iteration (cnt+=1).
    For loops allow you to tackle all three parts in the beginning. You seem to be aware of while loops, so to make the structure of the for loop make more sense, the following for loop:
    for(int cnt=0;cnt<=2;cnt++) {
        ...
    }
    is equivalent to this while loop:
    int cnt=0;
    while(cnt<=2) {
        ...
        cnt++; // cnt++ is the same as cnt+=1
    }
    They are the same loops, but the for loop just allows a more condensed way of writing the while loop.
    Also in your code, you would print out: Goodbye, World and terminate the program (returning from the main terminates the program). Your code says: if cnt is greater than 2, print and exit. As you can see in the above while loop, the condition in the for loop work like: Loop while the condition is true. So, the phrase: exit when cnt is greater than 2, can be also written as: loop while cnd is less than or equal to 2. This is why the condition of the while loop is: cnt<=2. When cnt is greater than 2, the loop will terminate, Goodbye, World will be printed, and the program will terminate.

    You will notice a break statement was not needed for this to work. This is because cnt>2 is ALWAYS reachable, so we don't need to do anything fancy (like break statements) to exit the loop.

    Tell me how much of this you understand.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. #8
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My break seems to be broken.

    Thanks for the feedback. I understood all of that. I already understand iterations, loops, etc.

    But what I'm trying to understand is how to effectively code a goto. GregBrannon's insight has given me the clue I needed, I think. I still need to rearrange the code to try it. My new understanding is that the break doesn't jump to the label. It jumps to the end of the bracketed routine of that label. I might be able to work with that.

  9. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: My break seems to be broken.

    My new understanding is that the break doesn't jump to the label. It jumps to the end of the bracketed routine of that label.
    Exactly.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. I've got a question on my somehow broken code
    By idclmao in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 2nd, 2014, 05:10 AM
  2. Help with a broken for loop?
    By Iantaylora in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 19th, 2013, 06:11 PM
  3. Replies: 2
    Last Post: December 16th, 2013, 04:36 PM
  4. send image file from java to php broken pipe exception occured
    By ashi123 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 21st, 2011, 02:53 PM
  5. Broken JPanel?
    By psu in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 22nd, 2011, 11:11 AM