title: Method calling itself.. recursive?
body: <pre><code>public static int m(int i, int j)    
  {     
    if ( i &gt; j)    
      return 0;    
    else    
    {
      i++;    
      m(i++, j);
    }
    return i;    
  }
  </code></pre>
  
  <p>I had two questions. 1.) What is returned by <code>out.print(m(3,8));</code> and 2.) How many times is the method m called? The answers should be 5 and 7 respectively.      </p>
  
  <p>When I worked out question 1, I came out with 5 but the way I did it was incorrect because the method was not called 7 times, it was only called twice. The way I did this was I went straight to the else statement since <code>(i &gt; j)</code> was false at the start and method m was called again this time with <code>(4, 8)</code> I figured it was still false so I went back to the line where m is called and the variable i changed to 5 because of the <code>i++</code> in <code>m(i++, j)</code>. After that it would return 5 for the Value of i.    </p>
  
  <p>That was obviously wrong so I added some out.prints for the values of i throughout the program to see how the value was changing and it went from 3 to 9 with an <code>out.print(i);</code> at the beginning of method <code>m</code>. An <code>out.print(i);</code> right before <code>return i;</code> showed the values started to go backwards from 10 to 5 and the method was called 7 times. How does this work?</p>
  
  <p>EDIT: After logging it, I was able to come up with some logic to it but I would like someone to clarify that it is correct.</p>
  
  <p>method m is called with 3,8 at the start. After, it calls itself with 4,8 then 5,8....until 9,8 where the if statement becomes true and the method returns 0. It called itself 6 times so it starts go backwards or decrement-ing 6 times so since m(i++, j) is post(the i) then i becomes 10 and 10 is returned, then 9, then 8, then 7, 6, and finally 5. When it returned 10 that was 1, 9 was 2, 8 was 3, 7 was 4, 6 was 5, and 5 was 6. So since it was 6 when i = 5, that was the value that was returned. Is this correct? And if it is, a more in depth explanation would be nice to have.</p>