1 / 41

ครั้งที่ 7 การใช้ Loop

ครั้งที่ 7 การใช้ Loop. อาจารย์สายสุนีย์ เจริญสุข สาขาวิชาเทคโนโลยีสารสนเทศธุรกิจ คณะเทคโนโลยีสารสนเทศ. วัตถุประสงค์. เข้าใจหลักการควบคุมแบบวนหลูบ (Looping) สามารถใช้คำสั่งควบคุมการทำงานของโปรแกรมแบบวนหลูบได้. EAU 1/2552 : อาจารย์สายสุนีย์ เจริญสุข.

navid
Télécharger la présentation

ครั้งที่ 7 การใช้ Loop

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ครั้งที่ 7 การใช้Loop อาจารย์สายสุนีย์ เจริญสุข สาขาวิชาเทคโนโลยีสารสนเทศธุรกิจ คณะเทคโนโลยีสารสนเทศ

  2. วัตถุประสงค์ • เข้าใจหลักการควบคุมแบบวนหลูบ (Looping) • สามารถใช้คำสั่งควบคุมการทำงานของโปรแกรมแบบวนหลูบได้ EAU 1/2552 : อาจารย์สายสุนีย์ เจริญสุข วิชา 00920212 การเขียนโปรแกรมคอมพิวเตอร์ 1

  3. คำสั่ง For การใช้คำสั่ง For ใช้ในการวนซ้ำ For(ตัวแปรใช้นับจำนวนรอบ = จำนวนรอบเริ่มต้น; เงื่อนไขการหยุดวนซ้ำ;สเต็ปขั้นของการนับ) { <ทำงานตามคำสั่ง> }

  4. ตัวอย่าง โปรแกรมการใช้คำสั่ง for loop ในการพิมพ์ค่าของเลขจำนวนเต็มจาก 1 ถึง 10 พร้อมกับหาผลบวกของเลขเหล่านั้น แล้วแสดงออกที่จอภาพ static void Main(string[] args) { inti, sum = 0; for(i=1;i<=10;i++){ Console.Write("{0}\t",i); //แต่ถ้าต้องการผลลัพธ์เรียงทีละหนึ่งจำนวนให้ใช้คำสั่ง WriteLine sum= sum+1; } Console.WriteLine("Sum of all numbers from 1 to 10 is {0}",sum); }

  5. ผลลัพธ์

  6. ตัวอย่าง โปรแกรมเมื่อคลิกปุ่ม “วนซ้ำแบบเดินหน้า” จะแสดงค่าเพิ่มขึ้นทีละหนึ่ง แต่ถ้าคลิกปุ่ม”วนซ้ำแบบถอยหลัง” จะแสดงลดค่าที่ละหนึ่ง แต่ถ้าคลิกปุ่ม “ปรับ stepการวนซ้ำ” จะแสดงค่าเพิ่มทีละ 3 ค่า

  7. private void btnLoopUp_Click(object sender, EventArgs e) { int cnt = 10; string strOut = "วนรอบทั้งหมด =" + cnt.ToString() + "\tรอบ\r\n"; strOut += "-------------------------------------------\r\n"; for (int i = 1; i <= 10; i++) { strOut += "วนรอบที่ : " + i.ToString()+"\r\n"; } txtOut.Text = strOut; }

  8. private void btnLoopDown_Click(object sender, EventArgs e) { int cnt = 10; string strOut = "วนรอบทั้งหมด =" + cnt.ToString() + "\tรอบ\r\n"; strOut += "-------------------------------------------\r\n"; for (int i = cnt; i >=1; i--) { strOut += "วนรอบที่ : " + i.ToString() + "\r\n"; } txtOut.Text = strOut; }

  9. private void btnStep_Click(object sender, EventArgs e) { int cnt = 10; string strOut = "วนรอบทั้งหมด =" + cnt.ToString() + "\tรอบ\r\n"; strOut += "-------------------------------------------\r\n"; for (int i = 0; i <= 10; i+=3) { strOut += "วนรอบที่ : " + i.ToString() + "\r\n"; } txtOut.Text = strOut; }

  10. ตัวอย่าง โปรแกรมในการใช้คำสั่ง For-Loop ที่ซ้ำซ้อน ดังต่อไปนี้เช่น กรณีที่เมื่อกำหนด row= 5,Column= 7 แล้วคลิกปุ่ม “วาดตาราง” จะได้ผลลัพธ์ในTextBox ดังรูป

  11. private void btnTable_Click(object sender, EventArgs e) { txtOut.Text = ""; for (int row = 1; row <= numRow.Value; row++) { for (int column = 1; column <= nudColumn.Value; column++) { txtOut.Text += column.ToString() + "|"; } txtOut.Text += "\r\n"; } }

  12. เช่น กรณีที่กำหนดช่องล่างสุดเป็น 8 แล้วคลิกปุ่ม”วาดสามเหลี่ยม UP” จะได้ผลลัพธ์ในช่องTextBoxดังรูปด้านล่างนี้

  13. private void btnTriUp_Click(object sender, EventArgs e) { txtOut.Text = ""; for (int j = 1; j <= (int)nudTriRow.Value; j++) { for (int i = 1; i <= j; i++) { txtOut.Text += i.ToString() + " "; } txtOut.Text += "\r\n"; } }

  14. เช่น กรณีที่กำหนดช่องล่างสุดเป็น 8 แล้วคลิกปุ่ม”วาดสามเหลี่ยม Down” จะได้ผลลัพธ์ในช่องTextBoxดังรูปด้านล่างนี้

  15. private void btnTriDown_Click(object sender, EventArgs e) { txtOut.Text = ""; for (int j = (int)nudTriRow.Value; j >= 1; j--) { for (int i = 1; i <= j; i++) { txtOut.Text += i.ToString() + " "; } txtOut.Text += "\r\n"; } }

  16. คำสั่ง While Loop รูปแบบของคำสั่ง While มีลักษณะดังนี้ While(codition){ Statement 1; Statement 2; } หมายเหตุ ภายในลูปจะต้องมีคำสั่งในการเพิ่ม ลด หรือเปลี่ยนแปลงค่าของตัวแปรที่ใช้ตรวจสอบเงื่อนไข เพื่อให้เป็นเท็จจะได้จบลูป

  17. ตัวอย่าง โปรแกรมการใช้คำสั่ง While Loop ในการพิมพ์ค่าของเลขจำนวนเต็มจาก 1 ถึง 10 พร้อมกับหาผลบวกของเลขเหล่านั้น แล้วแสดงออกที่จอภาพ static void Main(string[] args) { int i, sum = 0; i = 1; /*initialization*/ while (i <= 10) /*condition*/ { Console.Write("{0}", i); sum = sum + i; i++; /*increment*/ } Console.WriteLine("\nSum of all number from 1 to 10 is {0}", sum); }

  18. ผลลัพธ์ได้ดังนี้

  19. ตัวอย่าง โปรแกรมการสุ่มตัวเลขจากการ Random

  20. row = num; col = 1; while (row >= 1) { while (col <= row) { txtOut.Text += num.ToString() + " "; col++; } txtOut.Text += "\r\n"; row--; col = 1; } } private void btnRandom_Click(object sender, EventArgs e) { Random rndObject = new Random(); int num = rndObject.Next(10); txtOut.Text = " "; int row = 1; int col = 1; while (row <= num) { while (col <= row) { txtOut.Text += num.ToString() + " "; col++; } txtOut.Text += "\r\n"; row++; col = 1; }

  21. คำสั่ง Do-While Loop รูปแบบคำสั่ง Do-While มีลักษณะดังนี้ Do{ Statement 1; Statement 2; ….. Statement n; } While(condition);

  22. ตัวอย่าง โปรแกรมคำสั่ง Do-While Loop ในการพิมพ์ค่าของเลขจำนวนเต็มจาก 1 ถึง 10 พร้อมหาผลบวกของเลขเหล่านั้น แล้วแสดงผลทางจอภาพ static void Main(string[ ] args) { int i, sum = 0; i = 1; do { Console.Write("{0}", i); sum = sum + i; i++; } while (i <= 10); Console.WriteLine("\nSum of all numbers from 1 to 10 is {0}", sum); }

  23. ผลลัพธ์ได้ดังนี้

  24. ตัวอย่าง โปรแกรมในการคิดดอกเบี้ยเงินทบต้น โดยกรอกข้อมูลเงินต้น อัตราดอกเบี้ย และจำนวนปีที่ฝาก จากนั้นคลิกปุ่ม “ผลตอบแทน” จะได้ผลรับในช่อง TextBox ดังรูป

  25. private void btnReturn_Click(object sender, EventArgs e) { if ((txtBase.Text == "") || (txtInterest.Text == "")) { MessageBox.Show("คุณต้องระบุเงินต้นและดอกเบี้ยให้ครบถ้วน", "ผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { txtReturn.Text =""; double dblBase = Convert.ToDouble(txtBase.Text); double dblInterest = Convert.ToDouble(txtInterest.Text); double dblYear = (double)nudReturn.Value; double dblReturn = 0.0; int i =1; do{ dblReturn = dblBase+(dblBase * dblInterest /100); txtReturn.Text += "ผลตอบแทนปีที่ "+ i.ToString()+"\t= " + dblReturn.ToString("#,###.##")+" บาท" +"\r\n"; dblBase = dblReturn; i++; }while(i<=(int)nudReturn.Value); } }

  26. ตัวอย่างโปรแกรมต่อไปนี้ เป็นการรับค่าของเลขจำนวนเต็ม 2 จำนวนจากผู้ใช้ เก็บไว้ที่ตัวแปร num1 และ num2 ตามลำดับ จากนั้นจะรับค่าซึ่งเป็นเลขจำนวนเต็มอีก 1 ค่า เก็บไว้ที่ตัวแปร choice หลังจากนั้นจะใช้คำสั่ง switch โดยใช้เลขจำนวนเต็มที่รับเข้ามาเก็บไว้ที่ตัวแปร choice เป็นตัวเปรียบเทียบ ซึ่งถ้าตรงกับกรณีใดกรณีหนึ่ง จะทำคำสั่งที่อยู่ในส่วนนั้นๆ คือ • ถ้าเป็นเลข 1 โปรแกรมจะพิมพ์ผลบวก(+) ของ num1 และ num2 • ถ้าเป็นเลข 2 โปรแกรมจะพิมพ์ผลลบ(-) ของ num1 และ num2 • ถ้าเป็นเลข 3 โปรแกรมจะพิมพ์ผลคูณ(*) ของ num1 และ num2 • ถ้าเป็นเลข 2 โปรแกรมจะพิมพ์ผลหาร(/) ของ num1 และ num2 • ถ้าเป็นเลขอื่นๆ โปรแกรมจะพิมพ์ข้อความว่า “Invalid Choice!....Byte Byte”

  27. case 1: Console.WriteLine("{0}+{1} = {2}", num1, num2, num1 + num2); break; case 2: Console.WriteLine("{0}-{1} = {2}", num1, num2, num1 - num2); break; case 3: Console.WriteLine("{0}*{1} = {2}", num1, num2, num1 * num2); break; case 4: Console.WriteLine("{0}/{1} = {2}", num1, num2, num1 / num2); break; default: Console.WriteLine("Invalid Choice!.........Bye Bye ");   } } static void Main(string[] args) { int num1, num2, choice; string a, b, c; Console.Write("num1="); a = Console.ReadLine(); num1 = Convert.ToInt32(a); Console.Write("num2="); b = Console.ReadLine(); num2 = Convert.ToInt32(b); Console.Write("Your selection is:"); c = Console.ReadLine(); choice = Convert.ToInt32(c); switch (choice) {

  28. ตัวอย่าง การใช้คำสั่ง switch • ถ้าค่าที่อยู่ในตัวแปร choice มีค่าเท่ากับ 1 หรือ 2 หรือ 3 จะพิมพ์ข้อความว่า “You gave 1,2 or 3” • ถ้าค่าที่อยู่ในตัวแปร choice มีค่าเท่ากับ 4 หรือ 5 จะพิมพ์ข้อความว่า “You gave 4 or5” • แต่ถ้าค่าที่อยู่ในตัวแปร choice มีค่าอื่นใดคือไม่ใช่ 1,2,3,4 หรือ 5 ส่วนของคำสั่ง switch จะไม่แสดงอะไรออกที่หน้าจอ ซึ่งโดยส่วนมากแล้วจะนิยมใส่ค่า default เอาไว้ในช่วงสุดท้าย ดังตัวอย่างต่อไปนี้

  29. case 1: case 2: case 3: Console.WriteLine("You gave 1,2,3 "); break; case 4: case 5: Console.WriteLine("You gave 4 or 5 "); break; } } static void Main(string[ ] args) { int choice; string a; Console.Write("num ="); a = Console.ReadLine(); choice = Convert.ToInt32(a); switch (choice) {

  30. ตัวอย่าง โปรแกรมการใช้คำสั่ง break,continue และ goto • เมื่อกดปุ่ม break จะทำการสุ่มตัวเลขดังรูป

  31. private void btnBreak_Click(object sender, EventArgs e) { Random rndObject = new Random(); int num = rndObject.Next(10); txtOut.Text = ""; for(int i = 1;i<=10;i++) { txtOut.Text +="วนรอบครั้งที่"+i.ToString()+"\r\n"; if(i==num) { txtOut.Text +="สุ่มตัวเลขจำนวน ="+num.ToString(); break; } } }

  32. เมื่อกดปุ่ม “Continue” จะทำการสุ่มตัวเลขดังรูป

  33. private void btnContinue_Click(object sender, EventArgs e) { Random rndObject = new Random(); int num = rndObject.Next(10); txtOut.Text = ""; for (int i = 1; i <= 10; i++) { txtOut.Text += "วนซ้ำรอบที่" + i.ToString() ; if (i == num) { txtOut.Text += "\tตรงกับจำนวนที่สุ่ม " + "\r\n"; continue; } txtOut.Text += "\r\n"; } }

  34. เมื่อกดปุ่ม “Goto” จะทำการสุ่มตัวเลขดังรูป

  35. case 5: txtOut.Text += "ห้า:"; goto case 1; case 6: txtOut.Text += "หก:"; goto case 2; case 7: txtOut.Text += "เจ็ด:"; goto case 1; case 8: txtOut.Text += "แปด:"; goto case 2; case 9: txtOut.Text += "เก้า:"; goto case 1; } } private void btnGoto_Click(object sender, EventArgs e) { Random rndObject = new Random(); int num = rndObject.Next(10); switch(num) { case 0: txtOut.Text += num+ "สุ่มได้เลข 0" +"\r\n"; break; case 1: txtOut.Text += num+"\tเลขที่สุ่มเป็นเลขคี่"+"\r\n"; break; case 2: txtOut.Text += num+"\tเลขที่สุ่มเป็นเลขคู่"+"\r\n"; break; case 3: txtOut.Text += "สาม:"; goto case 1; case 4: txtOut.Text += "สี่:"; goto case 2;

  36. แบบทดสอบ 1.กำหนดให้นักศึกษาเขียนโปรแกรมแสดงเลขคี่ ได้ผลลัพธ์ดังนี้ 1,3.5.7,9 2. กำหนดให้นักศึกษาเขียนโปรแกรมแสดงเลขคู่ โดยได้ผลลัพธ์ดังนี้0,2,4,6,8,10

  37. การบ้านงานกลุ่ม • จงเขียนโปรแกรมโดยใช้คำสั่ง for,while และ do-while ในการพิมพ์อักษรตัวใหญ่จาก Z ถึง A • จงเขียนโปรแกรมเพื่อรับค่าจากคีย์บอร์ดเป็นเลขจำนวนเต็ม 2 จำนวน ไว้ที่ตัวแปร a และ b โดย 0<a<10 และ 20<b<40 ถ้าค่าไม่อยู่ในช่วงดังกล่าว ให้โปรแกรมแสดง error และวนไปรับค่าใหม่ ทำการบวกเลขเฉพาะเลขคู่ที่มีค่าตั้งแต่ a ถึง b แล้วแสดงผลลัพธ์ออกทางจอภาพ • จงเขียนโปรแกรมแสดงตัวเลข 0 ถึง 4 เรียงออกมาเป็นรูปพีระมิด ดังแสดงหน้าถัดไป

  38. 0 0 1 0 1 2 0 1 2 3 0 1 2 3 4 0 1 2 3 0 1 2 3 0 1 2 0 1 0

More Related