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

Thread: What is the difference between a statement surrounded by curly brackets and one that isn't?

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    26
    My Mood
    Where
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default What is the difference between a statement surrounded by curly brackets and one that isn't?

    Question is a little bit elementary, but i just need a programmer to programmer explanation.
    For example:
    if (number < 1 || number > 100 ) 
     throw new InvalidInputException();
     System.out.println("You entered " + number);
    In this example, the statement
      System.out.println("You entered " + number);
    gets executed if the number entered is within range or an exception is thrown if it isn't.
    But if i change the if statement to:
    if (number < 1 || number > 100 ) {
     throw new InvalidInputException();
     System.out.println("You entered " + number);
    }
    it says the code
    System.out.println("You entered " + number);
    is unreachable. Why is this? and of what significance are curly brackets in control statements generally?


  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: What is the difference between a statement surrounded by curly brackets and one that isn't?

    The if() {} statement is not inside a try {} block, so there is nothing to catch that exception, and the program will end.

    If a number out of range is entered, the program stops running.

    If a number in range is entered, the if() {} block doesn't even execute, so in no case will the println() statement ever be executed.

    -summit45

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    26
    My Mood
    Where
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default Re: What is the difference between a statement surrounded by curly brackets and one that isn't?

    Yeah, the if () {} statement is inside a try catch statement, i just forgot to include it in my question.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: What is the difference between a statement surrounded by curly brackets and one that isn't?

    This code:

    if (number < 1 || number > 100 ) 
     throw new InvalidInputException();
     System.out.println("You entered " + number);

    Should be re-written as this:

    if (number < 1 || number > 100 ) {
     throw new InvalidInputException();
    }
     System.out.println("You entered " + number);

    Which hopefully clears up your question. Always use curly brackets for if statements and loops!
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    ojonugwa (August 30th, 2013)

  6. #5
    Member
    Join Date
    Jul 2013
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: What is the difference between a statement surrounded by curly brackets and one that isn't?

    Quote Originally Posted by ojonugwa View Post
    Yeah, the if () {} statement is inside a try catch statement, i just forgot to include it in my question.
    Well if it throws that exception, that breaks out of the try {} block, and executes the catch statement. And if the number is in range, the if() {} block doesn't execute, so either way, that println() will never run.

    -summit45

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: What is the difference between a statement surrounded by curly brackets and one that isn't?

    Quote Originally Posted by summit45 View Post
    Well if it throws that exception, that breaks out of the try {} block, and executes the catch statement. And if the number is in range, the if() {} block doesn't execute, so either way, that println() will never run.

    -summit45
    I don't think you quite understand the question.

    In the first code example, the if statement does not have any curly brackets. That means that when the if statement evaluates to true, only the very next statement will be run, which is the throw statement. If the if statement evaluates to false, the println() will indeed run.

    However, the next code example does use curly brackets, but it puts the end curly bracket after the print statement. Since the throw always happens right before the print statement, the print statement is never run, hence the compiler error.

    The solution is to properly use curly brackets for every if statement and loop.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. The Following User Says Thank You to KevinWorkman For This Useful Post:

    ojonugwa (August 30th, 2013)

  9. #7
    Member
    Join Date
    Jul 2013
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: What is the difference between a statement surrounded by curly brackets and one that isn't?

    Quote Originally Posted by KevinWorkman View Post
    I don't think you quite understand the question.

    In the first code example, the if statement does not have any curly brackets. That means that when the if statement evaluates to true, only the very next statement will be run, which is the throw statement. If the if statement evaluates to false, the println() will indeed run.

    However, the next code example does use curly brackets, but it puts the end curly bracket after the print statement. Since the throw always happens right before the print statement, the print statement is never run, hence the compiler error.

    The solution is to properly use curly brackets for every if statement and loop.
    Yeah I got him, that's what I said, or so I thought lol. Well I didn't talk about that first if() without braces but yeah exactly, that's just it. If you put that println() inside the if(), and the person enters a right number (within range), the if() block doesn't execute, and it won't print. If they enter a wrong number (out of range), the if() block throws an exception, which stops it from executing further code, and so println() still doesn't run.

    -summit45

  10. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: What is the difference between a statement surrounded by curly brackets and one that isn't?

    Quote Originally Posted by summit45 View Post
    Yeah I got him, that's what I said, or so I thought lol. Well I didn't talk about that first if() without braces but yeah exactly, that's just it. If you put that println() inside the if(), and the person enters a right number (within range), the if() block doesn't execute, and it won't print. If they enter a wrong number (out of range), the if() block throws an exception, which stops it from executing further code, and so println() still doesn't run.

    -summit45
    The point is that the println() *does* run in the first example when the if statement evaluates to false. It's still a reachable statement. In the second example, there's no way that the code could execute to run the println() statement, hence the error.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. The Following User Says Thank You to KevinWorkman For This Useful Post:

    summit45 (August 30th, 2013)

Similar Threads

  1. Replies: 3
    Last Post: October 8th, 2010, 09:21 AM
  2. Curly braces or semicolon?
    By SweetyStacey in forum Java Theory & Questions
    Replies: 7
    Last Post: May 7th, 2010, 06:34 AM
  3. [SOLVED] why casting int to String is not possible through brackets method
    By voltaire in forum Java Theory & Questions
    Replies: 2
    Last Post: May 2nd, 2010, 04:00 PM