Switch statements

Java ระดับกลาง

Jim White

Java Developer

Switch คืออะไร?

  • Switch ทำงานคล้ายกับคำสั่ง if-else
    • ดำเนินการตามเงื่อนไขที่กำหนด
    • ใช้แทนเงื่อนไข if-then-else หลายชั้น
  • กระชับกว่าการใช้ if-then-else ซ้อนกันหลายครั้ง
if (bonus == 10000) {
    System.out.println("Let's buy a car!");
} else if (bonus == 5000) {
    System.out.println("Let's take a trip!");
} else if (bonus == 1000) {
    System.out.println("Let's save!");
}
Java ระดับกลาง

ไวยากรณ์ของ Switch

switch (expression) {
    case value1:  // if (expression == value1)
        // statements
    case value2:  // if (expression == value2)
        // statements
    // use as many case statements as needed
}
Java ระดับกลาง

ตัวอย่าง Switch

char direction = 'N';

switch (direction) {
    case 'E':  
        System.out.println("We are headed east.");
    case 'S':  
        System.out.println("We are headed south.");
    case 'W':  
        System.out.println("We are headed west.");
    case 'N':   
        System.out.println("We are headed north."); // This would be printed
}
Java ระดับกลาง

Break

  • หยุดไม่ให้ Java ทำงานต่อใน case ถัดไป
    • หากไม่มี break Java จะทำงานต่อเนื่องจนกว่าจะพบ break
char direction = 'W';

switch (direction) {
    case 'E':
        System.out.println("We are headed east.");
    case 'S':
        System.out.println("We are headed south.");
    case 'W':
        System.out.println("We are headed west."); // This would be printed
    case 'N':
        System.out.println("We are headed north."); // This would also be printed
}
Java ระดับกลาง

break ช่วยให้รวมหลาย case เข้าด้วยกันได้:

char direction = 'N';

switch (direction) {
    case 'E':     
    case 'W':  
        System.out.println("We are headed east or west.");
        break;
    case 'S':  
    case 'N':
        System.out.println("We are headed south or north.");
        break;       
}
We are headed south or north.
Java ระดับกลาง

Default

  • สามารถเพิ่ม case default ไว้ท้าย switch ได้
    • case default ไม่มีค่าเปรียบเทียบกับ expression
    • หากไม่มี case ใดตรงกับ expression คำสั่งใน default จะถูกเรียกใช้
    • default ต้องอยู่เป็น case สุดท้ายใน switch
  • default ทำหน้าที่เป็น case สำหรับ "ค่าอื่น ๆ ทั้งหมด"
char direction = 'z';

switch (direction) {
  case 'E':     
  case 'W':  
    System.out.println("To east or west.");
    break;
  case 'S':  
  case 'N':
    System.out.println("To south or north.");
    break; 
  default:
    System.out.println("We are lost.");
}
We are lost.
Java ระดับกลาง

Conditional เทียบกับ Switch

  • ใช้ conditional แทนได้เพื่องานเดียวกัน
  • ใช้ได้กับทุกชนิดข้อมูล
char character = 'A';
if (character == 'A') {
    System.out.println("It's A");
} else if (direction =='B') {
    System.out.println("It's B");
} else {
    System.out.println("Unknown");
}
  • Switch อ่านง่ายกว่า
  • ใช้ได้เฉพาะบางชนิดข้อมูล เช่น int, byte, short, char, หรือ String
char character = 'A';
switch (character) {
  case 'A':
    System.out.println("It's A");
    break;
  case 'B':  
    System.out.println("It's B");
    break;
  default:
    System.out.println("Unknown");
}
Java ระดับกลาง

มาฝึกกันเถอะ!

Java ระดับกลาง

Preparing Video For Download...