SlideShare a Scribd company logo
1 of 9
Download to read offline
การเขียนค้าสั่งควบคุมแบบวนซ้า
การวนซ้า หมายถึง การสั่งให้โปรแกรมท้างานอย่างใดอย่างหนึ่งวนไปวนมา
จนกระทังครบตามจ้านวนที่ต้องการ หรือ ตรงตามท้าซ้าเกิดขึนบ่อย ๆ ใน
่
ชีวิตประจ้าวัน เช่น การวนรับรหัสผ่าน จนกว่าจะใส่รหัสผ่านถูกต้อง หรือไม่เกินกา
รวนรับรหัส บัตรเติมเงินของบริการโทรศัพท์ ไม่เกินครัง จนกว่าจะใส่รหัสบัตรเติม
เงินถูกต้อง เป็นต้น
ค้าสั่ง for
ค้าสั่ง for เป็นเป็นค้าสั่งสั่งควบคุมการท้างานและก้าหนดการท้างานวนรอบโดยตังค่า
ก่อน แล้วจึงท้าการพิจารณาเงื่อนไข ถ้าเงื่อนไขเป็นจริง ก็จะท้าค้าสั่งและวนรอบต่อไป
ตัวอย่างการเขียนโปรแกรม
ผลลัพธ์ของโปรแกรม

การเขียนโดยใช้ค้าสั่ง for จะเห็นได้ว่ามีความกระชับเขียนได้สันกว่าค้าสั่ง while
เพราะสามารถก้าหนดได้ในครังเดียว แต่การท้างานก็จะได้ผลเหมือนกันกับค้าสั่งwhile
ลักษณะการท้างานก็คือโปรแกรมจะท้าการเช็คในส่วนของเงื่อนไข หากเงื่อนไข
เป็นจริงก็จะเพิ่มค่าของตัวแปรไป 1 และท้าค้าสั่งในลูปจากนันก็ท้าการตรวจสอบเงื่อนไขอีก
หากเงื่อนไขเป็นจริงก็จะท้างานซ้า ๆ แบบนีไปเรื่อย ๆ จนกว่าเงื่อนไขเป็นเท็จ การท้างาน
ของโปรแกรมก็จะหลุดจากลูป และจบการท้างาน
ค้าสั่ง While
ค้าสั่ง While เป็นค้าสั่งส้าหรับใช้ควบคุมการท้างานแบบมีเงือนไข อีกแบบหนึ่ง ซึ่งแตกต่างออกไปจาก
การใช้งานค้าสั่ง if และelseif ก่อนหน้านี ซึ่งค้าสั่ง While เป็นค้าสั่งที่จ้าเป็นในการน้าไป
ใช้เขียนโปรแกรม ลองเข้ามาดูค่ะ ว่ามีรูปแบบการเขียนและวิธีเขียนอย่างไร
ค้าสั่ง While เป็นค้าสั่งส้าหรับใช้ควบคุมการท้างานแบบมีเงือนไข ในการก้าหนดค้าสั่งให้มี
การท้างานแบบวนรอบ โดยพิจารณาเงื่อนไขก่อน หากค่าที่ได้เป็นจริงตามเงื่อนไข จึงจะประมวลผล
ตามค้าสั่งที่ก้าหนดไว้
รูปแบบการเขียน ค้าสั่ง While
While (...เงื่อนไข...) {ค้าสั่งที่ต้องการให้ท้างาน เมื่อเงื่อนไขเป็นจริง
ค้าสั่ง Do.....While
ค้าสั่งนีมีลักษณะการท้างานคล้ายกับค้าสั่ง While แต่มีส่วนแตกต่างอยู่เล็ก ๆ น้อย ๆ ที่มี
Do เพิ่มเข้ามานี่ดิ แล้วไอ้ Do นี่จะท้าให้แตกต่างจาก While เดี่ยว ๆ
ค้าสั่ง do while เป็นค้าสั่งที่ก้าหนดให้มีการท้างานวนรอบ คล้าย ๆ ค้าสั่ง While
แต่แตกต่างกันที่ค้าสั่ง do while จะให้ท้าค้าสั่งในลูป do ก่อน แล้วค่อยพิจารณาเงื่อนไข
ใน while
ถ้าค่าเงื่อนไขใน while เป็นจริง จึงจะวนรอบท้าค้าสั่งในลูป doต่อไป
รูปแบบการใช้ค้าสั่ง do while
do {ค้าสั่ง} while (เงื่อนไข)
ตัวอย่างการโปรแกรม

ผลลัพธ์ของโปรแกรม
จากตัวอย่างการโปรแกรม เราได้ท้าการเขียนโปรแกรม โดยสร้างตัวแปร $i แล้วให้มีค่า
เท่ากับ 5 หลังจากนัน ตรงนีละทีจะท้างานต่างจาก while เราะจะเจอกับค้าสั่ง do เมื่อเจอค้าสั่ง
่
doโปรแกรมจะท้าเลยทันที (do แปลว่าท้านี่น่า) ซึ่งก็คือแสดงผลค่าตัวแปร i คือ แสดงเลข 5
ออกมา และขึนบรรทัดใหม่ แล้วเมือเจอค้าสั่ง $i-- จ้ากันได้ไหมเพิ่งผ่านไปในบทความที่แล้ว
่
มีความหมายว่าลบค่า $i ไป 1 ดังนันตอนนี $i=4 แล้ว
จากนันโปรแกรมจะเจอค้าสั่ง while ซึ่งจะท้าการเช็คค่าในตัวแปร i คือ หากตัวแปร i
มีค่ามากกว่าหรือเท่ากับ 1 โปรแกรมจะวนลูปท้าค้าสั่งในลูป do อีกครัง การท้างานก็เหมือน
ค้าสั่งwhile ก็คือเช็คจนเงือนไขเป็นเท็จจึงจะหลุดลูปและจบการท้างาน
่
ค้าถามคือ แล้วเมื่อไหร่ละจะหลุดลูป ค้าตอบก็คือเมื่อมันลดค่า$i ลงรอบละ 1 เรื่อย ๆ จนเมื่อ
$i=0 ก็จะหลุดลูปและจบการท้างาน
ในการท้างานจริง ๆ การเขียนโดยใช้เงือนไขจ้าพวก while เราจะเอาไปใช้ประโยชน์
ในการท้าซ้า ๆ เช่น การดึงขอมูลจากดาต้าเบสมาแสดง การวนรอบแสดงค้าสั่งเซลล์ต่าง ๆ
ของตารางเป็นต้น ซึ่งเราจะให้อะไรแสดงผลออกทางหน้าจอ เราก็เอาสิ่งเหล่านัน มาใส่ไว้ใน
ลูปที่ต้องการให้ท้างาน แล้วจะแสดงเท่าไหร่ ก็ขึนอยูกับเงือนไข
่ ่
คณะผู้จัดทา
นาย พิศิษฐ์ อัจฉริยหิรญชัย
ั
นาย สฤษฏ์พนธ์ สุโนภักดิ์
นางสาว ณิชากร เอี่ยมม่วง
นางสาว พรนภา กากะนิก
นางสาว พรสวรรค์ ศรีเมือง
ชั้นมัธยมศึกษาปีที่ 5/4

เลขที่ 6
เลขที่ 7
เลขที่ 14
เลขที่ 16
เลขที่ 17

More Related Content

Featured

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 

Featured (20)

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 

การเขียนคำสั่'งควบคุมแบบวนซ้ำ

  • 1. การเขียนค้าสั่งควบคุมแบบวนซ้า การวนซ้า หมายถึง การสั่งให้โปรแกรมท้างานอย่างใดอย่างหนึ่งวนไปวนมา จนกระทังครบตามจ้านวนที่ต้องการ หรือ ตรงตามท้าซ้าเกิดขึนบ่อย ๆ ใน ่ ชีวิตประจ้าวัน เช่น การวนรับรหัสผ่าน จนกว่าจะใส่รหัสผ่านถูกต้อง หรือไม่เกินกา รวนรับรหัส บัตรเติมเงินของบริการโทรศัพท์ ไม่เกินครัง จนกว่าจะใส่รหัสบัตรเติม เงินถูกต้อง เป็นต้น
  • 2. ค้าสั่ง for ค้าสั่ง for เป็นเป็นค้าสั่งสั่งควบคุมการท้างานและก้าหนดการท้างานวนรอบโดยตังค่า ก่อน แล้วจึงท้าการพิจารณาเงื่อนไข ถ้าเงื่อนไขเป็นจริง ก็จะท้าค้าสั่งและวนรอบต่อไป ตัวอย่างการเขียนโปรแกรม
  • 3. ผลลัพธ์ของโปรแกรม การเขียนโดยใช้ค้าสั่ง for จะเห็นได้ว่ามีความกระชับเขียนได้สันกว่าค้าสั่ง while เพราะสามารถก้าหนดได้ในครังเดียว แต่การท้างานก็จะได้ผลเหมือนกันกับค้าสั่งwhile ลักษณะการท้างานก็คือโปรแกรมจะท้าการเช็คในส่วนของเงื่อนไข หากเงื่อนไข เป็นจริงก็จะเพิ่มค่าของตัวแปรไป 1 และท้าค้าสั่งในลูปจากนันก็ท้าการตรวจสอบเงื่อนไขอีก หากเงื่อนไขเป็นจริงก็จะท้างานซ้า ๆ แบบนีไปเรื่อย ๆ จนกว่าเงื่อนไขเป็นเท็จ การท้างาน ของโปรแกรมก็จะหลุดจากลูป และจบการท้างาน
  • 4. ค้าสั่ง While ค้าสั่ง While เป็นค้าสั่งส้าหรับใช้ควบคุมการท้างานแบบมีเงือนไข อีกแบบหนึ่ง ซึ่งแตกต่างออกไปจาก การใช้งานค้าสั่ง if และelseif ก่อนหน้านี ซึ่งค้าสั่ง While เป็นค้าสั่งที่จ้าเป็นในการน้าไป ใช้เขียนโปรแกรม ลองเข้ามาดูค่ะ ว่ามีรูปแบบการเขียนและวิธีเขียนอย่างไร ค้าสั่ง While เป็นค้าสั่งส้าหรับใช้ควบคุมการท้างานแบบมีเงือนไข ในการก้าหนดค้าสั่งให้มี การท้างานแบบวนรอบ โดยพิจารณาเงื่อนไขก่อน หากค่าที่ได้เป็นจริงตามเงื่อนไข จึงจะประมวลผล ตามค้าสั่งที่ก้าหนดไว้ รูปแบบการเขียน ค้าสั่ง While While (...เงื่อนไข...) {ค้าสั่งที่ต้องการให้ท้างาน เมื่อเงื่อนไขเป็นจริง
  • 5. ค้าสั่ง Do.....While ค้าสั่งนีมีลักษณะการท้างานคล้ายกับค้าสั่ง While แต่มีส่วนแตกต่างอยู่เล็ก ๆ น้อย ๆ ที่มี Do เพิ่มเข้ามานี่ดิ แล้วไอ้ Do นี่จะท้าให้แตกต่างจาก While เดี่ยว ๆ ค้าสั่ง do while เป็นค้าสั่งที่ก้าหนดให้มีการท้างานวนรอบ คล้าย ๆ ค้าสั่ง While แต่แตกต่างกันที่ค้าสั่ง do while จะให้ท้าค้าสั่งในลูป do ก่อน แล้วค่อยพิจารณาเงื่อนไข ใน while ถ้าค่าเงื่อนไขใน while เป็นจริง จึงจะวนรอบท้าค้าสั่งในลูป doต่อไป รูปแบบการใช้ค้าสั่ง do while do {ค้าสั่ง} while (เงื่อนไข)
  • 7. จากตัวอย่างการโปรแกรม เราได้ท้าการเขียนโปรแกรม โดยสร้างตัวแปร $i แล้วให้มีค่า เท่ากับ 5 หลังจากนัน ตรงนีละทีจะท้างานต่างจาก while เราะจะเจอกับค้าสั่ง do เมื่อเจอค้าสั่ง ่ doโปรแกรมจะท้าเลยทันที (do แปลว่าท้านี่น่า) ซึ่งก็คือแสดงผลค่าตัวแปร i คือ แสดงเลข 5 ออกมา และขึนบรรทัดใหม่ แล้วเมือเจอค้าสั่ง $i-- จ้ากันได้ไหมเพิ่งผ่านไปในบทความที่แล้ว ่ มีความหมายว่าลบค่า $i ไป 1 ดังนันตอนนี $i=4 แล้ว จากนันโปรแกรมจะเจอค้าสั่ง while ซึ่งจะท้าการเช็คค่าในตัวแปร i คือ หากตัวแปร i มีค่ามากกว่าหรือเท่ากับ 1 โปรแกรมจะวนลูปท้าค้าสั่งในลูป do อีกครัง การท้างานก็เหมือน ค้าสั่งwhile ก็คือเช็คจนเงือนไขเป็นเท็จจึงจะหลุดลูปและจบการท้างาน ่ ค้าถามคือ แล้วเมื่อไหร่ละจะหลุดลูป ค้าตอบก็คือเมื่อมันลดค่า$i ลงรอบละ 1 เรื่อย ๆ จนเมื่อ $i=0 ก็จะหลุดลูปและจบการท้างาน ในการท้างานจริง ๆ การเขียนโดยใช้เงือนไขจ้าพวก while เราจะเอาไปใช้ประโยชน์ ในการท้าซ้า ๆ เช่น การดึงขอมูลจากดาต้าเบสมาแสดง การวนรอบแสดงค้าสั่งเซลล์ต่าง ๆ ของตารางเป็นต้น ซึ่งเราจะให้อะไรแสดงผลออกทางหน้าจอ เราก็เอาสิ่งเหล่านัน มาใส่ไว้ใน ลูปที่ต้องการให้ท้างาน แล้วจะแสดงเท่าไหร่ ก็ขึนอยูกับเงือนไข ่ ่
  • 8.
  • 9. คณะผู้จัดทา นาย พิศิษฐ์ อัจฉริยหิรญชัย ั นาย สฤษฏ์พนธ์ สุโนภักดิ์ นางสาว ณิชากร เอี่ยมม่วง นางสาว พรนภา กากะนิก นางสาว พรสวรรค์ ศรีเมือง ชั้นมัธยมศึกษาปีที่ 5/4 เลขที่ 6 เลขที่ 7 เลขที่ 14 เลขที่ 16 เลขที่ 17