งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-61 Java Programming Language.

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-61 Java Programming Language."— ใบสำเนางานนำเสนอ:

1 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-61 Java Programming Language Chapter 3 : ประโยคควบคุม (Control Statement)

2 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 2 The boolean Type and Operators การเปรียบเทียบระหว่างค่า 2 ค่า, อาทิเช่น i มากกว่า j หรือไม่. ในภาษาจาวามี Operator ที่ใช้ใน การเปรียบเทียบ ซึ่งผลลัพธ์จากการเปรียบเทียบจะ มีค่าเป็น Boolean value ซึ่งจะมีค่าเพียง true หรือ false. ตัวอย่างเช่น boolean b = (1 > 2); //b=false

3 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 3 เครื่องหมายที่ใช้ในการ เปรียบเทียบ Operator Name < น้อยกว่า (less than) <= น้อยกว่าหรือเท่ากับ (less than or equal to) > มากกว่า (greater than) >= มากกว่าหรือเท่ากับ (reater than or equal to) == เท่ากับ (equal to) != ไม่เท่ากับ ( not equal to)

4 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 4 Boolean Operators Operator Name ! ไม่ (not) && และ (and) || หรือ (or) ^ exclusive or

5 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 5 Truth Table for Operator !

6 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 6 Truth Table for Operator &&

7 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 7 Truth Table for Operator ||

8 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 8 Truth Table for Operator ^

9 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 9 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)

10 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 10 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++)

11 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 11 Selection Statements  if Statements  switch Statements F Conditional Operators

12 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 12 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); }

13 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 13 Note กรณีที่ในประโยค if มีคำสั่งเพียงบรรทัดเดียว เราจะใส่ { } หลังประโยค if หรือไม่ใส่ก็ได้

14 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 14 Caution ห้ามใส่เครื่องหมาย ; ด้านหลังประโยด if statement. if (radius >= 0); { area = radius*radius*PI; System.out.println("The area for the circle of radius " + radius + " is " + area); } บรรทัดนี้ผิด

15 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 15 The if...else Statement if (booleanExpression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; }

16 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 16 if...else Example if (radius >= 0) { area = radius * radius * 3.14159; System.out.println("The area is " + area); } else { System.out.println("Negative input"); }

17 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 17 Multiple Alternative if Statements

18 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 18 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

19 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 19 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

20 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 20 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

21 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 21 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

22 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 22 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


ดาวน์โหลด ppt Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-61 Java Programming Language.

งานนำเสนอที่คล้ายกัน


Ads by Google