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

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

บทที่ 4 คำสั่งควบคุมแบบมีทางเลือก

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


งานนำเสนอเรื่อง: "บทที่ 4 คำสั่งควบคุมแบบมีทางเลือก"— ใบสำเนางานนำเสนอ:

1 บทที่ 4 คำสั่งควบคุมแบบมีทางเลือก
รายวิชา ง30201 การเขียนโปรแกรมเชิงวัตถุ Reading: ใบความรู้ บทที่ 4

2 คำสั่งควบคุมแบบมีทางเลือก
คำสั่งควบคุม แบบมีทางเลือก คำสั่ง if if if..else Nested if คำสั่ง switch 1 2 Mahidol Wittayanusorn School L. Ngamprasit

3 คำสั่ง if : if if(condition) statement; if(condition) { statements; }
Mahidol Wittayanusorn School L. Ngamprasit

4 คำสั่ง if : if ถ้าป้อนข้อมูลเข้าเป็น 74 ผลลัพธ์ที่ได้คือ ...
import java.util.Scanner; public class ScoreTest { public static void main(String[] args) { Scanner in = new Scanner(System.in); float score = in.nextFloat(); if(score >= 60) System.out.println("Passed"); } ถ้าป้อนข้อมูลเข้าเป็น 74 ผลลัพธ์ที่ได้คือ ... Passed ถ้าป้อนข้อมูลเข้าเป็น 55 ผลลัพธ์ที่ได้คือ ... Mahidol Wittayanusorn School L. Ngamprasit

5 คำสั่ง if : if ถ้าป้อนข้อมูลเข้าเป็น 74 ผลลัพธ์ที่ได้คือ ...
import java.util.Scanner; public class ScoreTest { public static void main(String[] args) { Scanner in = new Scanner(System.in); float score = in.nextFloat(); if(score < 60) { System.out.println("Failed"); System.out.println("You must take this course again."); } ถ้าป้อนข้อมูลเข้าเป็น 74 ผลลัพธ์ที่ได้คือ ... ถ้าป้อนข้อมูลเข้าเป็น 55 ผลลัพธ์ที่ได้คือ ... Failed You must take this course again. Mahidol Wittayanusorn School L. Ngamprasit

6 คำสั่ง if : if..else if(condition) { statements1; } else {
no yes Mahidol Wittayanusorn School L. Ngamprasit

7 คำสั่ง if : if..else ถ้าป้อนข้อมูลเข้าเป็น 74 ผลลัพธ์ที่ได้คือ ...
import java.util.Scanner; public class ScoreTest { public static void main(String[] args) { Scanner in = new Scanner(System.in); float score = in.nextFloat(); if(score >= 60) System.out.println("Passed"); else { System.out.println("Failed"); System.out.println("You must take this course again."); } ถ้าป้อนข้อมูลเข้าเป็น 74 ผลลัพธ์ที่ได้คือ ... Passed ถ้าป้อนข้อมูลเข้าเป็น 55 ผลลัพธ์ที่ได้คือ ... Failed You must take this course again. Mahidol Wittayanusorn School L. Ngamprasit

8 คำสั่ง if : nested if if(condition1) { statements1; }
else if(condition2) { statements2; else { statementsN; no yes no yes no yes Mahidol Wittayanusorn School L. Ngamprasit

9 คำสั่ง if : nested if import java.util.Scanner;
public class GradeTest { public static void main(String[] args) { Scanner in = new Scanner(System.in); float score = in.nextFloat(); char grade; if(score >= 80) grade = 'A'; else if(score >= 70) grade = 'B'; else if(score >= 60) grade = 'C'; else if(score >= 50) grade = 'D'; else grade = 'F'; System.out.println(grade); } Mahidol Wittayanusorn School L. Ngamprasit

10 คำสั่งควบคุมแบบมีทางเลือก
ทบทวนบทที่ 4 เรื่อง if ปฏิบัติการที่ 4 คำสั่ง if เข้าไปที่ webcs.mwit.ac.th การเรียนการสอน Mahidol Wittayanusorn School L. Ngamprasit

11 คำสั่งควบคุมแบบมีทางเลือก
คำสั่งควบคุม แบบมีทางเลือก คำสั่ง if if if..else Nested if คำสั่ง switch 1 2 Mahidol Wittayanusorn School L. Ngamprasit

12 คำสั่ง switch ตรวจสอบค่าของตัวแปรหรือนิพจน์ ถ้าตรงตาม case ใดก็จะทำตามคำสั่งนั้นๆ จนกว่าจะเจอคำสั่ง break กรณีที่ไม่มีค่าใดตรงกับ case ที่ระบุโปรแกรม จะมาทำงานที่คำสั่ง default โดยอัตโนมัติ ต้องมีชนิดข้อมูลเป็น byte, short, char , int, enum และ String เท่านั้น switch (variable/expression) { case constant1 : statements; break; case constant2 : statements; ... case constantN : statements; default : statements; } Mahidol Wittayanusorn School L. Ngamprasit

13 คำสั่ง switch : ตัวอย่าง
ตรวจสอบค่าของตัวแปรหรือนิพจน์ ถ้าตรงตาม case ใดก็จะทำตามคำสั่งนั้นๆ จนกว่าจะเจอคำสั่ง break กรณีที่ไม่มีค่าใดตรงกับ case ที่ระบุโปรแกรม จะมาทำงานที่คำสั่ง default โดยอัตโนมัติ public class SwitchExam { public static void main(String[] args) { int num = 1; switch (num) { case 1: System.out.println("One"); case 2: System.out.println("Two"); case 3: System.out.println("Three"); default: System.out.println("Bye"); } ผลลัพธ์ที่ได้คือ ... One Two Three Bye Mahidol Wittayanusorn School L. Ngamprasit

14 คำสั่ง switch : ตัวอย่าง
ตรวจสอบค่าของตัวแปรหรือนิพจน์ ถ้าตรงตาม case ใดก็จะทำตามคำสั่งนั้นๆ จนกว่าจะเจอคำสั่ง break กรณีที่ไม่มีค่าใดตรงกับ case ที่ระบุโปรแกรม จะมาทำงานที่คำสั่ง default โดยอัตโนมัติ public class SwitchExam { public static void main(String[] args) { int num = 2; switch (num) { case 1: System.out.println("One"); case 2: System.out.println("Two"); case 3: System.out.println("Three"); default: System.out.println("Bye"); } ผลลัพธ์ที่ได้คือ ... Two Three Bye Mahidol Wittayanusorn School L. Ngamprasit

15 คำสั่ง switch : ตัวอย่าง
ตรวจสอบค่าของตัวแปรหรือนิพจน์ ถ้าตรงตาม case ใดก็จะทำตามคำสั่งนั้นๆ จนกว่าจะเจอคำสั่ง break กรณีที่ไม่มีค่าใดตรงกับ case ที่ระบุโปรแกรม จะมาทำงานที่คำสั่ง default โดยอัตโนมัติ public class SwitchExam { public static void main(String[] args) { int num = 3; switch (num) { case 1: System.out.println("One"); case 2: System.out.println("Two"); case 3: System.out.println("Three"); default: System.out.println("Bye"); } ผลลัพธ์ที่ได้คือ ... Three Bye Mahidol Wittayanusorn School L. Ngamprasit

16 คำสั่ง switch : ตัวอย่าง
ตรวจสอบค่าของตัวแปรหรือนิพจน์ ถ้าตรงตาม case ใดก็จะทำตามคำสั่งนั้นๆ จนกว่าจะเจอคำสั่ง break กรณีที่ไม่มีค่าใดตรงกับ case ที่ระบุโปรแกรม จะมาทำงานที่คำสั่ง default โดยอัตโนมัติ public class SwitchExam { public static void main(String[] args) { int num = 4; switch (num) { case 1: System.out.println("One"); case 2: System.out.println("Two"); case 3: System.out.println("Three"); default: System.out.println("Bye"); } ผลลัพธ์ที่ได้คือ ... Bye Mahidol Wittayanusorn School L. Ngamprasit

17 คำสั่ง switch : ตัวอย่าง(ปรับปรุง 1)
ตรวจสอบค่าของตัวแปรหรือนิพจน์ ถ้าตรงตาม case ใดก็จะทำตามคำสั่งนั้นๆ จนกว่าจะเจอคำสั่ง break กรณีที่ไม่มีค่าใดตรงกับ case ที่ระบุโปรแกรม จะมาทำงานที่คำสั่ง default โดยอัตโนมัติ public class SwitchExam { public static void main(String[] args) { int num = 1; switch (num) { case 1: System.out.println("One"); break; case 2: System.out.println("Two"); case 3: System.out.println("Three"); default: System.out.println("Bye"); } ผลลัพธ์ที่ได้คือ ... One Mahidol Wittayanusorn School L. Ngamprasit

18 คำสั่ง switch : ตัวอย่าง(ปรับปรุง 1)
ตรวจสอบค่าของตัวแปรหรือนิพจน์ ถ้าตรงตาม case ใดก็จะทำตามคำสั่งนั้นๆ จนกว่าจะเจอคำสั่ง break กรณีที่ไม่มีค่าใดตรงกับ case ที่ระบุโปรแกรม จะมาทำงานที่คำสั่ง default โดยอัตโนมัติ public class SwitchExam { public static void main(String[] args) { int num = 2; switch (num) { case 1: System.out.println("One"); break; case 2: System.out.println("Two"); case 3: System.out.println("Three"); default: System.out.println("Bye"); } ผลลัพธ์ที่ได้คือ ... Two Mahidol Wittayanusorn School L. Ngamprasit

19 คำสั่ง switch : ตัวอย่าง(ปรับปรุง 2)
ตรวจสอบค่าของตัวแปรหรือนิพจน์ ถ้าตรงตาม case ใดก็จะทำตามคำสั่งนั้นๆ จนกว่าจะเจอคำสั่ง break กรณีที่ไม่มีค่าใดตรงกับ case ที่ระบุโปรแกรม จะมาทำงานที่คำสั่ง default โดยอัตโนมัติ public class SwitchExam { public static void main(String[] args) { int num = 1; switch (num) { case 1: System.out.println("One"); case 2: System.out.println("Two"); break; case 3: System.out.println("Three"); default: System.out.println("Bye"); } ผลลัพธ์ที่ได้คือ ... One Two Mahidol Wittayanusorn School L. Ngamprasit

20 คำสั่ง switch : ตัวอย่าง(ปรับปรุง 3)
ตรวจสอบค่าของตัวแปรหรือนิพจน์ ถ้าตรงตาม case ใดก็จะทำตามคำสั่งนั้นๆ จนกว่าจะเจอคำสั่ง break กรณีที่ไม่มีค่าใดตรงกับ case ที่ระบุโปรแกรม จะมาทำงานที่คำสั่ง default โดยอัตโนมัติ ผลลัพธ์ที่ได้คือ ... public class SwitchExam { public static void main(String[] args) { int num = 1; switch (num) { case 1: case 2: System.out.println("Two"); break; case 3: System.out.println("Three"); default: System.out.println("Bye"); } Two ถ้าให้ num = 2 ผลลัพธ์ที่ได้คือ ... Two ถ้าให้ num = 3 ผลลัพธ์ที่ได้คือ ... Three Mahidol Wittayanusorn School L. Ngamprasit

21 คำสั่ง switch : ตัวอย่าง(ปรับปรุง 4)
เงื่อนไข คือ แสดงข้อความว่า Hello เมื่อ num มีค่าเท่ากับ 1 หรือ 2 แสดงข้อความว่า Bye เมื่อ num มีค่าเท่ากับ 3 หรือ 4 ผลลัพธ์ที่ได้คือ ... public class SwitchExam { public static void main(String[] args) { int num = 1; switch (num) { case 1: case 2: System.out.println("Hello"); break; case 3: case 4: System.out.println("Bye"); } Hello ถ้าให้ num = 2 ผลลัพธ์ที่ได้คือ ... Hello ถ้าให้ num = 3 ผลลัพธ์ที่ได้คือ ... Bye Mahidol Wittayanusorn School L. Ngamprasit

22 คำสั่ง switch : ตัวอย่าง(ปรับปรุง 4)
เงื่อนไข คือ แสดงข้อความว่า Hello เมื่อ num มีค่าเท่ากับ 1 หรือ 2 แสดงข้อความว่า Bye เมื่อ num มีค่าเท่ากับ 3 หรือ 4 public class SwitchExam { public static void main(String[] args) { int num = 1; switch (num) { case 1: case 2: System.out.println("Hello"); break; case 3: case 4: System.out.println("Bye"); } ถ้าให้ num = 4 ผลลัพธ์ที่ได้คือ ... Bye ถ้าให้ num = 5 ผลลัพธ์ที่ได้คือ ... Mahidol Wittayanusorn School L. Ngamprasit

23 คำสั่งควบคุมแบบมีทางเลือก
ศึกษาบทที่ 4 เรื่อง switch ปฏิบัติการที่ 5 คำสั่ง switch เข้าไปที่ webcs.mwit.ac.th การเรียนการสอน Mahidol Wittayanusorn School L. Ngamprasit


ดาวน์โหลด ppt บทที่ 4 คำสั่งควบคุมแบบมีทางเลือก

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


Ads by Google