งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Chapter 5 การทำซ้ำโดย while loop และ do while loop

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Chapter 5 การทำซ้ำโดย while loop และ do while loop"— ใบสำเนางานนำเสนอ:

1 Chapter 5 การทำซ้ำโดย while loop และ do while loop

2 Agenda while loop do while loop Mini debug for while loop Lab

3 loop ในการเขียนโปรแกรม จะมีการประมวลผลซ้ำ (loop หรือ iterate) เพื่อให้ โปรแกรมทำงานตาม statement หรือการประมวลผล ที่กำหนดไว้ ซ้ำมากกว่า 1 ครั้ง โดยไม่จำเป็นที่จะต้องเขียน statement นั้น ซ้ำไปซ้ำมาในโค้ด การทำงานจะทำงานตาม statement ไปจนหมด แล้ว ถ้าหาก เงื่อนไขที่กำหนดไว้ให้ทำซ้ำ ยังเป็นจริง โปรแกรมจะวนกลับไปทำงานตาม statement อีกรอบ จนกว่า เงื่อนไขที่กำหนดไว้จะเป็นเท็จ

4 ส่วนประกอบของการ iteration
มีอยู่ 3 ประเภท Initialization คือ การกำหนดค่าเริ่มต้นของตัวแปรที่จะเป็นเงื่อนไขในการ iteration เช่น x = 1 Testing คือ การทดสอบว่า เงื่อนไขที่ทำการ iteration นั้นยังเป็นจริงหรือไม่ จะมีการทำ iteration ไปเรื่อยๆ หากเงื่อนไขยังเป็นจริง เช่น x > 99 Incrementing เป็นการเปลี่ยนแปลงค่าของตัวแปรที่ใช้เป็นเงื่อนไขในการ iteration เช่น x = x + 1

5 ประเภทของ Iteration Statement
คือ วิธีการทำให้โปรแกรมทำงานเป็น loop ได้ ในภาษา C มีอยู่ 4 ชนิดด้วยกัน while statement do-while statement for statement break and continue statement

6 while statement

7 while statement มีการตรวจสอบเงื่อนไขก่อน หากเงื่อนไขเป็นจริง โปรแกรมจะเริ่มทำตาม statement ที่กำหนดไว้ แล้วกลับไปเริ่มต้นใหม่ จนกว่าเงื่อนไขจะเป็นไม่เป็นจริง ถึงจะหยุดการทำงาน แล้วออกไปจาก loop

8 syntax while (condition) { statement1; statement2; … statementN; }

9 flowchart Entry Statement 2 expr True Statement 1 False Exit

10 Example Example 1 พิมพ์เลข 1 ถึง 10 โดยใช้ while loop Example 2 เลือกการทำงานจากตัวเลข menu ที่กำหนดไว้ หากเลือกตัวเลขนอกเหนือที่กำหนดไว้ จะต้อง ใส่ตัวเลขที่เลือกใหม่

11 Example 1 ตัวอย่าง output จากการทำงาน
#include <stdio.h> main() { int count; count = 1; printf(“Print count from 1 to 10\n”); while (count <= 10) printf(“%d ”, count); count++; } printf(“\n”); Print count from 1 to 10 ตัวอย่าง output จากการทำงาน

12 Example 2 #include <stdio.h> int main() { int a; /* initative value of a so that it can enter the loop */ a = 4; while (a != 1 && a !=2 && a !=3) { printf("1. Check spelling\n"); printf("2. Correct spelling errors\n"); printf("3. Display spelling errors\n"); printf("Enter your choice (1-3): "); /* read selection from keyboard */ scanf("%d", &a); switch(a) { case 1: printf("Check spelling\n"); break; case 2: printf("Correct spelling errors\n"); case 3: printf("Display spelling errors\n"); default: /* do nothing */ } return (0);

13 Example 2 ตัวอย่าง output จากการทำงาน 1. Check spelling
2. Correct spelling errors 3. Display spelling errors Enter your choice (1-3): 5 Enter your choice (1-3): 1 Check spelling ตัวอย่าง output จากการทำงาน

14 do while statement

15 Do while statement จะแตกต่างจาก while statement โดยที่ โปรแกรมจะเริ่มทำงานตาม statement ที่ระบุไว้เลย โดยไม่มีการเช็ค condition ก่อน จากนั้นเมื่อทำงานเสร็จแล้วถึงจะเช็คเงื่อนไข หากเงื่อนไขเป็นจริง จะทำการทำซ้ำ แต่หากเงื่อนไขเป็นเท็จจะจบการทำงานแล้วออกจาก loop

16 syntax do { statement1; statement2; … statementN; } while (condition);

17 flowchart Entry Statement 1 Statement 2 expr True False Exit

18 Example Example 1 พิมพ์เลข 1 ถึง 10 โดยใช้ while loop Example 2 เลือกการทำงานจากตัวเลข menu ที่กำหนดไว้ หากเลือกตัวเลขนอกเหนือที่กำหนดไว้ จะต้อง ใส่ตัวเลขที่เลือกใหม่

19 Example 1 ตัวอย่าง output จากการทำงาน
/* print count from 1 to 10 */ #include <stdio.h> main() { int count; count = 1; printf(“Print count from 1 to 10\n”); do { printf(“%d ”, count); count++; } while (count <=10); printf(“\n”); } Print count from 1 to 10 ตัวอย่าง output จากการทำงาน

20 Example 2 #include <stdio.h> int main() { int a; do { printf("1. Check spelling\n"); printf("2. Correct spelling errors\n"); printf("3. Display spelling errors\n"); printf("Enter your choice (1-3): "); /* read selection from keyboard */ scanf("%d", &a); switch(a) { case 1: printf("Check spelling\n"); break; case 2: printf("Correct spelling errors\n"); case 3: printf("Display spelling errors\n"); default: /* do nothing */ } } while (a != 1 && a != 2 && a !=3); return (0);

21 Example 2 ตัวอย่าง output จากการทำงาน 1. Check spelling
2. Correct spelling errors 3. Display spelling errors Enter your choice (1-3): 5 Enter your choice (1-3): 1 Check spelling ตัวอย่าง output จากการทำงาน

22 Null statement เราสามารถใช้งาน empty statement (null statement) ใน while statement ได้ โดยใช้ ; ต่อท้าย หรือ ในบรรทัดใหม่ เนื่องจากมีการทำงาน ได้ทำไปแล้วในเงื่อนไขของ while จึงไม่จำเป็นต้องมีการทำงานอีกใน loop ตัวอย่าง while ((c = getchar()) == ‘ ’) ; หรือ while ((c = getchar()) == ‘ ’) ; ทำงานเหมือน while ((c = getchar()) == ‘ ’) { /* do nothing */ }

23 Null statement นั่นคือ blank character ใน input stream จะถูกข้าม (เมื่อตัว input stream เป็น blank character จะเข้า loop ได้ แล้วจะไม่ทำอะไร แต่จะไปรับ ค่า input ใหม่อีกครั้ง เรื่อยๆ จนกว่าจะไม่ใช่ blank character) หากต้องการให้ white space ถูกข้ามไป สามารถใช้ code ดังนี้ while ((c = getchar()) == ‘ ’ || c == ‘\n’ || c == ‘\t’);

24 Mini debugging ปัญหาที่พบได้บ่อยจากการใช้ loop แนวทางการแก้ไข
infinity loop คือ โปรแกรมทำงานตาม statement ไปเรื่อยๆ โดยไม่สามารถออกจาก loop ได้ โปรแกรม ไม่สามารถทำงาน ใน loop ได้ แนวทางการแก้ไข ตรวจสอบ condition ที่กำหนด ว่า เป็น จริง หรือ เป็น เท็จ เสมอ หรือไม่ ตรวจสอบค่าของตัวแปรที่ใช้งาน ว่ามีการเปลี่ยนแปลงค่าได้หรือไม่ ตรวจสอบ ว่า ค่าของตัวแปรที่ใช้ในเงื่อนไข สามารถทำให้ condition เป็น จริง หรือ เท็จได้หรือไม่

25 Example while (x = 5) หากใช้เงื่อนไขดังนี้ statement ที่อยู่ใน loop while จะ วนต่อไปเรื่อยๆ ไม่สามารถออก loop ได้ (infinity loop) ค่าที่ได้จาก (x = 5) คือ 1 คือ หมายความว่า เรากำหนดค่า 1 ให้กับตัวแปร x ได้สำเร็จ แต่ไม่ได้เป็นการเปรียบเทียบว่า x เท่ากับ 5 หรือไม่


ดาวน์โหลด ppt Chapter 5 การทำซ้ำโดย while loop และ do while loop

งานนำเสนอที่คล้ายกัน


Ads by Google