Java編程思想:第4章 控制執行流程

來源:互聯網
上載者:User

標籤:

4.1 true和false

所有條件陳述式都用真和假來判斷。

4.2 if-else

if(bool-exp) statement;  or  if(bool-exp) statement; else statement;

4.3 迭代

while(b){} do{}while(b)

for(init;b;step){}init裡可以用逗號定義多個初始變數

4.4 Foreach文法

增強for迴圈  for(T t : 集合/數組){}

4.5 Return

4.6 Break和Continue

任何迭代語句主體都可以用break和continue來控制迴圈流程。

可以與標籤一同使用:中斷迴圈直到標籤所在的地方。

注意:Java中使用標籤的唯一理由就是因為有迴圈嵌套存在,並且想從多層嵌套中break或者continue;

4.7 臭名昭著的goto

4.8 switch

根據整數運算式,可以從一系列代碼中選擇一個去執行。

switch(int-exp){

  case int-val1 : st1; break;

  ...

  default: sn;

整數運算式包括   byte、short、char、int以及封裝類和enum

注意: 從1.7開始String也可以,原理是把String轉成hashcode當做int來處理

 

}

 

Java編程思想:第4章 控制執行流程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.