The while loop

Download Report

Transcript The while loop

Chapter 5
การทาซ้ าโดย
while loop และ do while loop
1
Agenda
•
•
•
•
while loop
do while loop
Mini debug for while loop
Lab
2
loop
• ในการเขียนโปรแกรม จะมีการประมวลผลซ้า (loop หรือ
iterate) เพือ
่ ให ้ โปรแกรมทางานตาม statement หรือการ
ประมวลผล ทีก
่ าหนดไว ้ ซ้ามากกว่า 1 ครัง้
โดยไม่จาเป็ นที่
จะต ้องเขียน statement นัน
้ ซ้าไปซ้ามาในโค ้ด
• การทางานจะทางานตาม statement ไปจนหมด แล ้ว ถ ้าหาก
เงือ
่ นไขทีก
่ าหนดไว ้ให ้ทาซ้า ยังเป็ นจริง โปรแกรมจะวนกลับไป
ทางานตาม statement อีกรอบ จนกว่า เงือ
่ นไขทีก
่ าหนดไว ้จะ
เป็ นเท็จ
3
สว่ นประกอบของการ iteration
•
มีอยู่ 3 ประเภท
1. Initialization คือ การกาหนดค่าเริม
่ ต ้นของตัวแปรทีจ
่ ะเป็ นเงือ
่ นไขใน
การ iteration
•
่ x=1
เชน
2. Testing คือ การทดสอบว่า เงือ
่ นไขทีท
่ าการ iteration นัน
้ ยังเป็ นจริง
หรือไม่ จะมีการทา iteration ไปเรือ
่ ยๆ หากเงือ
่ นไขยังเป็ นจริง
•
่ x > 99
เชน
้ นเงือ
3. Incrementing เป็ นการเปลีย
่ นแปลงค่าของตัวแปรทีใ่ ชเป็
่ นไขใน
การ iteration
•
่ x=x+1
เชน
4
ประเภทของ Iteration Statement
• คือ วิธก
ี ารทาให ้โปรแกรมทางานเป็ น loop ได ้
• ในภาษา C มีอยู่ 4 ชนิดด ้วยกัน
1.
2.
3.
4.
while statement
do-while statement
for statement
break and continue statement
5
while statement
6
while statement
• มีการตรวจสอบเงือ
่ นไขก่อน หากเงือ
่ นไขเป็ นจริง โปรแกรมจะ
เริม
่ ทาตาม statement ทีก
่ าหนดไว ้ แล ้วกลับไปเริม
่ ต ้นใหม่
จนกว่าเงือ
่ นไขจะเป็ นไม่เป็ นจริง ถึงจะหยุดการทางาน แล ้ว
ออกไปจาก loop
7
syntax
while (condition)
{
statement1;
statement2;
…
statementN;
}
8
flowchart
Entry
Statement 2
True
expr
Statement 1
False
Exit
9
Example
Example 1
พิมพ์เลข 1 ถึง 10 โดยใช ้ while loop
Example 2
เลือกการทางานจากตัวเลข menu ทีก
่ าหนดไว ้ หากเลือกตัวเลขนอกเหนือที่
่ วั เลขทีเ่ ลือกใหม่
กาหนดไว ้ จะต ้อง ใสต
10
Example 1
#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
1 2 3 4 5 6 7 8 9 10
ตัวอยาง
output จากการทางาน
่
11
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");
break;
case 3:
printf("Display spelling errors\n");
break;
default:
/* do nothing */
break;
}
}
return (0);
}
12
Example 2
1. Check spelling
2. Correct spelling errors
3. Display spelling errors
Enter your choice (1-3): 5
1. Check spelling
2. Correct spelling errors
3. Display spelling errors
Enter your choice (1-3): 1
Check spelling
ตัวอยาง
output จากการทางาน
่
13
do while statement
14
Do while statement
• จะแตกต่างจาก while statement โดยที่ โปรแกรมจะเริม
่
็
ทางานตาม statement ทีร่ ะบุไว ้เลย โดยไม่มก
ี ารเชค
็ เงือ
condition ก่อน จากนัน
้ เมือ
่ ทางานเสร็จแล ้วถึงจะเชค
่ นไข
หากเงือ
่ นไขเป็ นจริง จะทาการทาซ้า แต่หากเงือ
่ นไขเป็ นเท็จจะ
จบการทางานแล ้วออกจาก loop
15
syntax
do
{
statement1;
statement2;
…
statementN;
} while (condition);
16
flowchart
Entry
Statement 1
Statement 2
True
expr
False
Exit
17
Example
Example 1
พิมพ์เลข 1 ถึง 10 โดยใช ้ while loop
Example 2
เลือกการทางานจากตัวเลข menu ทีก
่ าหนดไว ้ หากเลือกตัวเลขนอกเหนือที่
่ วั เลขทีเ่ ลือกใหม่
กาหนดไว ้ จะต ้อง ใสต
18
Example 1
/* 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
1 2 3 4 5 6 7 8 9 10
ตัวอยาง
output จากการทางาน
่
19
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");
break;
case 3:
printf("Display spelling errors\n");
break;
default:
/* do nothing */
break;
}
} while (a != 1 && a != 2 && a !=3);
return (0);
}
20
Example 2
1. Check spelling
2. Correct spelling errors
3. Display spelling errors
Enter your choice (1-3): 5
1. Check spelling
2. Correct spelling errors
3. Display spelling errors
Enter your choice (1-3): 1
Check spelling
ตัวอยาง
output จากการทางาน
่
21
Null statement
้
• เราสามารถใชงาน
empty statement
(null statement) ใน while
statement ได ้ โดยใช ้ ; ต่อท ้าย หรือ ในบรรทัดใหม่
– เนือ
่ งจากมีการทางาน
ได ้ทาไปแล ้วในเงือ
่ นไขของ
while
จึงไม่
จาเป็ นต ้องมีการทางานอีกใน loop
– ตัวอย่าง
while ((c = getchar()) == ‘ ’) ;
หรือ
while ((c = getchar()) == ‘ ’)
;
ทางานเหมือน
}
while ((c = getchar()) == ‘ ’) {
/* do nothing */
22
Null statement
นั่นคือ blank character ใน input stream จะถูกข ้าม (เมือ
่ ตัว input stream
เป็ น blank character จะเข ้า loop ได ้ แล ้วจะไม่ทาอะไร แต่จะไปรับ ค่า input
ใหม่อก
ี ครัง้ เรือ
่ ยๆ จนกว่าจะไม่ใช ่ blank character)
หากต ้องการให ้ white space ถูกข ้ามไป สามารถใช ้ code ดังนี้
while ((c = getchar()) == ‘ ’ || c == ‘\n’ || c == ‘\t’);
23
Mini debugging
• ปั ญหาทีพ
่ บได ้บ่อยจากการใช ้ loop
– infinity loop คือ โปรแกรมทางานตาม statement ไปเรือ
่ ยๆ โดยไม่
สามารถออกจาก loop ได ้
– โปรแกรม ไม่สามารถทางาน ใน loop ได ้
• แนวทางการแก ้ไข
– ตรวจสอบ condition ทีก
่ าหนด ว่า เป็ น จริง หรือ เป็ น เท็จ เสมอ หรือไม่
้
– ตรวจสอบค่าของตัวแปรทีใ่ ชงาน
ว่ามีการเปลีย
่ นแปลงค่าได ้หรือไม่
้
– ตรวจสอบ ว่า ค่าของตัวแปรทีใ่ ชในเงื
อ
่ นไข สามารถทาให ้ condition
เป็ น จริง หรือ เท็จได ้หรือไม่
24
Example
while (x = 5)
้ อ
หากใชเงื
่ นไขดังนี้ statement ทีอ
่ ยูใ่ น loop while จะ วน
ต่อไปเรือ
่ ยๆ ไม่สามารถออก loop ได ้ (infinity loop)
– ค่าทีไ่ ด ้จาก (x = 5) คือ 1 คือ หมายความว่า เรากาหนดค่า 1 ให ้กับตัว
แปร x ได ้สาเร็จ แต่ไม่ได ้เป็ นการเปรียบเทียบว่า x เท่ากับ 5 หรือไม่
25