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

Thread: Unreachable code

  1. #1
    Junior Member
    Join Date
    Aug 2024
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Unreachable code

     int var = 2;
     
      block1:
      {
         if (var != 1) {
            break block1; 
            System.out.println("variable is one");  // unreachable
         }
      }
    When i run the block of code above it says unreachable code till i remove the curly braces as shown below
      int var = 2;
     
      block1:
      {
         if (var != 1) 
            break block1; 
     
         System.out.println("variable is one"); // reachable
      }
    Why does groupint the code in a block make it unreachable? TIA

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,107
    Thanks
    65
    Thanked 2,717 Times in 2,667 Posts

    Default Re: Unreachable code

    The brackets following the if statement delimit a block of code to be executed when the expression in the if statement is true.
    A break statement ends the execution of the code within the block. That means the println statement following the break can never be reached.
    Without the brackets, the default block of code controlled by the if statement is the single break statement following the if statement.
    That leaves the println statement outside of the if statement's control. It will be executed when the if statement's expression is false.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Unreachable Statement in simple recursion code
    By omerben in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 12th, 2021, 02:50 PM
  2. Need help with my code (Unreachable code error)
    By Prexxy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2018, 10:18 AM
  3. [ask]port unreachable
    By clavius11 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 6th, 2013, 05:52 AM
  4. another unreachable statement..
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 2nd, 2009, 12:15 PM
  5. unreachable statement Again?
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 1st, 2009, 10:23 AM