For and While loop choice.

 1 /*
 2     Difference between 'for' and 'while'.
 3     We can transform everything between 'for' and 'while'.
 4     if the incremnet is just use for increase,we recommended 'for';
 5     but if we want to preserved the increment and use it after the loop,'while' is recommented.
 6 */
 7 import kju.print.Print;
 8 class ForWhile 
 9 {
10     public static void main(String[] args) 
11     {
12         /*
13         'i' is created after 'for',so it was release after the close brace.
14         */
15         for(int i = 0; i < 3; i++) {
16             Print.println("i = " + i);
17         }
18         /*
19         i = 0
20         i = 1
21         i = 2
22         */
23         //Print.println("after the loop, i = " + i);    //compile error,i does not exist.
24         
25         Print.println("-----------");
26         /*
27         'j' is created before 'while',so it was preserved after the close brace.
28         */
29         int j = 0;
30         while(j < 3){
31             Print.println("j = " + j);
32             j++;
33         }
34         /*
35         j = 0
36         j = 1
37         j = 2
38         */
39         Print.println("after the loop, j = " + j);
40         //after the loop, j = 3
41     }
42 }

 

posted @ 2014-03-04 02:33  wonkju  阅读(186)  评论(0编辑  收藏  举报