MellyPie
01-23-2005, 05:04 PM
Hey y'all!
I'm a new un! Just learnin Java..
Anyway hope you guys can help....I've got some Java code
import java.io.*;
public class test{
public static void dude (int y)
{
if( y==0)
{
System.out.println("y="+y +" :Statement inside the if");
y+=1;
dude(y);
}
if(y==1)
{
System.out.println("y="+y+" :Statement inside the if");
y+=1;
dude(y);
}
System.out.println("y="+y+" Its caught the statement outside the if");
}
public static void main (String [] args)
{ int y=0;
dude(y);
}
}
Which compiles fine but produces unexpected results!
This is what it produces:
0 :Statement inside the if
1 :Statement inside the if
2 Its caught the statement outside the if
2 Its caught the statement outside the if
1 :Statement inside the if
2 Its caught the statement outside the if
2 Its caught the statement outside the if
I really dont understand why it is doing this as I would have expected an output of:
0 :Statement inside the if
1 :Statement inside the if
2 Its caught the statement outside the if
2 Its caught the statement outside the if
ie the first 4 lines! Why does it suddenly do y=1 again and so shove it thru the if statement again...and where are the last two "2 Its caught..." statements coming from?
Thanks for the help :)
I'm a new un! Just learnin Java..
Anyway hope you guys can help....I've got some Java code
import java.io.*;
public class test{
public static void dude (int y)
{
if( y==0)
{
System.out.println("y="+y +" :Statement inside the if");
y+=1;
dude(y);
}
if(y==1)
{
System.out.println("y="+y+" :Statement inside the if");
y+=1;
dude(y);
}
System.out.println("y="+y+" Its caught the statement outside the if");
}
public static void main (String [] args)
{ int y=0;
dude(y);
}
}
Which compiles fine but produces unexpected results!
This is what it produces:
0 :Statement inside the if
1 :Statement inside the if
2 Its caught the statement outside the if
2 Its caught the statement outside the if
1 :Statement inside the if
2 Its caught the statement outside the if
2 Its caught the statement outside the if
I really dont understand why it is doing this as I would have expected an output of:
0 :Statement inside the if
1 :Statement inside the if
2 Its caught the statement outside the if
2 Its caught the statement outside the if
ie the first 4 lines! Why does it suddenly do y=1 again and so shove it thru the if statement again...and where are the last two "2 Its caught..." statements coming from?
Thanks for the help :)