Outline Control Flow Statements Looping statement for statements

  • Slides: 39
Download presentation

Outline Control Flow Statements Looping statement • for statements • while/do-while statements Branching statement

Outline Control Flow Statements Looping statement • for statements • while/do-while statements Branching statement • break, continue, label: and return Your Turn 方法(Methods) 參數 傳值 傳址 傳回值 Your Turn (Arguments) (Pass-by-Value) (Pass-by-Reference) (return)

Control Flow Statements

Control Flow Statements

Control Flow Statements – if-else 較複雜的情況,我們會 使用巢狀 if-else 敘述 if (expression) { BODY 1

Control Flow Statements – if-else 較複雜的情況,我們會 使用巢狀 if-else 敘述 if (expression) { BODY 1 if(expression) { BODY 1 a } else { BODY 1 b } else { BODY 2 }

Decision; Condition if (a>b) { max = a; min = b; } else {

Decision; Condition if (a>b) { max = a; min = b; } else { min = a; max = b; } if (c>max) { max = c; } if (c<min) { min = c; }

? : 運算子 public class Compare { public static void main (String[] args) {

? : 運算子 public class Compare { public static void main (String[] args) { int x = Integer. parse. Int(args[0]); int y = Integer. parse. Int(args[1]); int z = Integer. parse. Int(args[2]); int max, min; max =x > y ? x : y; min =x < y ? x : y; max =z > max ? z : max; min =z < max ? z : min; System. out. println("Max: "+max+", Min: "+min); } }

Control Flow Statements – switch-case switch statement expression的數值,將會被拿來 比對下面每一個case的value 程式將會跳至第一個比對成功( 數值相等)的地方繼續執行,假 如遇到break; 敘述,就結束整個 switch敘述

Control Flow Statements – switch-case switch statement expression的數值,將會被拿來 比對下面每一個case的value 程式將會跳至第一個比對成功( 數值相等)的地方繼續執行,假 如遇到break; 敘述,就結束整個 switch敘述 否則將會一直往下比對。沒有比 對成功的話,則執行default: 之後 的程式段 • 沒遇到break時,會繼續執行下一行 指令敘述 switch(expression) { case value 1: statements… break; case value 2: statements… break; … default: statements… break; }

Control Flow Statements Looping 將一段程式,重複執行數次,我們稱這段重複執行的程 式為迴圈。 Java looping statement 的種類: for while do-while

Control Flow Statements Looping 將一段程式,重複執行數次,我們稱這段重複執行的程 式為迴圈。 Java looping statement 的種類: for while do-while

Control Flow Statements – for statement 語法 for( initialization; termination; increment ) { statements…

Control Flow Statements – for statement 語法 for( initialization; termination; increment ) { statements… } initialization:初始條件 • 在此宣告之變數其有效範圍僅存在於此迴圈內 • 例:for(int x=0; x<10; x++) { … } termination:終止條件,決定何時迴圈結束 increment:遞增,每次迴圈進行後所要做的事

Control Flow Statements – for Example: For. Example. java /** * 在螢幕上印出 1 ~

Control Flow Statements – for Example: For. Example. java /** * 在螢幕上印出 1 ~ 10 */ for (int i=1; i <=10; i++) { System. out. println ("i = " + i); }

Control Flow Statements – for Example: For. Example. java /** * 印出 1~10, 最後在印出最後的

Control Flow Statements – for Example: For. Example. java /** * 印出 1~10, 最後在印出最後的 i 值 */ int i; for (i=1; i <=10; i++) System. out. println ("i = " + i); System. out. println ("After loop, i = " + i);

Control Flow Statements – for Example: For. Example. java /** * 一個迴圈內,可以有多個控制變數 */ int

Control Flow Statements – for Example: For. Example. java /** * 一個迴圈內,可以有多個控制變數 */ int max. X = 10; int max. Y = 15; for (int x = 0, y = 0; (x < max. X) && (y < max. Y); x++, y = x * 2) { System. out. println(" x < 10 : " + x + ", y < 15 : " + y); }

Control Flow Statements – for Example: For. Example. java /** * 巢狀迴圈的使用方法 */ for

Control Flow Statements – for Example: For. Example. java /** * 巢狀迴圈的使用方法 */ for (int i=1; i <=10; i++) { for (int j=1; j <=10; j++) { for (int k=1; k <=10; k++) { System. out. println("i = " + i + " j = " + j + " k = " + k); } // end of for k } // end of for j } // ned of for i

Control Flow Statements – while/do-while Example: While. Example. java public class While. Example {

Control Flow Statements – while/do-while Example: While. Example. java public class While. Example { public static void main(String[] args) { double r = 0; while(r < 0. 99 d) { r = Math. random(); // Math 類別中提供產生亂數的類別方法 System. out. println(r); } // end of while } }

Control Flow Statements – while/do-while Example: Do. While. Example. java /** * 計算所有平方值小於 100

Control Flow Statements – while/do-while Example: Do. While. Example. java /** * 計算所有平方值小於 100 的數字並印出 */ public class Do. While. Example { public static void main (String[] args) { int i=0, j; do { i++; j = i * i; System. out. println ( i + " * " + i + " = " + j); } while (j < 100); } }

Control Flow Statements – branching Exmaple: Break. And. Continue. java for(int i = 0;

Control Flow Statements – branching Exmaple: Break. And. Continue. java for(int i = 0; i < 100; i++) { if(i == 74) break; // Out of for loop if(i % 9 != 0) continue; // Next iteration System. out. println(i); }

Control Flow Statements – branching break with label search: for ( ; i <

Control Flow Statements – branching break with label search: for ( ; i < array. Of. Ints. length; i++) { for (j = 0; j < array. Of. Ints[i]. length; j++) { if (array. Of. Ints[i][j] == searchfor) { found. It = true; break search; } } }

Control Flow Statements – branching continue with label test: for (int i = 0;

Control Flow Statements – branching continue with label test: for (int i = 0; i <= max; i++) { int n = substring. length(); while (n-- != 0) { if (n == 1) continue test; System. out. println(“no move!!!”); } //end of while found. It = true; break test; } // end of for i

方法(Method) 定義 可重複使用的程式碼片段 範例:public static void main (String[] args); 格式 存取範圍 方法特性 public protected

方法(Method) 定義 可重複使用的程式碼片段 範例:public static void main (String[] args); 格式 存取範圍 方法特性 public protected (空白) private static final abstract native synchronized 方法傳回型態 方法名稱 (參數宣告);

方法(Method) 定義 methods public Object push (Object item) 存取等級 method 本身的存取等級 static 宣告本 method

方法(Method) 定義 methods public Object push (Object item) 存取等級 method 本身的存取等級 static 宣告本 method 為 class method abstract 宣告本 method 內不需任何程式碼 final 宣告本 method 不允許被 subclass 覆蓋(override) native 宣告本 method 為其他程式語言所寫成 synchronized 必須由 monitor 來控制執行時機 (參數列) 參數列 throws 異常事件 宣告本 method 丟出的例外(exceptions)

方法(Method) Example: Method. Demo. java static void print. Triangle(int level) { int i, j;

方法(Method) Example: Method. Demo. java static void print. Triangle(int level) { int i, j; for ( i = 1; i <= level; i++) { for ( j = 1; j <= i; j++ ) System. out. print("*"); System. out. println(); } }

參數傳遞(Passing Argument) Example: Pass. Arg. Demo. java static void my. Method 1 (int i)

參數傳遞(Passing Argument) Example: Pass. Arg. Demo. java static void my. Method 1 (int i) { i += 1; System. out. println("In My. Method(): " + i); } public static void main(String[] args) { int i = 10; // 參數以 primitive 的型態傳入 my. Method(i); // 不會改變在 main 中的 i 值 System. out. println("In main(): " + i); }

參數傳遞(Passing Argument) Example: Pass. Arg. Demo. java static void my. Method 2 (int[] i)

參數傳遞(Passing Argument) Example: Pass. Arg. Demo. java static void my. Method 2 (int[] i) { i[0] = i[0] +1; System. out. println("In My. Func(): " + i[0]); } public static void main(String[] args) { int[] i = {10}; my. Method 2(i); System. out. println("In main(): " + i[0]); }

參數傳遞(Passing Argument) Memory i i[0] i 0 x 5678 0 x 1234 10 0

參數傳遞(Passing Argument) Memory i i[0] i 0 x 5678 0 x 1234 10 0 x 5678 0 x 9012 public static void main(String[] args) static void My. Func(int[] i)

從 method 回傳數值(return) Example: Pass. Arg. Demo. java static int my. Method 3 (int

從 method 回傳數值(return) Example: Pass. Arg. Demo. java static int my. Method 3 (int i) { i *= 10; return i; } public static void main(String[] args) { int i = 10; int j = my. Method 3(i); System. out. println("The return value: " + j); }

Your Turn 試寫二個 static method 方法: static void print. Add 2 N(int max) static

Your Turn 試寫二個 static method 方法: static void print. Add 2 N(int max) static int get. Add 2 N(int max) 方別傳入一個參數 max,可以計算從 1 加到 max 的運算 最後再寫一個 main() 來測試 如: 1+2+3+4+5+6+7+8+9+10 = 55 Result: 55