三大流程控制語句:順序、選擇、循環。java
選擇結構:express
循環結構:數組
一個if語句包含一個布爾表達式和一條或多條執行語句;code
布爾表達式值爲true,執行if 語句;ip
格式:作用域
if(布爾表達式) { //布爾表達式值爲true,執行語句; }
布爾表達式值爲true,執行 if 語句;it
布爾表達式值爲false,執行 else 語句;io
格式:for循環
if(布爾表達式) { //布爾表達式值爲true,執行語句; } else { //布爾表達式值爲false,執行語句; }
格式:class
if(布爾表達式1) { //布爾表達式1值爲true,執行語句; } else if(布爾表達式2) { //布爾表達式2值爲true,執行語句; } else if(布爾表達式3) { //布爾表達式值3爲true,執行語句; } else { //若是以上全部表達式的值都爲false,則執行語句; }
格式:
if(布爾表達式1) { //布爾表達式1值爲true,執行語句 if(布爾表達式2) { //布爾表達式2值爲true,執行語句 } }
格式:
switch(常量值/expression) { case value1: //執行語句 break; //可選 case value2: //執行語句 break; //可選 ...... default : //執行語句 }
switch語句有以下規則:
if 結構
switch 結構
public class MultiplicationTable { public static void main(String[] args) { for (int i = 1; i <= 9; ++i) { for (int j = 1; j <= 9; j++) { if(j < i) { //輸出的空格由"%d * %d = %2d "決定 System.out.print(" "); } else { System.out.printf("%d * %d = %2d ", i ,j , i*j); } } System.out.println(); } } } Output: 1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 1 * 4 = 4 1 * 5 = 5 1 * 6 = 6 1 * 7 = 7 1 * 8 = 8 1 * 9 = 9 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18 3 * 3 = 9 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27 4 * 4 = 16 4 * 5 = 20 4 * 6 = 24 4 * 7 = 28 4 * 8 = 32 4 * 9 = 36 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 6 * 6 = 36 6 * 7 = 42 6 * 8 = 48 6 * 9 = 54 7 * 7 = 49 7 * 8 = 56 7 * 9 = 63 8 * 8 = 64 8 * 9 = 72 9 * 9 = 81
public class Days { public static void main(String[] args) { int days = 0; Scanner sc = new Scanner(System.in); System.out.println("請輸入要肯定的年份:"); int year = sc.nextInt(); System.out.println("請輸入要肯定的月份:"); int month = sc.nextInt(); switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; break; case 4: case 6: case 9: case 11: days = 30; break; case 2: if((year %400 == 0) || (year % 100 != 0 && year %4 == 0)) { days = 29; break; } else { days = 28; break; } default: System.out.println("您輸入的月份有誤!"); System.exit(0); } System.out.printf("%4d 年 %2d 月 共有 %2d 天\n",year,month,days); } } Output: 請輸入要肯定的年份: 2018 請輸入要肯定的月份: 02 2018 年 2 月 共有 28 天 請輸入要肯定的年份: 2008 請輸入要肯定的月份: 02 2008 年 2 月 共有 29 天
while
循環只要布爾表達式值爲true
,就會執行循環內容。直到布爾表達式值爲false
,退出循環;
while(布爾表達式) { //布爾表達式值爲true,執行循環內容 }
do ... while
循環只要布爾表達式值爲true
,就會執行循環內容。直到布爾表達式值爲false
,退出循環;和while
相似,不一樣的是do...while
語句至少會被執行一次;
do { //布爾表達式值爲true,執行循環內容 }while(布爾表達式)
for
循環for
循環執行次數在執行前肯定。
for(初始化;布爾表達式;更新) { //執行代碼 }
關於for
的幾點說明:
聲明語句:聲明新的局部變量,該變量的類型必須和數組元素的類型匹配。其做用域限定在循環語句塊,其值與此時數組元素的值相等。
表達式:表達式是要訪問的數組名,或者是返回值爲數組的方法。
for(聲明語句:表達式) { //執行代碼 }
public class OutputLetters { public static void main(String[] args) { //循環輸出26個英文小寫字母,要求分兩行輸出 char ch = 'a'; int count = 0;//控制換行 while(ch <= 'z') { System.out.printf(ch + " "); ch ++; count ++; if (count % 13 == 0) { System.out.println(); } } System.out.println(); ch = 'a'; count = 0; for(count = 1, ch = 'a';ch <= 'z';ch ++,count ++) { System.out.printf(ch + " "); if (count % 13 == 0) { System.out.println(); } } } } Output: a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z
break
break
語句
break
語句能夠結束當前循環的執行;break
語句,循環體中位於break
語句後面的語句就不會被執行;break
語句至關於向外跳一層;continue
continue
語句:
continue
語句只能用在循環裏;continue
語句能夠結束當前循環的執行,可是要繼續下一次循環的執行;public class OutputLettersDemo { public static void main(String[] args) { //循環輸出26個英文小寫字母,要求分兩行輸出 //練習break,cotinue char ch = 'a'; int count = 0;//控制換行 while(ch <= 'z') { if(ch == 'x') break; System.out.printf(ch + " "); ch ++; count ++; if (count % 13 == 0) { System.out.println(); } } System.out.println(); ch = 'a'; count = 0; for(count = 1, ch = 'a';ch <= 'z';ch ++,count ++) { if(ch == 'x') continue; System.out.printf(ch + " "); if (count % 13 == 0) { System.out.println(); } } } } Output: a b c d e f g h i j k l m n o p q r s t u v w a b c d e f g h i j k l m n o p q r s t u v w y z
從上面的例子能夠看出,break語句直接退出當層循環(到x直接退出,再也不輸出),而continue語句只是結束當前循環,並無退出(只是沒有輸出x)。