Time to take a piece of paper and pencil and play computer and step through the code statement by statement to see what the computer sees. At each statement write down the values of the variables as they are changed.

February 10th, 2013, 11:12 AM

maple1100

Re: Java recursion

How can I make the computer display what it the value it seeing? I believe the computer are seeing value of x which are 17, 14,11, and 8.

February 10th, 2013, 11:21 AM

Norm

Re: Java recursion

Playing computer: For example if the program had this statement:
x = 24;
You would record a value of 24 for x
If later the program had this statement:
x = x + 3;
you would find where you recorded the value of x and add 3 to it making x's new value 27

Quote:

make the computer display what it the value it seeing

Use the println statement:
System.out.println("x="+x); // display the value of x

February 10th, 2013, 11:39 AM

maple1100

Re: Java recursion

I understand it when it it is replacing the value but what I don't understand is how the program take -5 + 8 without me adding the statement. Does the recursion record the value +2 each time it occur and automatically add it at the end? Thanks

February 10th, 2013, 11:49 AM

Norm

Re: Java recursion

Can you show the output from the println statements that you added that shows what you are talking about?
Where is the -5 and the +8?
Where is the +2?

Do you understand that each time a method is called a new copy of its args variables is created with new values?
When a method calls another method (or itself recursively) the current values of all its variables are saved. Those variables' values will be restored when execution returns from the called method.

February 10th, 2013, 12:54 PM

maple1100

Re: Java recursion

I understand how the program work now with your explanation of what happen when a method call itself. Thanks you so much!