Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 755451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:07:53+00:00 2026-05-14T15:07:53+00:00

Is breaking and continuing the only uses of labeled statements in Java? When have

  • 0
  • Is breaking and continuing the only uses of labeled statements in Java?
  • When have you used Labeled Statements in your programs?

Sorry the code snippet has been deleted. I am splitting the question

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:07:54+00:00Added an answer on May 14, 2026 at 3:07 pm

    JLS 14.7 Labeled statements

    (edited for clarity)

    Statements may have label prefixes (Identifier : Statement). The Identifier is declared to be the label of the immediately contained Statement.

    Unlike C and C++, the Java programming language has no goto statement; identifier statement labels are used with break (§14.15) or continue (§14.16) statements appearing anywhere within the labeled statement.

    So the JLS is clear that labels are used with break or continue, and no other grammatical element of the Java programming language uses it.

    Strictly speaking, break and continue, labeled or not, are NEVER necessary. They can always be written out of the code. Used idiomatically, however, they can lead to more readable code.


    Here’s an illustrative example: given an int[], we want to :

    • print "One (1)" on 1
    • print "Two (2)" on 2
    • print "Zero " on 0
    • immediately stop processing on any other number

      int[] arr = { 1, 2, 0, 1, -1, 0, 2 };
      loop:
      for (int num : arr) {
          switch (num) {
          case 1:
              System.out.print("One ");
              break;
          case 2:
              System.out.print("Two ");
              break;
          case 0:
              System.out.print("Zero ");
              continue loop;
          default:
              break loop;
          }
          System.out.print("(" + num + ") ");
      }
      // prints "One (1) Two (2) Zero One (1) "
      

    Here we see that:

    • The different numbers are processed in a switch
    • Unlabeled break in the switch is used to avoid “fall-through” between cases
    • Labeled continue loop; is used to skip post-processing on case 0: (the label is not necessary here)
    • Labeled break loop; is used to terminate the loop on default: (the label is necessary here; otherwise it’s a switch break)

    So labeled break/continue can also be used outside of nested loops; it can be used when a switch is nested inside a loop. More generally, it’s used when there are potentially multiple break/continue target, and you want to choose one that is not immediately enclosing the break/continue statement.


    Here’s another example:

        morningRoutine: {
            phase1: eatBreakfast();
            if (grumpy) break morningRoutine;
            phase2: kissWife();
            phase3: hugChildren();
        }
        http://stackoverflow.com is the best website ever!
    

    Here’s another case of a labeled break being used not within an iterative statement, but rather within a simple block statement. One may argue that the labels lead to better readability; this point is subjective.

    And no, the last line DOES NOT give compile time error. It’s actually inspired by Java Puzzlers Puzzle 22: Dupe of URL. Unfortunately, the puzzle does not go into “proper” use of labeled statements in more depth.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some business objects which use: Web.Configuration.WebConfigurationManager.AppSettings.Item(SomeSetting) Now that I'm breaking those objects
I have one piece of Cocoa code I wrote that takes in an XML
It seems UITextFields convert all non-breaking spaces (nbsp, U+00A0) to spaces (U+0020). This causes
One of our clients is breaking the software in every way imaginable, and he's
BCL Specifically, am I breaking the EULA by doing this?
Is it possible to format certain text in a WinForm Label instead of breaking
In this question, I asked about breaking a dataset into subsets, and got a
I've just noticed that tidy_repair_string() is removing my non-breaking spaces from empty elements causing
When debugging an application in Visual Studio, there are 2 options for breaking on
I've been tasked with implementing a Date/Time selector for several areas of our web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.