บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ...
TRANSCRIPT
![Page 1: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/1.jpg)
บทท 7 การเขยนโปรแกรมแบบวนรอบทาซ า (Loop)
รายวชา สธ 113 การออกแบบโปรแกรมทางธรกจเบองตน
อ.อภพงศ ปงยศ
![Page 2: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/2.jpg)
Overview
การวนรอบทาซา (Loop)
คาสง while Statement
คาสง do-while Statement
คาสง for Statement
คาสง break และ continue
2
![Page 3: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/3.jpg)
การวนรอบทาซ า (Loop)
ในการเขยนโปรแกรม จะมการประมวลผลซา (Loop หรอ Iteration) เพอให โปรแกรมทางานตาม Statement หรอการประมวลผลทกาหนดไว ซากนมากกวา 1 ครง โดยไมจ ำเปนทจะตองเขยน Statement นน ซ ำไปซ ำมำในโคด
การทางานจะทางานตาม Statement ไปจนหมด แลวถาหากเงอนไขทกาหนดไวใหทาซายงเปนจรง โปรแกรมจะวนกลบไปทางานตาม Statement อกรอบจนกวาเงอนไขทกาหนดไวจะเปนเทจ จงจะออกจากคาสงทาซา
3
![Page 4: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/4.jpg)
สวนประกอบของ Iteration
มอย 3 ประเภท 1) Initialization คอ การกาหนดคำเรมตนของตวแปรทจะเปนเงอนไขในการ
iterationเชน x = 1
2) Testing คอ การทดสอบวา เงอนไขททาการ Iteration นนยงเปนจรงหรอไม จะมการทา Iteration ไปเรอยๆ หากเงอนไขยงเปนจรง
เชน x < 20 3) Incrementing เปนการเปลยนแปลงคาของตวแปรทใชเปนเงอนไขในการ
Iterationเชน x = x + 1 หรอ x++
4
![Page 5: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/5.jpg)
ประเภทของ Iteration Statement
คอ วธการทาใหโปรแกรมทางานเปน Loop ได
ในภาษา C มอย 4 ชนดดวยกน คอwhile statement
do-while statement
for statement
break and continue statement
5
![Page 6: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/6.jpg)
While Statement
มการตรวจสอบเงอนไขกอน หากเงอนไขเปนจรง โปรแกรมจะเรมทาตาม Statement ทกาหนดไว แลวกลบไปเรมตนใหม จนกวาเงอนไขจะเปนเทจ ถงจะหยดการทางาน แลวออกไปจาก Loop
6
while (condition)
{
statement1;
statement2;
...
statementN;
}
![Page 7: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/7.jpg)
while statement flowchart
7
expr Statement 1
Statement 2
Entry
True
False
Exit
![Page 8: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/8.jpg)
Example 1: พมพเลข 1 ถง 10 โดยใช while loop
8
Print count from 1 to 10
1 2 3 4 5 6 7 8 9 10
![Page 9: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/9.jpg)
Example 2: เลอกการทางานของ ATM จากตวเลข Menu ทกาหนดไว หากเลอกตวเลขนอกเหนอทกาหนดไว จะตองใสตวเลขทเลอกใหม (while)
9
![Page 10: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/10.jpg)
Example 2: Output
10
1. Withdraw
2. Show Balance
3. Transfer
Enter your choice (1-3): 5
Please input your choice correctly.
1. Withdraw
2. Show Balance
3. Transfer
Enter your choice (1-3): 1
Withdraw
![Page 11: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/11.jpg)
do while Statement
จะแตกตางจาก while Statement ตรงทโปรแกรมจะเรมทางานตาม Statement ทระบไว โดยไมมการเชคเงอนไขกอน จากนนเมอทางานเสรจแลวหนงรอบถงจะเชคเงอนไข หากเงอนไขเปนจรง จะวนกลบไปทาซา แตหากเงอนไขเปนเทจจะจบการทางานแลวออกจาก Loop ทนท
11
do {
statement1;
statement2;
...
statementN;
} while (condition);
![Page 12: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/12.jpg)
do while statement flowchart
12
expr
Statement 1
Statement 2
Entry
True
FalseExit
![Page 13: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/13.jpg)
Example 3: พมพเลข 1 ถง 10 โดยใช do while loop
13
Print count from 1 to 10
1 2 3 4 5 6 7 8 9 10
![Page 14: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/14.jpg)
Example 4: เลอกการทางานของ ATM จากตวเลข Menu ทกาหนดไว หากเลอกตวเลขนอกเหนอทกาหนดไว จะตองใสตวเลขทเลอกใหม (do-while)
14
![Page 15: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/15.jpg)
Example 4: Output
15
1. Withdraw
2. Show Balance
3. Transfer
Enter your choice (1-3): 4
Please input your choice correctly.
1. Withdraw
2. Show Balance
3. Transfer
Enter your choice (1-3): 2
Show Balance
![Page 16: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/16.jpg)
for Statement
ในการเขยนโปรแกรม เมอตองการใหมการประมวลผลซา (Loop) โดยทเราทรำบจ ำนวนของกำรท ำซ ำ เราสามารถใช for Loop แทน while Loop ได
เชน ทาการคานวณคาเฉลยของกลมตวเลขจานวน 10 คา
16
แตกยงสามารถใชงาน while หรอ do while ไดอยเชนกน
![Page 17: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/17.jpg)
for statement syntax
17
for (initial; condition; incrementing) {
statement 1;
statement 2;
…
statement n;
}
เมอ
initial: การกาหนดคาเรมตนตวแปรทใชในการคมการวนรอบ
condition: เงอนไขของการวนรอบ
incrementing: การเปลยนแปลงคาของตวแปรควบคมแตละรอบ
for จะแตกตางจาก while และ do while ทใน while(condition) จะมเพยงเงอนไขทจะทาซาเทานนสวนคาเรมตนและการเปลยนแปลงคาของตวแปรควบคมการทาซาจะอยทอน เชนการกาหนดคาเรมตนจะอย ภายนอกกอนเขา Loop หรอ การเปลยนแปลงคาของตวแปรอยภายใน Statement ทอยในลป เปนตน
![Page 18: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/18.jpg)
for statement syntax [cont.]
สามารถเปรยบเทยบกบการใช while ไดดงนinitial;
while (condition) {
statement1;
…
statementn;
incrementing;
}
18
![Page 19: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/19.jpg)
for statement flowchart
ตวอยาง สาหรบ
for (counter = 1; counter <=10; counter = counter+1) {
statement;
}
19
Entry
counter = 1
counter <= 10
statement
counter = counter + 1
Exit
True
False
![Page 20: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/20.jpg)
Example 5: พมพเลข 1 ถง 10 โดยใช for loop
20
Print count from 1 to 10
1 2 3 4 5 6 7 8 9 10
![Page 21: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/21.jpg)
ตวดาเนนการสาหรบกาหนดคาแบบยอ
ในการเพมคาของตวแปรควบคมแตละรอบ หรอสวนสดทายของคาสงใน while, do while, for เราสามารถใชตวดาเนนการสาหรบกาหนดคา (Assignment Operator) แบบยอได
21
ตวดาเนนการ ตวอยางการใชงาน ความหมาย คาทได
+= c += 7 c = c + 7 c เทากบ 10
-= d -= 4 d = d – 4 d เทากบ 1
*= e *= 5 e = e * 5 e เทากบ 20
/= f /= 3 f = f / 3 f เทากบ 2
%= g %= 9 g = g % 9 g เทากบ 3
เมอกาหนดให c = 3, d = 5, e = 4, f = 6, g = 12
![Page 22: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/22.jpg)
ตวดาเนนการสาหรบกาหนดคาแบบยอ [cont.] นอกการจากนน ยงมตวดาเนนการ ++ และ – สาหรบเพมและลดคาทละ 1 การ
ใชงานมสองแบบ คอ วางขางหนา และ วางขางหลงตวแปร หากวางขางหนาตวแปร ตวแปรจะถกเพมหรอลดคากอน จะถกนาคาไปใชงาน หากวางขางหลงตวแปร ตวแปรจะถกคาไปใชงานกอน แลวจงถกเพมหรอลดคา
22
ตวดาเนนการ ตวอยางการใชงาน ความหมาย
++ ++a เพมคา a ขน 1 แลวจงนาคา a ไปใช
++ a++ ใชคา a ปจจบน แลวเพมคา a ขน 1
-- --b ลดคา b ลง 1 แลวจงนาคา b ไปใช
-- b-- ใชคา b ปจจบน แลวลดคา b ลง 1
![Page 23: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/23.jpg)
คาสง break และ continue
break และ continue ใชในการเปลยนแปลงเสนทางการทางานของโปรแกรม
แตหากเปนไปได ควรหลกเลยงการใชงาน break และ continue เพอใหโปรแกรมมโครงสรางทด
23
![Page 24: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/24.jpg)
break statement
การใชงาน break ในประโยคคาสงทาซา while, do while, for หรอ switch จะทาใหออกจากประโยคคาสงทาซาทนท
จะใช break เพอใหโปรแกรมกระโดดขามไปทางานในสวนทเหลอทอยนอก Loop ทนท
24
![Page 25: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/25.jpg)
Example 6: เปนการเขยนโปรแกรม แสดง counter โดยใช for loop หาก counter มคาเทากบ 5 จะตองออกจาก for loop ทนท
25
1 2 3 4
Broke out of loop at x = 5
![Page 26: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/26.jpg)
continue statement
การใชงาน continue ในประโยคคาสงทาซา while, do while, for จะมผลทาใหการทางานกระโดดขามคาสงทเหลออยใน Loop แลวกลบไปเรมทางานในรอบตอไปใหมทนท
สาหรบ while และ do while เงอนไขของการวนรอบ จะถกทดสอบทนท
สาหรบ for ตวแปรนบจะถก ลด/เพม คา แลวทาการทดสอบเงอนไขการวนรอบ
26
![Page 27: บทที่ 7 การเขียนโปรแกรม แบบวนรอบท้าซ ้า (Loop) · Enter your choice (1-3): 5 Please input your choice correctly](https://reader034.vdocuments.net/reader034/viewer/2022042320/5f09746b7e708231d426e91e/html5/thumbnails/27.jpg)
Example 7: เปนการเขยนโปรแกรมแสดง counter โดยใช for loop แตเมอ counter มคาเทากบ 5 จะใหกระโดดขามคาสง printf ออกไป แลวไปเพม counter จากการทางานของ for loop ทนท
271 2 3 4 6 7 8 9 10
Used continue to skip printing the value 5