Code Design 1 : works perfectly
public static void main (String[] args)
{
recursion(2);
}
public static void recursion(int num)
{
if (num > 0)
{
recursion( num - 1 );
System.out.println(num);
}
}
Code Design 2 : Infinite Loop. ?
public static void main (String[] args)
{
recursion(2);
}
public static void recursion(int num)
{
if (num == 0) return;
while (num > 0)
{
recursion( num - 1 );
System.out.println(num);
}
}
- Can someone plz help me in understanding why 2nd design is getting
into infinite loop? - I have already put return in 2nd design . So it should have worked
fine. Also can you plz give me explanation in detail?
1. First of all
ifis Not a Loop, we only haveforloop,for-each,whileloop anddo-whileloop.2. The reason for the 2nd code to go in for a Infinite loop is that, you are never decrementing the value of
num.Do this….