Return Statement

Missing return statement

Page 1 of 1

3 Replies - 2499 Views - Last Post: 13 August 2010 - 07:10 PM Rate Topic: -----

#1 Vincent53  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-May 10

Return Statement

Posted 13 August 2010 - 06:06 PM

Hello everyone,

I have a bit of a problem I was trying to boil down the following code snippet to just onr return statement. I think that I am on the right track.But I keep getting an error that says I am missing a return statement in the beginning part. I am stumped. I would appreciate it if anyone could give me a few hints.

 public boolean isTheFirstOneBigger(int num1, int num2){ //this line is where I keep getting missing return 
                                                                     // statement error
        while (num1 > num2) 
            return true;
               if (num1 <= num2 == false);

            
        }
        
    }


Thank You
Vincent53

This post has been edited by macosxnerd101: 13 August 2010 - 06:09 PM
Reason for edit:: The end code tag is: [/code], not [end code].


Is This A Good Question/Topic? 0
  • +

Replies To: Return Statement

#2 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Return Statement

Posted 13 August 2010 - 06:11 PM

Your problem is that the return statement is within a WHILE statement. There is always the possibility that the WHILE statement will not be executed, so you MUST provide an alternate to return.

Can simplify by:
return (num1 > num2);


Also, I don't see the point of your If statement. It also has a semicolon behind it.

This post has been edited by bcranger: 13 August 2010 - 06:12 PM

Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10392
  • View blog
  • Posts: 38,458
  • Joined: 27-December 08

Re: Return Statement

Posted 13 August 2010 - 06:12 PM

You only return in the loop. So if the loop doesn't occur, there isn't a return statement. Hence the "missing return statement" error. Also, why use a loop here? I would just use an if statement, as a return statement will end the method call.

Also, your condition in the if statement is equivalent to if(num1 > num2), which is the same as your loop. No need to compare the condition to false here. Also, why do you have a semicolon after the if statement?
if (num1 <= num2 == false);


Was This Post Helpful? 0
  • +
  • -

#4 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: Return Statement

Posted 13 August 2010 - 07:10 PM

Why do you need a loop to complete this problem ? I only asked because thankfully the function name gives some insight into what you wish to achive. isTheFirstOneBigger followed by two integers as parameters. Now i am assuming that the first paramater is the one you wish to know "Is it Bigger" than the second parameter.

Subsequently you should not need a loop to achive this, as macrosxnerd101 showed.

Something along the lines of
public boolean isTheFirstOneBigger( int first, int second ) {
    return ( first > second );
}


Should be enough,to test this i wrote the following class and tested it out with a simple nested for-loop.

public class Number {

	private Number() {
		
		for( int i = 0; i < 20; i++ )	{
			for ( int y = 20; y > 0; y-- )
				System.out.println("First " +i+" Second " +y+" Result :: " + isTheFirstOneBigger(i,y) );
		}

	}

	private boolean isTheFirstOneBigger( int first, int second )	{
		return ( first > second );
	} 

	public static void main(String[] args)	{
		new Number();
	}

}



A snipped of the output showing the correct comparisons being achieved.

Quote

First 10 Second 20 Result :: false
First 10 Second 19 Result :: false
First 10 Second 18 Result :: false
First 10 Second 17 Result :: false
First 10 Second 16 Result :: false
First 10 Second 15 Result :: false
First 10 Second 14 Result :: false
First 10 Second 13 Result :: false
First 10 Second 12 Result :: false
First 10 Second 11 Result :: false
First 10 Second 10 Result :: false
First 10 Second 9 Result :: true
First 10 Second 8 Result :: true
First 10 Second 7 Result :: true
First 10 Second 6 Result :: true
First 10 Second 5 Result :: true
First 10 Second 4 Result :: true
First 10 Second 3 Result :: true
First 10 Second 2 Result :: true
First 10 Second 1 Result :: true
First 11 Second 20 Result :: false

This post has been edited by DaneAU: 13 August 2010 - 07:13 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1