• user warning: Duplicate entry '536306482' for key 'PRIMARY' query: INSERT INTO accesslog (title, path, url, hostname, uid, sid, timer, timestamp) values('สิทธิผู้บริโภค 5 ประการ', 'node/46639', '', '18.189.189.102', 0, '378ec27a1fb87cbb94a3f8af11f82575', 180, 1716110592) in /home/tgv/htdocs/modules/statistics/statistics.module on line 63.
  • user warning: Table 'cache_filter' is marked as crashed and should be repaired query: SELECT data, created, headers, expire, serialized FROM cache_filter WHERE cid = '3:33232a38a3a3534e0b76e64d664f54a4' in /home/tgv/htdocs/includes/cache.inc on line 27.
  • user warning: Table 'cache_filter' is marked as crashed and should be repaired query: UPDATE cache_filter SET data = '<!--paging_filter--><p><img src=\"/files/u20858/head4.png\" height=\"81\" width=\"600\" /><br />\n<b>2. กลุ่มคำสั่ง Iteration</b><br />\nเป็นคำสั่งที่สั่งให้เครื่องคอมพิวเตอร์ประมวลผลเหตุการณ์ที่ต้องการซ้ำ ๆ มากกว่าหนึ่งครั้งโดยการตรวจสอบเงื่อนไข เป็นกลุ่มคำสั่งที่ใช้สั่งให้ทำซ้ำ มี 3 รูปแบบ คือ For … Next, While, Do … While ซึ่งแต่ละรูปแบบจะมีการทำซ้ำที่แตกต่างกันไป<br />\n<br />\n<b><img src=\"/files/u20858/head7.png\" style=\"margin: 0px; border-width: 0px; padding: 0px\" height=\"81\" width=\"600\" /><br />\n2.1 while</b></p>\n<div>\nวงรอบ WHILE เป็นฟังก์ชันที่ใช้ในการวนรอบการทำงาน โดย\n</div>\n<div>\n1. จะตรวจสอบเงื่อนไขตามที่กำหนดก่อน 2. ถ้าเงื่อนไขเป็นจริงจะทำงานตามคำสั่งที่อยู่ภายใต้เครื่องหมายปีกกา\n</div>\n<div>\n3. เมื่อปฏิบัติคำสั่งเสร็จสิ้นแล้ว จะวนกลับไปตรวจสอบเงื่อนไขอีกครั้ง\n</div>\n<div>\n4. จะหยุดทำซ้ำเมื่อเงื่อนไขที่ตรวจสอบเป็นเท็จ ถ้าเงื่อนไขเป็นเท็จโปรแกรมจะไปทำคำสั่งหลังเครื่องหมายปีกกาปิด นอกชุดคำสั่ง while\n<table cellPadding=\"1\" cellSpacing=\"1\" border=\"1\" style=\"border-width: 0px; border-color: #6929fe\" width=\"500\">\n<tbody>\n<tr>\n<td style=\"text-align: center\"><b>รูปแบบฟังก์ชั่น while</b></td>\n<td style=\"text-align: left\"><b>ผังงาน</b></td>\n</tr>\n<tr>\n<td width=\"150\">\n<div style=\"text-align: left\">\n while (condition){\n </div>\n<div style=\"text-align: left\">\n statement;\n </div>\n<div style=\"text-align: left\">\n }\n </div>\n<div style=\"text-align: center\">\n </div>\n</td>\n<td align=\"center\">\n<div style=\"text-align: center\">\n <img src=\"/files/u20858/while.png\" height=\"244\" width=\"354\" />\n </div>\n<p> ภาพที่ 2.1 while</p></td>\n</tr>\n</tbody>\n</table>\n<p>\n<img src=\"/files/u20858/head18.png\" height=\"81\" width=\"600\" /> </p>\n<table cellPadding=\"1\" cellSpacing=\"1\" border=\"1\" style=\"border-width: 0px; border-color: #6929fe\" width=\"500\">\n<tbody>\n<tr>\n<td style=\"text-align: center\"><b>ตัวอย่างโปรแกรม while</b></td>\n<td style=\"text-align: center\"><b>ผลของโปรแกรม</b></td>\n</tr>\n<tr>\n<td>int x = 1;<br />\n while(x &lt;= 5){<br />\n printf(&quot;%d\\n&quot;,x);<br />\n x++;<br />\n }\n </td>\n<td style=\"text-align: center\">1<br />\n 2<br />\n 3<br />\n 4<br />\n 5\n </td>\n</tr>\n</tbody>\n</table>\n<p>\n<img src=\"/files/u20858/head6.png\" style=\"margin: 0px; border-width: 0px; padding: 0px\" height=\"81\" width=\"600\" /><br />\n<b>2.2 do - while</b><br />\nวงรอบ DO WHILE เป็นฟังก์ชันที่ใช้ในการวนรอบการทำงาน โดย<br />\n1. จะทำงานตามคำสั่งที่ระบุภายในเครื่องหมายปีกกาก่อน 1 รอบ<br />\n2. จะตรวจสอบเงื่อนไขตามที่กำหนด ถ้าเงื่อนไขเป็นจริงจะวนไปทำงานซ้ำจนกว่าเงื่อนไขที่กำหนดไว้จะเป็นเท็จ</p>\n<table cellPadding=\"1\" cellSpacing=\"1\" border=\"1\" style=\"border-width: 0px; border-color: #6929fe\" width=\"500\">\n<tbody>\n<tr>\n<td style=\"text-align: center\"><b>รูปแบบคำสั่ง do-while</b></td>\n<td style=\"text-align: center\"><b>ผังงาน</b></td>\n</tr>\n<tr>\n<td width=\"150\">do{<br />\n statement;<br />\n }while (condition);</td>\n<td align=\"center\"><img src=\"/files/u20858/do-while.png\" height=\"354\" width=\"273\" /><br />\n ภาพที่ 2.2 do-while</td>\n</tr>\n</tbody>\n</table>\n<p>\n<img src=\"/files/u20858/head18.png\" height=\"81\" width=\"600\" /> </p>\n<table cellPadding=\"1\" cellSpacing=\"1\" border=\"1\" style=\"border-width: 0px; border-color: #6929fe\" width=\"500\">\n<tbody>\n<tr>\n<td style=\"text-align: center\"><b>ตัวอย่างโปรแกรม do-while</b></td>\n<td style=\"text-align: center\"><b>ผลของโปรแกรม</b></td>\n</tr>\n<tr>\n<td>int x = 1;<br />\n do{<br />\n printf(&quot;%d\\n&quot;,x);<br />\n x++;<br />\n }while(x &lt;= 5)\n </td>\n<td style=\"text-align: center\">1<br />\n 2<br />\n 3<br />\n 4<br />\n 5\n </td>\n</tr>\n</tbody>\n</table>\n<p>\n<b><img src=\"/files/u20858/head5.png\" style=\"margin: 0px; border-width: 0px; padding: 0px\" height=\"81\" width=\"600\" /><br />\n2.3 for next</b><br />\nวนรอบ FOR จะทำซ้ำจนกว่าค่าของตัวแปรที่ตั้งจะครบตามเงื่อนไขที่ต้องการ<br />\n1. จะเพิ่มหรือลดค่าทีละหนึ่งไปเรื่อยๆ<br />\n2. ถ้าเงื่อนไขเป็นจริงจะทำตามประโยคคำสั่งภายในเครื่องหมายปีกกา<br />\n3. เมื่อตรวจสอบเงื่อนไขแล้วพบว่าเงื่อนไขเป็นเท็จก็จะหยุดการทำงาน แล้วออกไป<br />\nทำประโยคคำสั่งหลังเครื่องหมายปีกกาปิด </p>\n<table cellPadding=\"1\" cellSpacing=\"1\" border=\"1\" style=\"border-width: 0px; border-color: #6929fe\" width=\"500\">\n<tbody>\n<tr>\n<td style=\"text-align: center\"><b>รูปแบบคำสั่ง for</b></td>\n<td style=\"text-align: center\"><b>ผังงาน</b></td>\n</tr>\n<tr>\n<td width=\"150\">\n<p>\n for(initialization; condition; increment or decrement)<br />\n {<br />\n statement;<br />\n }</p>\n<p> หรือ\n </p>\n<p>\n for ( ; ; ;) {<br />\n block;<br />\n }\n </p>\n</td>\n<td align=\"center\"><img src=\"/files/u20858/for.png\" height=\"303\" width=\"388\" /><br />\n ภาพที่ 2.2 do-while</td>\n</tr>\n</tbody>\n</table>\n<div>\n<br />\n<img src=\"/files/u20858/head18.png\" height=\"81\" width=\"600\" /> \n<table cellPadding=\"1\" cellSpacing=\"1\" border=\"1\" style=\"border-width: 0px; border-color: #6929fe\" width=\"500\">\n<tbody>\n<tr>\n<td style=\"text-align: center\"><b>ตัวอย่างโปรแกรม for</b></td>\n<td style=\"text-align: center\"><b>ผลของโปรแกรม</b></td>\n</tr>\n<tr>\n<td>for(i = 1;i &lt;= 5;i++){<br />\n printf(&quot;%d\\n&quot;,i);<br />\n } </td>\n<td style=\"text-align: center\">1<br />\n 2<br />\n 3<br />\n 4<br />\n 5\n </td>\n</tr>\n</tbody>\n</table>\n<p>\n<b><span style=\"font-weight: normal\" class=\"Apple-style-span\"><img src=\"/files/u20858/head16.png\" style=\"margin: 0px; border-width: 0px; padding: 0px\" height=\"81\" width=\"600\" /><br />\n</span>คำสั่งประกอบที่เกี่ยวข้องที่ใช้ควบคุม</b> ได้แก่<br />\nbreak และcontinue<br />\n<b style=\"margin: 0px; padding: 0px\">a. break คือ </b>คำสั่งให้ออกจากลูปโดยหยุดการตรวจสอบเงื่อนไขถัดไป<br />\n<b style=\"margin: 0px; padding: 0px\">b. continue คือ </b>คำสั่งให้วนลูปถัดไป โดยไม่ทำคำสั่งที่เหลือในลูปนั้</p>\n<p><img src=\"/files/u20858/head8.png\" style=\"margin: 0px; border-width: 0px; padding: 0px\" height=\"81\" width=\"600\" /><br />\n<b>a. break</b><br />\nคำสั่งให้ออกจากลูปโดยหยุดการตรวจสอบเงื่อนไขถัดไป<br />\nint x=0, i=0;<br />\nwhile (i&lt;15) {<br />\nx++; <br />\nif ((x%5)==0) { break; }<br />\nprintf(&quot;i = &quot; + i + &quot;, x = &quot; +x);<br />\ni++;<br />\n} // end while<br />\n<br />\n<img src=\"/files/u20858/head9.png\" style=\"margin: 0px; border-width: 0px; padding: 0px\" height=\"81\" width=\"600\" /><br />\n<b>b. continue</b><br />\nคำสั่งให้วนลูปถัดไป โดยไม่ทำคำสั่งที่เหลือในลูปนั้น<br />\nint x=0, i=0;<br />\nwhile (i&lt;15) {<br />\nx++; <br />\nif ((x%5)==0) { continue; }<br />\nprintf(&quot;i=&quot; + i + &quot;, x=&quot; +x);<br />\ni++;<br />\n} // end while </p>\n<hr />\n</div></div>', created = 1716110612, expire = 1716197012, headers = '', serialized = 0 WHERE cid = '3:33232a38a3a3534e0b76e64d664f54a4' in /home/tgv/htdocs/includes/cache.inc on line 112.

เงื่อนไขและคำสั่งวนซ้ำ


2. กลุ่มคำสั่ง Iteration
เป็นคำสั่งที่สั่งให้เครื่องคอมพิวเตอร์ประมวลผลเหตุการณ์ที่ต้องการซ้ำ ๆ มากกว่าหนึ่งครั้งโดยการตรวจสอบเงื่อนไข เป็นกลุ่มคำสั่งที่ใช้สั่งให้ทำซ้ำ มี 3 รูปแบบ คือ For … Next, While, Do … While ซึ่งแต่ละรูปแบบจะมีการทำซ้ำที่แตกต่างกันไป


2.1 while

วงรอบ WHILE เป็นฟังก์ชันที่ใช้ในการวนรอบการทำงาน โดย
1. จะตรวจสอบเงื่อนไขตามที่กำหนดก่อน 2. ถ้าเงื่อนไขเป็นจริงจะทำงานตามคำสั่งที่อยู่ภายใต้เครื่องหมายปีกกา
3. เมื่อปฏิบัติคำสั่งเสร็จสิ้นแล้ว จะวนกลับไปตรวจสอบเงื่อนไขอีกครั้ง
4. จะหยุดทำซ้ำเมื่อเงื่อนไขที่ตรวจสอบเป็นเท็จ ถ้าเงื่อนไขเป็นเท็จโปรแกรมจะไปทำคำสั่งหลังเครื่องหมายปีกกาปิด นอกชุดคำสั่ง while
รูปแบบฟังก์ชั่น while ผังงาน
while (condition){
statement;
}

ภาพที่ 2.1 while

ตัวอย่างโปรแกรม while ผลของโปรแกรม
int x = 1;
while(x <= 5){
printf("%d\n",x);
x++;
}
1
2
3
4
5


2.2 do - while
วงรอบ DO WHILE เป็นฟังก์ชันที่ใช้ในการวนรอบการทำงาน โดย
1. จะทำงานตามคำสั่งที่ระบุภายในเครื่องหมายปีกกาก่อน 1 รอบ
2. จะตรวจสอบเงื่อนไขตามที่กำหนด ถ้าเงื่อนไขเป็นจริงจะวนไปทำงานซ้ำจนกว่าเงื่อนไขที่กำหนดไว้จะเป็นเท็จ

รูปแบบคำสั่ง do-while ผังงาน
do{
statement;
}while (condition);

ภาพที่ 2.2 do-while

ตัวอย่างโปรแกรม do-while ผลของโปรแกรม
int x = 1;
do{
printf("%d\n",x);
x++;
}while(x <= 5)
1
2
3
4
5


2.3 for next

วนรอบ FOR จะทำซ้ำจนกว่าค่าของตัวแปรที่ตั้งจะครบตามเงื่อนไขที่ต้องการ
1. จะเพิ่มหรือลดค่าทีละหนึ่งไปเรื่อยๆ
2. ถ้าเงื่อนไขเป็นจริงจะทำตามประโยคคำสั่งภายในเครื่องหมายปีกกา
3. เมื่อตรวจสอบเงื่อนไขแล้วพบว่าเงื่อนไขเป็นเท็จก็จะหยุดการทำงาน แล้วออกไป
ทำประโยคคำสั่งหลังเครื่องหมายปีกกาปิด

รูปแบบคำสั่ง for ผังงาน

for(initialization; condition; increment or decrement)
{
statement;
}

หรือ

for ( ; ; ;) {
block;
}


ภาพที่ 2.2 do-while

ตัวอย่างโปรแกรม for ผลของโปรแกรม
for(i = 1;i <= 5;i++){
printf("%d\n",i);
}
1
2
3
4
5


คำสั่งประกอบที่เกี่ยวข้องที่ใช้ควบคุม
ได้แก่
break และcontinue
a. break คือ คำสั่งให้ออกจากลูปโดยหยุดการตรวจสอบเงื่อนไขถัดไป
b. continue คือ คำสั่งให้วนลูปถัดไป โดยไม่ทำคำสั่งที่เหลือในลูปนั้


a. break
คำสั่งให้ออกจากลูปโดยหยุดการตรวจสอบเงื่อนไขถัดไป
int x=0, i=0;
while (i<15) {
x++;
if ((x%5)==0) { break; }
printf("i = " + i + ", x = " +x);
i++;
} // end while


b. continue
คำสั่งให้วนลูปถัดไป โดยไม่ทำคำสั่งที่เหลือในลูปนั้น
int x=0, i=0;
while (i<15) {
x++;
if ((x%5)==0) { continue; }
printf("i=" + i + ", x=" +x);
i++;
} // end while


สร้างโดย: 
ชัยฤทธิ์ ตอนปัญญา

มหาวิทยาลัยศรีปทุม ผู้ใหญ่ใจดี
 

 ช่วยด้วยครับ
นักเรียนที่สร้างบล็อก กรุณาอย่า
คัดลอกข้อมูลจากเว็บอื่นทั้งหมด
ควรนำมาจากหลายๆ เว็บ แล้ววิเคราะห์ สังเคราะห์ และเขียนขึ้นใหม่
หากคัดลอกทั้งหมด จะถูกดำเนินคดี
ตามกฎหมายจากเจ้าของลิขสิทธิ์
มีโทษทั้งจำคุกและปรับในอัตราสูง

ช่วยกันนะครับ 
ไทยกู๊ดวิวจะได้อยู่นานๆ 
ไม่ถูกปิดเสียก่อน

ขอขอบคุณในความร่วมมือครับ

อ่านรายละเอียด

ด่วน...... ขณะนี้
พระราชบัญญัติลิขสิทธิ์ (ฉบับที่ 2) พ.ศ. 2558 
มีผลบังคับใช้แล้ว 
ขอให้นักเรียนและคุณครูที่ใช้งาน
เว็บ thaigoodview ในการส่งการบ้าน
ระมัดระวังการละเมิดลิขสิทธิ์ด้วย
อ่านรายละเอียดที่นี่ครับ

 

สมาชิกที่ออนไลน์

ขณะนี้มี สมาชิก 0 คน และ ผู้เยี่ยมชม 268 คน กำลังออนไลน์