Download presentation
Presentation is loading. Please wait.
2
1 Introduction to Java Programming Lecture 7 Flow Control : Boolean expressions and the if statement
3
2 Boolean expressions 布林運算式 (Boolean expressions) 由條件及邏輯的運算 所組成 條件運算式 < is less than > is greater than <= is less than or equal to >= is greater than or equal to == is equal to != is not equal to NOTE: == is not the same as = in Java! 條件運算式是二元運算,計算的結果是 true 或 false 的布林值 (boolean)
4
3 條件運算式 Examples: int x = 15; int y = 100; System.out.println(x < y); System.out.println(x <= y); System.out.println(x > y); System.out.println(x >= y); System.out.println(x == y); System.out.println(x != y); boolean z = (x - y < 0);
5
4 邏輯運算式 邏輯運算式 &&logical AND || logical OR ! NOT 邏輯運算式計算一個或數個布林值,計算的結果也是個布 林值 Examples: int x = 15; int y = 100; boolean a = (x > y); boolean b = (x != y); System.out.println(a && b); System.out.println(a || b); System.out.println(!a);
6
5 The if statement Syntax: if ( boolean_expression ) statement ; if ( boolean_expression ) { statement_list... } 如果 boolean_expression( 布林運算式 ) 的計算結果是 true , statement 就會被執行,否則就跳過、不執行
7
6 The if statement Example: int age = 20; if ( age > 18 ) System.out.println(" 超過 18 歲,請買全票 "); if ( age 12 ) { System.out.println(" 學生 "); System.out.println(" 可以買學生票 "); }
8
7 The if-else statement Syntax: if ( boolean_expression ) statement1 ; else statement2 ; 或 if ( boolean_expression ) { statement_list1 } else { statement_list2 } 如果 boolean_expression( 布林運算式 ) 的計算結果是 true , statement 1 就會被執行,否則就執行 statement 2
9
8 The if-else statement Example: String courseName = “Java Programming”; // 列印前 10 個字元. if (courseName.length() > 10 ) System.out.println(courseName.substring(0,10)); else System.out.println(courseName);
10
9 The if-else statement 下面的程式有什麼邏輯錯誤 if ( age <= 12 ) System.out.println(“ 孩童 "); else System.out.println(“ 不是孩童 "); System.out.println(“ 必須買全票 ");
11
10 The if - else if - else statement if ( boolean_expression1 ) { statement1 } else if ( boolean_expression2 ) { statement2 } else{ statement3 } 如果 boolean_expression1 的計算結果是 true , statement1 就會被執行,否則就檢查 boolean_expression2 ,結果是 true 就執行 statement2 , 否則就執行 statement 3 。 只有一個 statement 會被執行。
12
11 Java Provides a shortcut if/else operator: The conditional expression This is Java’s only ternary operator (i.e. it takes three operands) ( 唯一的三元運算子,可用來代替 if-else) y = ( x > 0 ) ? 1 : -1 The conditional Statement. True Option False Option ? short- cut operator colon
13
12 Conditional expression y = ( x > 0 ) ? 1 : -1 is equivalent to if (x > 0) y = 1; else y = -1; The syntax of conditional expression: (boolean_expression) ? exp1 : exp2
14
13 Conditional Operator if (num % 2 == 0) System.out.println(num + “is even”); else System.out.println(num + “is odd”); is equivalent to System.out.println( (num % 2 == 0)? num + “is even” : num + “is odd”);
15
14 switch statement 如果所要執行的指令是根據數個 int 或 char 的變數來定, 我們可以使用 switch 來代替一連串的 if - else if … else. Example: switch (year) { case 7 : annualInterestRate = 7.25; break; case 15 : annualInterestRate = 8.50; break; case 30 : annualInterestRate = 9.0; break; default : System.out.println( "Wrong number of years, enter 7, 15, or 30"); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.