循环递归的区别?

循环:

重复执行一段代码,递归,遍历,迭代都属于循环。

代码举例:

1  for(int x = 10; x < 20; x = x+1) {
2          System.out.print("value of x : " + x );
3          System.out.print("\n");
4       }

递归:

重复调用自身的,如下例子不断调用自身方法。

代码举例:

1    private static int fab(int index) {  
2         if (index == 1 || index == 2) {  
3             return 1;  
4         } else {  
5             return fab(index - 1) + fab(index - 2);  
6         }  
7     }

 

posted @ 2017-02-09 21:09  西北野狼  阅读(177)  评论(0编辑  收藏  举报