1

My question is about how exactly do method call work as a condition of an if/else statement ?

I understand recursion is a method that calls itself by changing the value of its arguments, a solution is tried and if it is the right one the recursion is stopped.

Such simple recursion make sense to me:

/* Given a string, compute recursively (no loops) a new string where all the 
 * lowercase 'x' chars have been changed to 'y' chars.
 */
public String changeXY(String str) {
    if(str.length() == 0)
        return str;

    if(str.charAt(0) == 'x')
        return 'y' + changeXY(str.substring(1));

    return str.charAt(0) + changeXY(str.substring(1));
}

But when a method call is put as a condition of an if statement I don't understand how it works ? What is being checked ? How is it decided if the method call is true and can then return true to stop the recursion ? How are possibilities 'saved' to then backtrack if it is not the right one ? How does the base case work by returning a compare ?

/* Given an array of ints, is it possible to choose a group of some of the 
 * ints, such that the group sums to the given target? This is a classic 
 * backtracking recursion problem. Rather than looking at the whole array, 
 * our convention is to consider the part of the array starting at index 
 * start and continuing to the end of the array. The caller can specify the 
 * whole array simply by passing start as 0. No loops are needed -- the 
 * recursive calls progress down the array.
 */
public boolean groupSum(int start, int[] nums, int target) {
    if(start >= nums.length)
        return target == 0;

    if(groupSum(start+1, nums, target - nums[start]))
        return true;

    if(groupSum(start+1, nums, target))
        return true;

    return false;
}

Both codes were taken from this github.

Thanks!

  • 2
    are you asking us to explain if statements, or return values? – Stultuske Jan 23 '19 at 09:11
  • In the case of `if(groupSum(start+1, nums, target))` the return value of your groupSum method call is checked which is a boolean value itself. – OH GOD SPIDERS Jan 23 '19 at 09:13
  • str.charAt(0) is also a method call. The difference to groupSum is that groupSum returns a boolean that can be used directly in an if-statement. The character returned from charAt(0) must first be compared to another character in order to produce a boolean value. – Torben Jan 23 '19 at 09:14
  • @Stultuske My understanding of if statement : if(num==0){ ... } if the integer variable value is indeed 0, the condition is true so what is inside the condition will be executed. Concerning the return statements, to me they must return the same type as the method type, eg : a boolean method must return a boolean. A return statement stops the method or in the case of recursion it can call again the method, if the method is of the same type. Where my understanding stops is when a method is put as a condition, how it works, and, as we see in the base case, when a compare is returned. thanks ! – Emma Vande Wouwer Jan 23 '19 at 09:18
  • @OHGODSPIDERS and how is the method considered true or false ? The only condition I see testing the true/false value of the method is when the "start" variable is bigger or equal to the array length ? wouldn't that be when all solutions have already been tried ? – Emma Vande Wouwer Jan 23 '19 at 09:27
  • @EmmaVandeWouwer there is no such thing as a method type, only the return type. the returntype of that method is boolean, so what it returns if you call it is a boolean, either true or false. so: if(callBooleanMethod()) will execute depending on the result of callBooleanMethod() So: if ( callBooleanMethod()) is the same as: boolean b = callBooleanMethod(); if (b) – Stultuske Jan 23 '19 at 09:29
  • @EmmaVandeWouwer: The methods return type is boolean, so it will always return true or false. The condition inside an if statement is and always must evaluate to a boolean value. So in the first case `num==0` is evaluated to either `true` or `false` and the if statement block executed according to that evaluation. In the second case your `groupSum` method is executed and returns a boolean, so either `true` or `false` and execution of the if statement block happens according to that return value. – OH GOD SPIDERS Jan 23 '19 at 09:31
  • @Torben Yea okay I understand now it can be done because it is a boolean method but I still don't understand how that method is valued as true or false ? That would be when it found the solution and so thats why it returns true and stop the recursion. But I don't understand how this is achieved ? In the first method it seems clear to me, the method is called with each time the next char, a condition checks if it is the one we're looking for, and a condition stops when it went through the entire string. In the second case, I don't understand how the solution is found. – Emma Vande Wouwer Jan 23 '19 at 09:32
  • @EmmaVandeWouwer consider it different instances of that method. the case where it returns true or false, that one stops, but the one that receives it, doesn't – Stultuske Jan 23 '19 at 09:33
  • Thanks a lot for the replies, I think I understand a bit more. – Emma Vande Wouwer Jan 23 '19 at 09:46

0 Answers0