• Post Reply Bookmark Topic Watch Topic
  • New Topic

What happens when returning an "||" statement in a recursive call?  RSS feed

 
Greenhorn
Posts: 5
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new to programming so I have been working through the exercises at CodingBat.com. In the Recursion-2 section, there is a problem I wasn't able to solve (recursively) so I looked up an answer. The found solution contained the following code...



What the heck does it mean to return something or something else?! (Last return statement) For the code to work is has to be alternating between the 2 values, but I can't find any mention of return an || statement anywhere. Can someone help me with this?

Thanks a million,
Clif
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you're thinking about it wrong...

What you really have is



that "one thing" is



the helper method returns a boolean, so what you have is



which evaluates to a boolean...which is what the method is declared to return.
 
Clif Boyd
Greenhorn
Posts: 5
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! Thanks for the quick response.

Just so I have this clear. Do you mean that "||" in the return statement sets off a binary tree of method calls, and returns true if any of those branches evaluate to true?

Thanks again,
Clif
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the expression in the return statement results in one value that is then returned. It's no different than return 3 + 4;
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:No, the expression in the return statement results in one value that is then returned. It's no different than return 3 + 4;


or even

return (true || false);
 
Clif Boyd
Greenhorn
Posts: 5
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that the expression will only return a single boolean. But in order to evaluate the return value, it would have to evaluate all the underlying method calls ...

And each method call bifurcates into two more method calls until all branches reach the base case ...

Correct?

So the or statement translates to "If any possible combination of values return true, return true."
Am I thinking about this correctly?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, because you are calling the method recursively in the expression, the method calls need to be resolved*. But that doesn't have anything specifically to do with the use of || -- the same thing would happen with just about any other operator*.


* Sort of; short-circuiting operators such as || and && may end up not having to resolve all of their operands.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your initial question was "What the heck does it mean to return something or something else?!"

that is the question I was answering - no more, no less. you evaluate the first thing, you evaluate the second, you do a boolean OR on them, and return that value.

so I guess, without thinking about it too hard, yes. In this specific instance, there could potentially be many calls...and since we are OR-ing them, if any one returns true, the final result should be.
 
Clif Boyd
Greenhorn
Posts: 5
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. Sorry. I've been thinking about this one for days and you just blew my mind.

Many, many thanks. =)

It seems so obvious now. I keep running into these kinds of things in coding where I have all the answers, but I can't put them together coherently.

Thanks again.

Clif
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!