programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

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

Greenhorn
Posts: 5
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

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
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
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
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
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."

Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
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
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