# 9.การตัดสินใจด้วย Switch statements

💬 หลังจากที่เราได้เห็นการตัดสินใจของคอมพิวเตอร์ผ่านคำสั่ง **IF statements** กันไปบ้างละ ในรอบนี้เราลองมาดูการตัดสินใจแบบง่ายๆด้วยคำสั่งที่เรียกว่า **Switch statements** กันดูบ้างนะครับ

{% embed url="<https://www.youtube.com/watch?v=futpp6q8l9c&list=PLUjAn8nwWnijERZ3HpzBk7NfSrau74_lQ&index=17>" %}

## 🎯 สรุปสั้นๆ

### 👨‍🚀 การเขียน Switch แบบมาตรฐาน

```csharp
switch( EXPRESSION )
{
  case PATTERN1:
     // ถ้า expression ตรงกับ pattern 1 จะเข้ามาที่งานที่นี่
     break;
  case PATTERN2:
     // ถ้า expression ตรงกับ pattern 2 จะเข้ามาที่งานที่นี่
     break;
  default:
     // ถ้า expression ไม่ตรงกับ pattern ไหนเลยจะเข้ามาทำงานที่นี่
     break;
}
```

{% hint style="info" %}
**เกร็ดความรู้ 1**\
ในการเขียน switch นั้นเราสามารถใส่วงเล็บ { } ลงไปใน case หรือ default ได้นะจ๊ะ เพื่อเป็นการบอกว่า scope ของการทำงานอยู่ที่ไหน ตามตัวอย่าง code ด้านล่าง
{% endhint %}

```csharp
switch( EXPRESSION )
{
  case PATTERN1:
     {
       // ถ้า expression ตรงกับ pattern 1 จะเข้ามาที่งานที่นี่
       break;
     }
   ...
}
```

{% hint style="info" %}
**เกร็ดความรู้ 2**\
เราสามารถรวม case ที่ทำงานเหมือนกันเอาไว้ด้วยกันได้ และ รวมถึงการรวม case default ด้วยเช่นกัน ตาม code ด้านล่าง
{% endhint %}

```csharp
switch( EXPRESSION )
{
  default:
  case PATTERN1:
  case PATTERN2:
  case PATTERN3:
     {
       // ถ้า expression ตรงกับ pattern 1,2,3 
       // หรือไม่ตรงกับ pattern อื่นๆเลยจะเข้ามาที่งานที่นี่
       break;
     }
   ...
}
```

### 👨‍🚀 การเขียน switch โดยใช้ Type pattern

ในภาษา C# รุ่นใหม่ๆจะรองรับการใช้สิ่งที่เรียกว่า Type pattern แล้ว โดยเราสามารถเอาชนิดข้อมูลมาใช้เป็นเงื่อนไขได้

```csharp
switch( EXPRESSION )
{
  case int a:
       // ถ้า expression เป็น int จะเข้ามาที่งานที่นี่
       break;
  case double b:
       // ถ้า expression เป็น double จะเข้ามาที่งานที่นี่
       break;
   ...
}
```

### 👨‍🚀 การเขียน switch โดยใช้ When clause

```csharp
switch( EXPRESSION )
{
  case int a when a > 12:
       // ถ้า expression เป็น int และมีค่ามากกว่า 12 จะเข้ามาที่งานที่นี่
       break;
   ...
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.saladpuk.com/beginner-1/csharp101/basic/switch.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
