public int mystery(int b)
{
    if (b == 0)                    // checks if 5 == 0, which is false
        return 0;
    
    if (b % 2 == 0)                // checks if 5 is even, which is false
        return mystery(b - 1) + 3; // call 1
    else
        return mystery(b - 1) + 2; // call 2
                                   // stops at call 2 and calls m(4)
}

Call stack

m(4)
m(5)2

Explanation

The comments in the code above show the trace through m(5).

The statement if (b % 2 == 0) when b is 5 could be traced as: checks if 5 % 2, which is 1, is equal to 0, which is false. Since the statement is obviously checking if b is even, it could also be traced as: checks if 5 is even, which is false.

The 2 next to m(5) in the call stack indicates that m(5) stopped at call 2. This will become important when control is later returned to m(5).

The new call to m(4) is added to the top of the call stack. m(5) is suspended waiting for m(4) to return.

The + 2 is ignored because it has not yet been executed.

In the statement return mystery(b - 1) + 2;, return is the last command that executes. Everything to the right of return executes first. The return command in m(5) has not yet executed.

Forward to Step 3
Back to Step 1
Back to main example