Help Me Please!!!!
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Pankaj_Raverkar
Posted On:   Tuesday, August 27, 2002 11:37 PM

I have following code: class Arr { int a1[]; public Arr(int i) { a1 = new int[i]; } public void display(int i) { if (i==0)return; else display(i-1); System.out.println("["+(i-1)+"]"+a1[i-1]); } } class RecursiveFunda { public static void main(String[] args) { Arr t1 = new Arr(10); for (int j=0;j <10;j++) { t1.a1[j] = j; } t1.display(10); } } In my code the method "display()" gets called recursively. So when value of "i" reaches to "0" as per my knowledge ,it sh   More>>

I have following code:

			
class Arr
{
int a1[];
public Arr(int i)
{
a1 = new int[i];
}

public void display(int i)
{
if (i==0)return;
else display(i-1);
System.out.println("["+(i-1)+"]"+a1[i-1]);
}
}
class RecursiveFunda
{
public static void main(String[] args)
{
Arr t1 = new Arr(10);
for (int j=0;j <10;j++)
{
t1.a1[j] = j;
}
t1.display(10);
}
}



In my code the method "display()" gets called recursively. So when value of "i" reaches to "0" as per my knowledge ,it should return to main() method. I have wrote this prog in C language also and observed it by tracing and also the program stack. So what I observed is after executing return statement it comes to curly brace, that is perfectly fine but after curly brace it should go to main() method but it is not it again enters in to the method "display()" and execute the System.out.println() statement which prints all the values of stack by poping.

I am very much confused that why this is happning. Why it again enters in to the method display() after exiting that method.




Thanks

Pankaj    <<Less

Re: Help Me Please!!!!

Posted By:   Animesh_Srivastava  
Posted On:   Wednesday, August 28, 2002 02:19 AM

>>> So when value of "i" reaches to "0" as per my knowledge ,it should return to main() method...


Not really! Your display() methods checks that if i==0 then "return", nowhere does it says "return to main()". It cant actually say that. So, as it happens in normal course, control will return to the statement which is next to the statement from where this function was called. In this case it is the println() statement (if i is 0).


>>>Why it again enters in to the method display() after exiting that method..


It is NOT entering the display() method again. All it is doing is executing the statements which are just after the call to display(). You can if you wish, check this by putting a println("entered display method with i="+i) as the very first line of the display() method, even before you check i==0.


Here is what is going on in detail. Your main calls display() with i=10. display() checks wether i==0, if not it calls display() with i=9. Again since i is not 0, it will call display() with i=8 and so on. Finally, a display() with i==0 will be called, from display(1). Since i is 0, this function returns control to the statement which is just after the display(i-1) statement. Here you have the println() statement, which would fire for i=1 and hence print a[0] (that is a[i-1]). Then it would meet the ending curly brace of display(1), and return the control back to the statement which was next to display(2). This again is the println() and prints a[1]. This cycle would continue till all calls are finished, and finally the display() method finishes. So the output you are getting (printing of all values from a[0] to a[9]) is infact correct and expected.


Hope that helps.

Animesh.

Re: Help Me Please!!!!

Posted By:   Gautam_Marwaha  
Posted On:   Wednesday, August 28, 2002 12:16 AM

The exit criterion as per your code is i == 0. This condition will be true only at the last call/cycle and NOT for the intermediate ones. So the intermediate calls/cycles will first execute to completion before the first call/cycle executes. One way to prevent intermediate cycles to execute fully (and show the output) is:

class Arr
{
int a1[];
boolean b = false;
public Arr(int i)
{
a1 = new int[i];
}

public void display(int i)
{
if (i==0 || b)
{
System.out.println("if - i = " + i + ", b = " + b);
return;
}
else
{
System.out.println("else - i = " + i + ", b = " + b);
b = true;
display(i-1);
}
System.out.println("["+(i-1)+"]"+a1[i-1]);
}
}
About | Sitemap | Contact