Java中break、continue、return語句的使用區別對比介紹
這篇文章主要介紹了Java中break、continue、return語句的使用區別對比,本文用非常清爽簡明的語言總結了這三個關鍵字的使用技巧,並用一個實例對比使用結果,需要的朋友可以參考下
break、continue、return之間的區別與聯繫
在軟件開發過程中,邏輯清晰是非常之重要的。 代碼的規範也是非常重要的。往往細節決定成敗。在編寫代碼的時候,一定要理解語言的作用以及使用的方法和場景。下面來介紹一下break、continue、return三者的區別和聯繫。
1. break :
(1).結束當前整個循環,執行當前循環下邊的語句。忽略循環體中任何其它語句和循環條件測試。
(2).只能跳出一層循環,如果你的循環是嵌套循環,那麼你需要按照你嵌套的層次,逐步使用break來跳出。[逐層判斷,逐步跳出]
(3)k在循環體內,強行結束循環的執行,也就是結束整個循環過程,不在判斷執行循環的條件是否成立,直接轉向循環語句下面的語句。
(4).當break出現在循環體中的switch語句體內時,其作用只是跳出該switch語句體。
總的來說:就近原則,結束當前的整個循環。
2. return:
(1)rn 從當前的方法中退出,返回到該調用的方法的語句處,繼續執行。
(2)rn 返回一個值給調用該方法的語句,返回值的數據類型必須與方法的聲明中的返回值的`類型一致。
(3). return後面也可以不帶參數,不帶參數就是返回空,其實主要目的就是用於想中斷函數執行,返回調用函數處。
3. continue:
(1).終止本次循環的執行,即跳過當前這次循環中continue語句後尚未執行的語句,接着進行下一次循環條件的判斷。
(2).結束當前循環,進行下一次的循環判斷。
(3).終止當前的循環過程,但他並不跳出循環,而是繼續往下判斷循環條件執行語句.他只能結束循環中的一次過程,但不能終止循環繼續進行.
代碼如下:
複製代碼 代碼如下:
public static void test() { for (int i = 0; i < 10; i++) {if (i == 3) {// break;//// continue;//i=3的時候結束這一次的循環,繼續執行i=4的循環return;// 直接結束整個函 tln("----" + i);}tln("-111--");}
相關文章
-
Java中break、continue、return語句的使用區別對比
在軟件開發過程中,邏輯清晰是非常之重要的。 代碼的規範也是非常重要的。往往細節決定成敗。在編寫代碼的時候,一定要理解語言的作用以及使用的方法和場景。你知道Java中break、continue、retu語句的使用區別是什麼嗎? -
,javascript break和continue區別
有兩種可以用在循環中的聲明:break和continue。JavaScriptbreak和continue聲明 有兩種特殊的聲明可用在循環內部:break和continue。Breakbreak命令可以終止循環的.運行,然後繼續執行循環之後的代碼(如果循環之後有代碼的 -
DOS的Interlnk、Intersvr、Qbasic命令使用說明
姓名:林允年齡:18身高:16cm微博暱稱:林允Jelly所在地:浙江 湖州性別:女生日:1996年4月16日教育信息大學:北京齊魯音樂學院 (2011年)主要成就人:《美人魚》當演女一號周星馳新片《美人魚》中的新任星女郎終於浮出水面了!她的名字叫 -
託福英語寫作範文:Examinations exert a pernicious influence on educat
We might marvel at the progress made in every field of study, but the methods of testing a person’s knowledge and ability remain as primitive as ever they were. It really is extraordinary that after -
高中優秀英語作文An Unforgettable Experience
高中優秀英語作文An Unforgettable ExperienceThis summer holiday, I went to Dalian with my family. We got there by air. Dalian is a very beautiful and mode city. On the bus, we could see all kinds o -
java如何利用java.net.URLConnection發送HTTP請求
如何通過Java發送HTTP請求,通俗點講,如何通過Java(模擬瀏覽器)發送HTTP請求。下面是由本站小編爲大家整理的java如何利用onnection發送HTTP請求,喜歡的可以收藏一下!瞭解更多詳情資訊,請關注應屆畢業生考試網! 運用原生Jav -
英語作文:An unforgettable experience
One moing before class, when I was running into the classroom, I knocked over the ink bottle on the teacher's desk, and the red ink spilt on the desk. I was sure I would be scolded by the teacher. Just -
英語滿分作文:Reading-TheBestMeanstoAcquireknowledge
Reading- The Best Means to Acquire knowledgeKnowledge may be acquired through many ways。 One way of getting knowledge is from travelling Another way is by conversation, especially one with a great man -
Should Private Cars Be Encouraged in China高中英語作文
with the rapid development of economy in china, the number of people who own private cars is rising day after day. quite different views eist as to this e is no doubt that private cars have been playi -
英語閱讀:African Union Leaders Talk Mali, Chinese Investmen
African leaders met in Ethiopia during the weekend to celebrate the 50th anniversary of their continental alliance and discuss issues such as Chinese investment on the continent as well as the fight a