Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Java Programming Language Chapter 3 : ประโยคควบคุม (Control Statement)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved The boolean Type and Operators การเปรียบเทียบระหว่างค่า 2 ค่า, อาทิเช่น i มากกว่า j หรือไม่. ในภาษาจาวามี Operator ที่ใช้ใน การเปรียบเทียบ ซึ่งผลลัพธ์จากการเปรียบเทียบจะ มีค่าเป็น Boolean value ซึ่งจะมีค่าเพียง true หรือ false. ตัวอย่างเช่น boolean b = (1 > 2); //b=false
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved เครื่องหมายที่ใช้ในการ เปรียบเทียบ Operator Name < น้อยกว่า (less than) <= น้อยกว่าหรือเท่ากับ (less than or equal to) > มากกว่า (greater than) >= มากกว่าหรือเท่ากับ (reater than or equal to) == เท่ากับ (equal to) != ไม่เท่ากับ ( not equal to)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Boolean Operators Operator Name ! ไม่ (not) && และ (and) || หรือ (or) ^ exclusive or
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Truth Table for Operator !
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Truth Table for Operator &&
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Truth Table for Operator ||
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Truth Table for Operator ^
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved The & and | Operators && : conditional AND operator & : unconditional AND operator || : conditional OR operator | : unconditional OR operator exp1 && exp2 (1 < x) && (x < 100) (1 < x) & (x < 100)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved The & and | Operators ถ้า x = 1, what is x after this expression? (x > 1) & (x++ < 10) ถ้า x = 1, what is x after this expression? (1 > x) && ( 1 > x++)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Selection Statements if Statements switch Statements F Conditional Operators
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Simple if Statements if (booleanExpression) { statement(s); } if (radius >= 0) { area = radius * radius * PI; System.out.println("The area" + " for the circle of radius " + radius + " is " + area); }
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Note กรณีที่ในประโยค if มีคำสั่งเพียงบรรทัดเดียว เราจะใส่ { } หลังประโยค if หรือไม่ใส่ก็ได้
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Caution ห้ามใส่เครื่องหมาย ; ด้านหลังประโยด if statement. if (radius >= 0); { area = radius*radius*PI; System.out.println("The area for the circle of radius " + radius + " is " + area); } บรรทัดนี้ผิด
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved The if...else Statement if (booleanExpression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; }
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved if...else Example if (radius >= 0) { area = radius * radius * ; System.out.println("The area is " + area); } else { System.out.println("Negative input"); }
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Multiple Alternative if Statements
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Trace if-else statement if (score >= 90.0) grade = 'A'; else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F'; Suppose score is 70.0The condition is false animation
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Trace if-else statement if (score >= 90.0) grade = 'A'; else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F'; Suppose score is 70.0The condition is false animation
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Trace if-else statement if (score >= 90.0) grade = 'A'; else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F'; Suppose score is 70.0The condition is true animation
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Trace if-else statement if (score >= 90.0) grade = 'A'; else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F'; Suppose score is 70.0grade is C animation
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Trace if-else statement if (score >= 90.0) grade = 'A'; else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F'; Suppose score is 70.0Exit the if statement animation