Câu lệnh switch

Java trung cấp

Jim White

Java Developer

Switch là gì?

  • Switch tương tự if-else
    • Thực thi theo điều kiện
    • Dùng thay cho nhiều if-then-else
  • Ngắn gọn hơn nhiều 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 trung cấp

Cú pháp switch

switch (expression) {
    case value1:  // if (expression == value1)
        // statements
    case value2:  // if (expression == value2)
        // statements
    // use as many case statements as needed
}
Java trung cấp

Ví dụ 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 trung cấp

Break

  • Dừng Java chạy tiếp các case sau
    • Không có break, Java sẽ thực thi các câu lệnh ở case tiếp theo cho đến khi gặp 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 trung cấp

break cho phép gộp nhiều 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 trung cấp

Mặc định

  • Có thể thêm default tùy chọn ở cuối switch
    • Nhánh default không có giá trị để so sánh với biểu thức
    • Nếu không nhánh nào khớp, các lệnh trong default sẽ chạy
    • default phải là nhánh cuối trong switch
  • default là cách xử lý "các giá trị còn lại"
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 trung cấp

Câu điều kiện và switch

  • Có thể dùng điều kiện để làm cùng tác vụ
  • Dùng được với mọi kiểu dữ liệu
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 dễ đọc hơn
  • Chỉ dùng với một số kiểu như int, byte, short, char hoặc 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 trung cấp

Ayo berlatih!

Java trung cấp

Preparing Video For Download...