How does returning an || statement work in java?

前端 未结 7 642
北荒
北荒 2021-01-29 16:04

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

7条回答
  •  清酒与你
    2021-01-29 16:46

    That's not what it means. It doesn't mean "return either value, one or the other".

    The return statement returns the result of the entire expression. That is, the OR (||) is evaluated first, before the return, then the result of that expression is returned.

    It might be more clearly written:

    return (helper(start + 1, nums, sum1 + nums[start], sum2)
            || helper(start + 1, nums, sum1, sum2 + nums[start]) );
    

提交回复
热议问题