Results 1 to 11 of 11
Like Tree3Likes
  • 1 Post By DarrylBurke
  • 2 Post By Tolls

Thread: Help with return statements

  1. #1
    pilpod is offline Member
    Join Date
    Aug 2016
    Posts
    9
    Rep Power
    0

    Default Help with return statements

    I am very new to computer programming and have been trying to learn the java language.

    It has been going fairly smoothly so far, but I just can't seem to understand return statements, how they work and when they're needed?

    I was also recently messing around in eclipse with return statements and was wondering why this works:

    Java Code:
    public int minValue(int a, int b) {
    
    if (a > b)
       return b;
    else
       return a;
    }
    but when I add in a variable within the method and return to that, it no longer works:

    Java Code:
    public int minValue(int a, int b) {
       int min;
       if (a > b)
         min = b;
         return min;
       else
         min = a;
         return min;
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Help with return statements

    This is why you should always stick brackets around code blocks...

    I assume this is giving a compilation error?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    pilpod is offline Member
    Join Date
    Aug 2016
    Posts
    9
    Rep Power
    0

    Default Re: Help with return statements

    The first example runs just fine. In the second example, eclipse underlines the else in red and just tells me to remove it. Unfortunately I am not home atm, and I can't remember the exact error it gave me.

    When you say stick brackets around code blocks, do you mean to bracket the lines of code under the if and the else?

  4. #4
    tanzeelNiazi is offline Member
    Join Date
    Aug 2016
    Posts
    4
    Rep Power
    0

    Default Re: Help with return statements

    Like Tolls said, you should use brackets around code blocks. You have used two statements in the IF block but the compiler will always execute the first return statement whether the condition is true or not. When compiler finds ELSE Block it thinks it's IF Block is missing, therefore, your program is not compiling. It is better to use curly braces around your code like this

    Java Code:
    public int minValue(int a, int b) {
       int min;
       if (a > b) {
         min = b;
         return min;
      } else {
         min = a;
         return min;
     }
    }
    or instead

    Java Code:
    public int minValue(int a, int b) {
       int min;
       if (a > b) {
         min = b;
      } else {
         min = a;
     }
      return min;
    }
    Hope this solves the issue.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Help with return statements

    Java Code:
    public int minValue(int a, int b) {
        int min;
        if (a > b)
            min = b;
        return min;
        else
            min = a;
        return min;
    }
    If I tidy up the indentation so it follows the flow of code it might help you see a bit better.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    pilpod is offline Member
    Join Date
    Aug 2016
    Posts
    9
    Rep Power
    0

    Default Re: Help with return statements

    Yes! That cleared it up for me. I have actually learned about code blocks but I guess I forgot all about it when I was looking at this code.

    So when you bracket the code of the if statement, it only sees the return statement if the condition is true?

    And if it's not bracketed, it just sees the return statement right before it hits the else statement and stops the method?
    Last edited by pilpod; 08-23-2016 at 06:36 PM.

  7. #7
    tanzeelNiazi is offline Member
    Join Date
    Aug 2016
    Posts
    4
    Rep Power
    0

    Default Re: Help with return statements

    If you want to execute multiple statements in an IF block then you must use curly braces to enclose those statements. Omitting curly braces from an IF block can result unexpected results. For example;

    Java Code:
    if (true)
    #statement 01
    In the above example #Statement 01 will execute only if the condition evaluates to true, but when

    Java Code:
    if (true)
    #statement 01
    #statement 02
    In this example #Statement 02 will always execute whatever condition evaluates to. If you want to make #Statement 02 a part of IF block then you should use:

    Java Code:
    if (true) {
    #statement 01
    #statement 02
    }
    In your case the compiler never reaches the ELSE Block and returns from first return statement i.e. in the IF Block.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Help with return statements

    For if/else, for loops, and other similar constructs, I always use {}. Even when I syntactically don't have to. It helps
    reduce debugging time.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    pilpod is offline Member
    Join Date
    Aug 2016
    Posts
    9
    Rep Power
    0

    Default Re: Help with return statements

    Ah, I will have to start getting into the habit of using curly braces even when I don't need to.

    Thank you all for the help, can't believe I made such a simple mistake hehe.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Help with return statements

    Quote Originally Posted by pilpod View Post
    Ah, I will have to start getting into the habit of using curly braces even when I don't need to.
    You always need to. Always.

    db
    quad64bit likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Help with return statements

    Quote Originally Posted by DarrylBurke View Post
    You always need to. Always.

    db
    I've had at least one review comment before now saying I shouldn't have put braces around a single line 'if' statement. I seem to remember the comment being along the lines that I should have confidence in my coding abilities.
    I replied that I did have confidence in my coding abilities, it was the coding abilities of those who came after me that I couldn't guarantee...and in any case, even I make mistakes.
    :)
    DarrylBurke and jim829 like this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 2
    Last Post: 05-21-2012, 10:59 AM
  2. Using IF statement to control return statements.
    By Teclis in forum New To Java
    Replies: 21
    Last Post: 04-16-2011, 08:42 AM
  3. Replies: 7
    Last Post: 04-11-2011, 07:26 PM
  4. Return statements in methods
    By adjit in forum New To Java
    Replies: 12
    Last Post: 03-17-2011, 02:31 PM
  5. Static Method and Return Statements
    By berelson in forum New To Java
    Replies: 2
    Last Post: 11-29-2008, 11:17 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •