美文网首页Java
Java break & return

Java break & return

作者: JaedenKil | 来源:发表于2018-01-18 14:34 被阅读6次

    Java break:

    public class TestExit {
        public static void main(String[] args) {
            System.out.println("Start Loop.");
            for (int j = 1; j <= 3; j++) {
                System.out.println("This is the outer level of loop(" + j + ").");
                for (int x = 1; x <= 3; x++) {
                    System.out.println("This is the inner level of loop(" + x + ").");
                    if (true) {
                        break;
                    }
                }
            }
            System.out.println("Finish Loop.");
        }
    }
    

    prints:

    Start Loop.
    This is the outer level of loop(1).
    This is the inner level of loop(1).
    This is the outer level of loop(2).
    This is the inner level of loop(1).
    This is the outer level of loop(3).
    This is the inner level of loop(1).
    Finish Loop.
    

    AND

    public class TestExit {
        public static void main(String[] args) {
            System.out.println("Start Loop.");
            for (int j = 1; j <= 3; j++) {
                System.out.println("This is the outer level of loop(" + j + ").");
                for (int x = 1; x <= 3; x++) {
                    System.out.println("This is the inner level of loop(" + x + ").");
                    while (true) {
                        break;
                    }
                }
            }
            System.out.println("Finish Loop.");
        }
    }
    

    prints:

    Start Loop.
    This is the outer level of loop(1).
    This is the inner level of loop(1).
    This is the inner level of loop(2).
    This is the inner level of loop(3).
    This is the outer level of loop(2).
    This is the inner level of loop(1).
    This is the inner level of loop(2).
    This is the inner level of loop(3).
    This is the outer level of loop(3).
    This is the inner level of loop(1).
    This is the inner level of loop(2).
    This is the inner level of loop(3).
    Finish Loop.
    

    So we may use

    if(some condition) {
      break;
    }
    

    but NEVER

    while(some condition) {
      break;
    }
    

    So when use break to break loop(while loop, for loop, switch loop), it breaks out the nearest loop.

    Meanwhile

    public class TestExit {
        public static void main(String[] args) {
            System.out.println("Start Loop.");
            for (int j = 1; j <= 3; j++) {
                System.out.println("This is the outer level of loop(" + j + ").");
                for (int x = 1; x <= 3; x++) {
                    System.out.println("This is the inner level of loop(" + x + ").");
                    if (true) {
                        return;
                    }
                }
            }
            System.out.println("Finish Loop.");
        }
    }
    

    prints:

    Start Loop.
    This is the outer level of loop(1).
    This is the inner level of loop(1).
    

    And

    public class TestExit {
        public static void main(String[] args) {
            System.out.println("Start Loop.");
            for (int j = 1; j <= 3; j++) {
                System.out.println("This is the outer level of loop(" + j + ").");
                for (int x = 1; x <= 3; x++) {
                    System.out.println("This is the inner level of loop(" + x + ").");
                    while (true) {
                        return;
                    }
                }
            }
            System.out.println("Finish Loop.");
        }
    }
    

    prints:

    Start Loop.
    This is the outer level of loop(1).
    This is the inner level of loop(1).
    

    And

    import android.support.test.runner.AndroidJUnit4;
    import android.util.Log;
    
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    
    @RunWith(AndroidJUnit4.class)
    public class TestExit {
    
        private static final String TAG = TestExit.class.getSimpleName();
    
        @Before
        public void setUp() throws Exception {
            Log.v(TAG, "This is setup.");
        }
    
        @After
        public void tearDown() throws Exception {
            Log.v(TAG, "This is teardown.");
        }
    
        @Test
        public void exitTest() throws Exception {
            Log.v(TAG, "Start the loop.");
            for (int i = 1; i <= 3; i++) {
                Log.v(TAG, "This is the outer loop(" + i + ").");
                for (int j = 1; j <= 3; j++) {
                    Log.v(TAG, "This is the inner loop(" + j + ").");
                    while (true) {
                        return;
                    }
                }
            }
            Log.v(TAG, "Finish the loop.");
        }
    }
    

    prints:

    1800  1819 V TestExit: This is setup.
    1800  1819 V TestExit: Start the loop.
    1800  1819 V TestExit: This is the outer loop(1).
    1800  1819 V TestExit: This is the inner loop(1).
    1800  1819 V TestExit: This is teardown.
    

    So return breaks the entire method.

    In general, break breaks loop, return breaks method.

    相关文章

      网友评论

        本文标题:Java break & return

        本文链接:https://www.haomeiwen.com/subject/sihwoxtx.html