# 10.การทำงานซ้ำๆด้วย While

💬 ในการเขียนโปรแกรมหลายๆ บางทีเราก็จะพบว่าโค้ดบางส่วนที่เราอยากให้มันทำงานหลายๆรอบ เราต้องไปคอยนั่งกดก๊อปปี้ไปวางหลายๆครั้งใช่มุ้ยล๊า ในรอบนี้เราจะลองดูคำสั่งที่ใช้ในการทำงานเดิมซ้ำๆกันที่เราเรียกมันว่า **While loop statements** นั่นเอง

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

## 🎥 ตัวอย่างการใช้คำสั่ง While loop statements

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

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

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

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

### 👨‍🚀 คำสั่ง while loop พื้นฐาน

```csharp
while( เงื่อนไข )
{
  // ถ้าเงื่อนไขเป็นจริงจะทำในวงเล็บนี้เรื่อยๆ
}
```

### 👨‍🚀 คำสั่ง break

เมื่อเราทำงานอยู่ใน loop ในบางครั้งเราก็ไม่อยากให้มันทำงานต่อแล้ว เราสามารถใช้คำสั่ง **break** เพื่อออกจาก loop ที่กำลังทำงานอยู่ได้


---

# 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/while-loop.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.
