return、break、continue区别以及作用范围

  1 package com.hangao.basic;
  2 
  3 /**
  4  * @author hangao hangao1204@hotmail.com
  5  * 
  6  * continue 中断当前循环的当前次,继续当前循环的下一次(如果有),继续执行后面的代码
  7  *
  8  * break 中断当前循环的所有次,不再继续当前循环,继续外层嵌套循环的下一次(如果有),继续执行后面的代码
  9  *
 10  * return 中断所有循环,且不再执行后面的代码
 11  *
 12  * 威力 return > break > continue
 13  */
 14 public class TestBreakContinueReturn {
 15     public static void main(String[] args) {
 16         testReturn1();
 17         testReturn2();
 18         testContinue1();
 19         testContinue2();
 20         testBreak1();
 21         testBreak2();
 22     }
 23 
 24     /**
 25      * 运行结果:
 26      * i value is 0
 27      */
 28     private static void testReturn1() {
 29         for (int i = 0; i < 3; i++) {
 30             if (i == 1){
 31                 return;//中断所有循环
 32             }
 33             System.out.println("i value is " + i);
 34         }
 35         System.out.println("TestBreakContinueReturn.testReturn1()");
 36     }
 37     
 38     /**
 39      * 运行结果:
 40      * i value is 0
 41      */
 42     private static void testReturn2() {
 43         for (int j = 0; j < 3; j++) {
 44             for (int i = 0; i < 3; i++) {
 45                 if (i == 1){
 46                     return;//中断所有循环
 47                 }
 48                 System.out.println("i value is " + i);
 49             }
 50         }
 51         System.out.println("TestBreakContinueReturn.testReturn2()");
 52     }
 53     
 54     /**
 55      * 运行结果:
 56      * i value is 0
 57      * i value is 2
 58      * TestBreakContinueReturn.testContinue1()
 59      */
 60     private static void testContinue1() {
 61         for (int i = 0; i < 3; i++) {
 62             if (i == 1){
 63                 continue;//中断当前循环
 64             }
 65             System.out.println("i value is " + i);
 66         }
 67         System.out.println("TestBreakContinueReturn.testContinue1()");
 68     }
 69     
 70     /**
 71      * 运行结果:
 72      * i value is 0
 73      * i value is 2
 74      * i value is 0
 75      * i value is 2
 76      * i value is 0
 77      * i value is 2
 78      * TestBreakContinueReturn.testContinue2()
 79      */
 80     private static void testContinue2() {
 81         for (int j = 0; j < 3; j++) {
 82             for (int i = 0; i < 3; i++) {
 83                 if (i == 1){
 84                     continue;//中断当前循环
 85                 }
 86                 System.out.println("i value is " + i);
 87             }
 88         }
 89         System.out.println("TestBreakContinueReturn.testContinue2()");
 90     }
 91 
 92     /**
 93      * 运行结果:
 94      * i value is 0
 95      * TestBreakContinueReturn.testBreak1()
 96      */
 97     private static void testBreak1() {
 98         for (int i = 0; i < 3; i++) {
 99             if (i == 1){
100                 break;//中断整个循环
101             }
102             System.out.println("i value is " + i);
103         }
104         System.out.println("TestBreakContinueReturn.testBreak1()");
105     }
106     
107     /**
108      * 运行结果:
109      * i value is 0
110      * i value is 0
111      * i value is 0
112      * TestBreakContinueReturn.testBreak2()
113      */
114     private static void testBreak2() {
115         for (int j = 0; j < 3; j++) {
116             for (int i = 0; i < 3; i++) {
117                 if (i == 1){
118                     break;//中断当前整个循环
119                 }
120                 System.out.println("i value is " + i);
121             }
122         }
123         System.out.println("TestBreakContinueReturn.testBreak2()");
124     }
125 }

 

posted @ 2016-11-08 10:41  jackkke  阅读(3016)  评论(0编辑  收藏  举报