Home Segments Top Top Previous Next

465: Mainline

If you wish, you can use a switch statement to control recursion. In the following, the first two statement sequences are terminated by return statements, rather than by break statements:

public class Demonstrate {
 public static int rabbits (int n) {
  switch (n) {                                          
   case 0: return 1;                                    
   case 1: return 1;                                    
   default: return rabbits(n - 1) + rabbits(n - 2);     
  } 
 } 
}