Java break & return

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.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末轧简,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌棋返,老刑警劉巖内边,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件挠他,死亡現(xiàn)場離奇詭異蔬墩,居然都是意外死亡敢茁,警方通過查閱死者的電腦和手機(jī)佑淀,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來彰檬,“玉大人伸刃,你說我怎么就攤上這事》瓯叮” “怎么了捧颅?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長瓶堕。 經(jīng)常有香客問我隘道,道長,這世上最難降的妖魔是什么郎笆? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任谭梗,我火速辦了婚禮,結(jié)果婚禮上宛蚓,老公的妹妹穿的比我還像新娘激捏。我一直安慰自己,他們只是感情好凄吏,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布远舅。 她就那樣靜靜地躺著,像睡著了一般痕钢。 火紅的嫁衣襯著肌膚如雪图柏。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天任连,我揣著相機(jī)與錄音蚤吹,去河邊找鬼。 笑死随抠,一個(gè)胖子當(dāng)著我的面吹牛裁着,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播拱她,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼背蟆,長吁一口氣:“原來是場噩夢啊……” “哼擎椰!你這毒婦竟也來了娇掏?” 一聲冷哼從身側(cè)響起辕狰,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤矿酵,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后背犯,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體坏瘩,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡盅抚,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年漠魏,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片妄均。...
    茶點(diǎn)故事閱讀 39,834評論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡柱锹,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出丰包,到底是詐尸還是另有隱情禁熏,我是刑警寧澤,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布邑彪,位于F島的核電站瞧毙,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏寄症。R本人自食惡果不足惜宙彪,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望有巧。 院中可真熱鬧释漆,春花似錦、人聲如沸篮迎。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽甜橱。三九已至逊笆,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間岂傲,已是汗流浹背难裆。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留譬胎,地道東北人差牛。 一個(gè)月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像堰乔,于是被迫代替她去往敵國和親偏化。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評論 2 354