-1

I have this code in Java, and I'm trying to figure out how it works, it stacks up until the |a[i] % 4 == 0| returns false. then it stops recursion and starts to go back up to i=0. and then prints false. Which is correct since 13 doesn't divide by 4. but I can't figure out how it works, and what it does in Java.

If someone can clarify that, I would be most grateful!

public class whatRec {
public static boolean what(int[] a) {
    return what(a, 0);
}

private static boolean what(int[] a, int i) {
    if (i == a.length - 1)
        return a[i] % 4 == 0;
    return (a[i] % 4 == 0) && what(a, i + 1);
}


public static void main(String[] args) {
    int[] a = {4, 24, 13, 12, 4, 12};
    System.out.println(what(a));
}
Matias
  • 7
  • 2
  • 5
    Hint: every time you dont understand what code is doing, you can: A) simply add print statements, for example on that recursive method B) learn how to use a debugger C) take a piece of paper and a pen, and "run" the code yourself. Try all these things, and only when you are truly stuck consider asking others to explain things to you. – GhostCat May 04 '21 at 12:09
  • And unrelated: allows follow java naming conventions. Class names should go UpperCase, always ;-) – GhostCat May 04 '21 at 12:10
  • What can't you figure out about how it works? – tgdavies May 04 '21 at 12:11
  • I tried using the debugger, and I also tried running it on papper, but I'm not sure what I'm missing. THAT is why i'm asking here as last. – Matias May 04 '21 at 12:12
  • @tgdavies what I can't understand is, what it does when it get's to the return (a[i] % 4 == 0) && what(a, i + 1); why does it stop counting until the end of the array, and it returns false. Shouldn't it keep running and then come back when i equals a.length-1? and print that answer? – Matias May 04 '21 at 12:13
  • What specifically do you not understand about how that expression is evaluated? – tgdavies May 04 '21 at 12:15
  • `return a[i] % 4 == 0 && a[i + 1]% 4 == 0 && a[i + 2] % 4 == 0 && ... && a[a.length - 1] % 4 == 0;` by filling in `what`. – Joop Eggen May 04 '21 at 12:16
  • 2
    It evaluates `(a[i] % 4 == 0)`, if it's false, it will return false (as `&&` is a [short-circuiting operator](https://stackoverflow.com/q/8759868/2541560)). If it's true, it will call `what(a, i + 1);` to get a boolean for evaluating the result (i.e. true && true, or true && false). – Kayaman May 04 '21 at 12:17
  • @tgdavies it gets to the return point when it gets false && true, so the outcome is false correct? so what happens next? it doesnt even check what(a, i+1)? it just returns false, and what about the other things in the stack? – Matias May 04 '21 at 12:33
  • If an expression is `false && f()` then `f()` is not evaluated. – tgdavies May 04 '21 at 12:40
  • @tgdavies ok, that clarifies one thing that I didn't quite get. now for the recursion part. it goes from i=0 until i=2 in this case right? but it returns the value of i=2, what about i=1 and i=0? – Matias May 04 '21 at 12:42
  • @Matias try expanding the expression, so `4 % 4 == 0 && (24 % 4 == 0 && (13 % 4 == 0 && ...))` (we don't expand further because `13 % 4 == 0` is false) and you'll see how the return values for all the values of `i` are incorporated into the 'top level' return value. – tgdavies May 04 '21 at 23:08

1 Answers1

0

Looks like this recursive function will return true only if all the elements in the array are divisible by 4.

  • Yes, I understand the outcome, what I don't understand is the process. How does the code stop at "13" and doesn't go on? is it because of the "false" return that just makes it stop and return false? I can't get my head around it, as I understand the stack goes from end to start, and executes them in that order. – Matias May 04 '21 at 12:20
  • Its because you are using && instead of &. This is logical and. Since one result is false which is 13%4 ==0, Java knows the entire result should be false therefore returns false without further execution. You can see the difference if you use bitwise and which is & instead of &&. – Ishara Priyadarshana May 04 '21 at 13:50