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

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

Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

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


งานนำเสนอเรื่อง: "Computer Programming การเขียนโปรแกรมคอมพิวเตอร์"— ใบสำเนางานนำเสนอ:

1 Computer Programming การเขียนโปรแกรมคอมพิวเตอร์
สัปดาห์ที่ 6 คำสั่งควบคุมการทำงานแบบเงื่อนไขและคำสั่งควบคุมการทำงานแบบวนซ้ำ

2 objectives เพื่อให้นิสิตรู้จักและเข้าใจคำสั่งควบคุมการทำงานแบบวนซ้ำในภาษาซี สามารถเขียนโปรแกรมภาษาซีโดยใช้คำสั่งเพื่อให้คอมพิวเตอร์ทำงานแบบวนซ้ำได้ สามารถเขียนโปรแกรมภาษาซีให้มีทำงานแบบวนซ้ำและกำหนดเงื่อนไขร่วมกันได้ สามารถนำความรู้เรื่องคำสั่งควบคุมการทำงานแบบวนซ้ำและกำหนดเงื่อนไขไปประยุกต์เพื่อแก้ไขโจทย์ปัญหาในชีวิตประจำวันได้ได้อย่างถูกต้องเหมาะสม

3 Outline 1 Review the Last Topic 2 For Loop 3 While Loop 4
Do-While Loop 5 Assignment #4

4 C Programming Structure
พรีโปรเซสเซอร์ไดเร็คทีฟ #include<file.h> type function_name(type); type variable; int main() { statement-1; ... statement-n; return 0; } type function_name(type variable) return(var); ฟังก์ชันโพรโทรไทพ์ ส่วนหัวโปรแกรม ตัวแปรชนิดโกบอล ตัวแปรชนิดโลคอล ฟังก์ชันหลัก คำสั่ง ส่วนตัวโปรแกรม ฟังก์ชันย่อย

5 Condition Control Statement
คำสั่งควบคุมการทำงานแบบเงื่อนไข คือ การเขียนโปรแกรมให้มีการตัดสินใจสามารถเลือกได้ว่าจะทำหรือไม่ทำตามคำสั่ง โดยขึ้นอยู่กับเงื่อนไขที่กำหนด ผลจากการเปรียบเทียบเงื่อนไขจะให้ผลลัพธ์เป็นจริงหรือเท็จ ถ้าผลเป็นจริงให้ทำงานตามคำสั่งด้านที่เงื่อนไขเป็นจริง ถ้าเป็นเท็จให้ทำตาม คำสั่งด้านที่เงื่อนไขเป็นเท็จ ซึ่งคำสั่งควบคุมการทำงานแบบเงื่อนไขออกเป็น 4 ประเภท คือ if condition control statement if-else condition control statement if-else-if condition control statement Nested if condition control statement Switch condition control statement

6 if Condition Control Statement (1)
True False if (Boolean expression) statement; if (expression) statement; if (Condition) statement;

7 if Condition Control Statement (2)
True False Statement-2; if (Boolean expression) { statement-1; statement-2; ... statement-n; }

8 if-else Condition Control Statement (1)
if (Boolean expression) statement-1; else statement-2; การทำงานขึ้นอยู่กับเงื่อนไข ถ้าเป็นจริงไปทำงานด้านหนึ่ง ถ้าเป็นเท็จก็จะไปทำงานอีกอย่างหนึ่ง

9 If-else Condition Control Statement (2)
……… T F Statement-2.1; Statement-2.2; if (expression) { statement-1.1; statement-1.2; ... statement-1.n; } else statement-2.1; statement-2.2; statement-2.n;

10 if-else-if condition control statement (1)
if (expression-1) statement-1; else if (expression-2) statement-2; ... else if (expression-m) statement-m; else statement-m+1;

11 if-else-if condition control statement (2)
if (expression-1) { statement-1.1; ... statement-1.n; } else if (expression-2) statement-2.1; statement-2.n; ... else if (expression-m) { statement-m.1; statement-m.n; } else statement-m+1.1; statement-m+1.n;

12 if-else-if condition control statement (3)
เป็นรูปแบบการเลือกการทำงานที่มีทางเลือกมากกว่า 2 ทางขึ้นไป Condition กรณี 1 กรณี 2 กรณี 3 กรณี 4 Statement 1; Statement 2; Statement 3; Statement 4;

13 Nested if Condition Control Statement (1)
if (expression-1) statement-1; else if (expression-2) statement-2; if (expression-3) statement-3; statement-4;

14 statements to be executed if test expression is true;
else if(test expression 1) statements to be executed if test expressions 1 is true; if (test expression 2) . statements to be executed if all test expressions are false;

15 Nested if Condition Control Statement (2)
True False Condition 2 Statement 2; Condition 3 Statement 3; Statement 4; เป็นรูปแบบการเลือกการทำงานที่มีทางเลือกมากกว่า 2 ทางขึ้นไป

16 Switch Condition Control Statement (1)
switch (expression-1) { case constant-expr-1: statement-1.1; statement-1.2; ... statement-1.n; break; case constant-expr-2: statement-2.1; statement-2.2; statement-2.n; ... case constant-expr-m: statement-m.1; statement-m.2; statement-m.n; break; default: statement-d.1; statement-d.2; statement-d.n; }

17 Switch Condition Control Statement (2)
กรณี 1 กรณี 2 กรณี 3 Default Statement 1-1; Statement 1-2; …. Statement 1-n; Statement 2-1; Statement 2-2; …. Statement 2-n; Statement 3-1; Statement 3-2; …. Statement 3-n; Statement m-1; Statement m-2; …. Statement m-n;

18 Why need iteration loop?
เหตุการณ์ที่เกิดขึ้นหลายรอบ เช่น โปรแกรมแสดง ชื่อ 20 ครั้ง เหตุการณ์ที่เกิดขึ้นหลายรอบ โดยมีการเปลี่ยนแปลงค่า หรือมีเงื่อนไข เช่น แสดงผลเลข 0, 1, 2, … , 10 แสดงผลรวมของ 1,3,5,7, … , 99 แสดง ชื่อ ไปเรื่อย ๆ จนกว่าค่า X จะมากกว่า 30

19 Example 1 Output Analysis แสดงผลเลข 0, 1, 2, … , 10 Input Analysis
ไม่มี Process Analysis โปรแกรมแสดงผลเลข 0, 1, 2, … ,10 Variable Define ไม่ใช้ (หรือใช้ count เพื่อเพิ่มค่า)

20 Example 1 (cont.) #include<stdio.h> #include<conio.h>
int main() { printf ("0\t"); printf ("1\t"); printf ("2\t"); printf ("3\t"); printf ("4\t"); ... printf ("10\t"); return 0; } #include<stdio.h> #include<conio.h> int main() { int count = 0; printf ("%d\t",count++); ... return 0; }

21 Outline 1 Review the Last Topic 2 For Loop 3 While Loop 4
Do-While Loop 5 Assignment #4

22 Structure of Iteration Loop
การกำหนดค่าเริ่มต้น (Initialization) Initialization False การทดสอบเงื่อนไข (Condition) Condition True การทำงานภายในวงรอบ (Execution) Statement การปรับปรุงค่า (Modification) Modification

23 Pretest Loop ตรวจสอบเงื่อนไขก่อน

24 Posttest Loop ตรวจสอบเงื่อนไขทีหลัง

25 Type of Iteration Loop คำสั่งวนซ้ำแบบ Pretest loop 1. while loop
2. for loop คำสั่งวนซ้ำแบบ Posttest loop 3. do-while loop

26 For Loop (cont.) initial เป็นส่วนที่ใช้กำหนดค่าเริ่มต้นให้กับตัวแปร
for (initial; expression; modification) { statement-1.1; statement-1.2; ... statement-1.n; } initial เป็นส่วนที่ใช้กำหนดค่าเริ่มต้นให้กับตัวแปร expression หรือ condition เป็นเงื่อนไขเพื่อพิจารณา modification เป็นส่วนที่เปลี่ยนแปลงค่าตัวแปร statement-1, 2, ... , n เป็นคำสั่งที่จะทำงานเมื่อเงื่อนไขเป็นจริง

27 For Loop (cont.) False True Statement -1-n Modification Initialization
Condition Initialization Statement -1-n Modification True False

28 For Loop (cont.) รูปแบบประโยค
for(expression1 ; expression2 ; expression3) statement; หรือ {statements;} คำอธิบาย ทำ expression1 เพียง 1 ครั้ง ทำ expression2 แล้วตรวจสอบค่าว่าเป็นจริงหรือเท็จ ถ้าเป็นจริงจะทำ statement ใน loop แล้วทำ expression3 ต่อ วนกลับ ไปทำ expression2 ถ้าเป็นเท็จจะไม่ทำ statement ใน loop

29 Example 2 Output Analysis แสดงผลเลขรวมของตัวเลขจาก 0-100
Input Analysis ไม่มี Process Analysis โปรแกรมทำการบวกค่าเก็บไว้ในตัวแปรผลลัพธ์ แล้วเพิ่มค่าจนถึง 100 Variable Define count เป็นตัวแปรชนิดจำนวนเต็มเพื่อนับจำนวน sum เป็นจำนวนเต็มเพื่อเก็บค่าผลรวม

30 Example 2 (cont.) START sum=0 count=1 False True sum=sum+count count++
END sum=sum+count count=1

31 Example 2 (cont.) #include<stdio.h> int main() {
int sum=0, count; for (count=1; count<=100; count++) sum = sum + count ; } printf ("Summation of 1 to 100 = %d",sum); return 0;

32 Example 3 Output Analysis แสดงผล a – z ทางจอภาพ Input Analysis ไม่มี
Example: จงเขียนผังงานและโปรแกรมสำหรับแสดงผลอักษร a – z ออกทางจอภาพ โดยใช้คำสั่ง for Output Analysis แสดงผล a – z ทางจอภาพ Input Analysis ไม่มี a b c d e f g h i j k l m n o p q r s t u v w x y z

33 Example 3 (cont.) Process Analysis
โปรแกรมทำการวนรอบเพื่อแสดงผลอักษรตั้งแต่ a – z โดยการเพิ่มค่าตัวแปรขึ้นครั้งละ 1 (ดูตาราง ASCII Code) Variable Define letter เป็นตัวแปรชนิดอักขระ printf ("%c\n",'a'); // printf ("%c\n",97); a printf ("%c\n",'a'+1); // printf ("%c\n",98); b printf ("%c\n",'a'+2); // printf ("%c\n",98); c

34 Example 3 (cont.) START letter letter='a' letter<='z' True False
END letter='a'

35 Example 3 (cont.) #include<stdio.h> int main() { char letter;
for (letter='a'; letter<='z'; letter++) printf ("%c ",letter); } return 0;

36 Example 4 Example: จงเขียนผังงานและโปรแกรมแสดงผลรูปสี่เหลี่ยมขนาด n x n โดยโปรแกรมจะรอรับจำนวนเต็มจากผู้ใช้งาน ดังตัวอย่าง Please enter number : 4 Output **** Please enter number : 9 Output *********

37 Example 4 (cont.) Output Analysis
ผลตัวเลข เป็นรูปสี่เหลี่ยมจัตุรัสขนาดเท่ากับจำนวนตัวเลขที่รับเข้ามา Input Analysis เลขจำนวนเต็มที่ผู้ใช้ป้อนเข้ามา Process Analysis โปรแกรมรอรับค่าจำนวนเต็มจากผู้ใช้งาน โปรแกรมวนรอบเพื่อทำการแสดง '*' เป็นรูปสี่เหลี่ยมจัตุรัส

38 Example 4 (cont.) บรรทัดที่ 1 แสดงผล '\n' แล้วแสดงผล '*' จำนวนเท่ากับค่าที่รับมา บรรทัดที่ 2 แสดงผล '\n' แล้วแสดงผล '*' จำนวนเท่ากับค่าที่รับมา บรรทัดที่ n แสดงผล '\n' แล้วแสดงผล '*' จำนวนเท่ากับค่าที่รับมา Variable Define num เป็นจำนวนเต็มเพื่อใช้เก็บค่าตัวเลขที่ผู้ใช้ป้อน i เป็นจำนวนเต็มเพื่อใช้นับจำนวนบรรทัด j เป็นจำนวนเต็มเพื่อใช้นับจำนวน '*'

39 Example 4 (cont.) START num, i, j i<=num True False '\n' END i=1
j<=num j=1 j++ '*'

40 Example 4 (cont.) int main() { int num,i,j;
printf ("Enter number : "); scanf ("%d",&num); for (i=1; i<=num; i++) printf ("\n"); for (j=1; j<=num; j++) printf ("*"); } return 0;

41 Outline 1 Review the Last Topic 2 For Loop 3 While Loop 4
Do-While Loop 5 Assignment #4

42 While Loop False True Statement -1-n Modification Condition
while (expression) statement-1; while (expression) { statement-1.1; statement-1.2; ... statement-1.n; }

43 statement; หรือ {statements;}
While Loop (cont.) รูปแบบประโยค while(expression) statement; หรือ {statements;} คำอธิบาย ตรวจสอบเงื่อนไขในนิพจน์ (expression) ถ้าเป็นจริง (non-zero) จะทำ statement ในวงรอบ ถ้าเป็นเท็จ (zero) จะไม่ทำ statement ในวงรอบ

44 Example 5 Output Analysis แสดงผลเลข 0, 1, 2, … , 10 Input Analysis
Example: จงเขียนผังงานและโปรแกรมที่มีการควบคุมทิศทางแบบวนรอบ โดยใช้คำสั่ง while เพื่อแสดงตัวเลข ออกทางหน้าจอ Output Analysis แสดงผลเลข 0, 1, 2, … , 10 Input Analysis ไม่มี Process Analysis โปรแกรมทำงานแบบวนรอบ เพื่อแสดงผลเลข 0, 1, 2, … ,10 Variable Define count เป็นจำนวนเต็มเพื่อใช้นับจำนวนรอบ

45 Example 5 (cont.) count = 9 count = 10 count = 11 count = 3 count = 2
START count = 0 count<=10 True False count++ count END Show number from zero to ten 10 1 2 10

46 Example 5 (cont.) #include<stdio.h> int main() { int count = 0;
printf ("Show number from zero to ten\n\n"); while (count<=10) printf ("%d\t",count); count++; } return 0; Show number from zero to ten

47 Example 6 Output Analysis
Example: จงเขียนผังงานและโปรแกรมที่มีการควบคุมทิศทางแบบวนรอบโดยใช้คำสั่ง while เพื่อให้โปรแกรมทำการบวกเลขจำนวนเต็มตั้งแต่ 1 จนถึงค่าที่ผู้ใช้งานกำหนด Output Analysis ผลลัพธ์การบวกเลขจำนวนเต็ม ตั้งแต่ 1 ถึงค่าที่ผู้ใช้กำหนด Input Analysis ค่าที่ผู้ใช้งานป้อนเข้ามา

48 Example 6 (cont.) Process Analysis
โปรแกรมถามว่าผู้ใช้งานต้องการบวกเลขตั้งแต่ 1 ถึงเลขใด วนรอบแบบ while เพื่อบวกค่า แสดงผลลัพธ์ที่ได้ Variable Define sum = 0 ผลรวมของการบวก โดยเริ่มต้นมีค่าเท่ากับ 0 i = 1 ค่าที่นำเข้าไปบวกกับ sum ในแต่ละรอบ โดยรอบ แรกค่า i มีค่าเท่ากับ 1 และมีค่าเพิ่มขึ้นรอบละ 1 final เพื่อรับค่าจากผู้ใช้ และกำหนดจุดสิ้นสุดของค่า i

49 Example 6 (cont.) START i=1,final,sum=0 final True i<=final
False i++ sum END final sum = sum + i

50 Example 6 (cont.) #include<stdio.h> int main() {
int i = 1, final, sum=0; printf ("Enter final number : "); scanf ("%d",&final); while (i<=final) sum = sum + i; i++; } printf ("Sum = %d",sum); return 0;

51 Outline 1 Review the Last Topic 2 For Loop 3 While Loop 4
Do-While Loop 5 Assignment #4

52 Do-while Loop True False Statement -1.1; ….. Statement -1.n; Condition
while (expression); Do { statement-1.1; statement-1.2; ... statement-1.n; } while (expression);

53 Do-while Loop (cont.) รูปแบบประโยค do statement; หรือ {statements;}
while(expression); คำอธิบาย ทำ statement ใน loop ทำ expression แล้วตรวจสอบค่าว่าเป็นจริงหรือเท็จ ถ้าเป็นจริงจะกลับไปทำ statement ใน loop ถ้าเป็นเท็จจะไม่เข้าไปทำ statement ใน loop

54 Example 7 Example: จงเขียนผังงานและโปรแกรมสำหรับรวมเลขจำนวนเต็ม
ตั้งแต่ 1 – 100 โดยใช้คำสั่ง do-while Output Analysis ผลรวมของเลขจำนวนเต็ม ตั้งแต่ Input Analysis ไม่มี Process Analysis โปรแกรมทำการบวกค่าเก็บไว้ในตัวแปรผลลัพธ์ แล้วเพิ่มค่าจนถึง 100 Variable Define count เป็นตัวแปรชนิดจำนวนเต็มเพื่อนับจำนวน sum เป็นจำนวนเต็มเพื่อเก็บค่าผลรวม

55 Example 7 (cont.) START count=1, sum=0 sum=sum+count count++ True
False count++ sum END sum=sum+count

56 Example 7 (cont.) int main() { int count=1,sum=0; do
sum = sum + count; count++; } while(count<=100); printf ("Summation of 1 to 100 = %d",sum); return 0;

57 Outline 1 Review the Last Topic 2 For Loop 3 While Loop 4.1
Nested Loop 5 Assignment #4

58 Nested Loop ตามไวยากรณ์ของภาษาซีประโยควนซ้ำทุกชนิดสามารถทำงานในวงรอบซึ่งเป็นงานอะไรก็ได้ แม้แต่ประโยควนซ้ำเอง การเขียนประโยควนซ้ำไว้ภายในประโยควนซ้ำอีกทีทำให้เกิดลักษณะของประโยควนซ้ำซ้อนกัน (nested loop) มีประโยชน์ในการทำงานหลายอย่าง เช่น การจัดการกับข้อมูลที่มีลักษณะเหมือนตารางหรือเป็นแบบ 2 มิติ (อาเรย์ 2 มิติ)

59 Example 8 Example: การประยุกต์ใช้ประโยควนซ้ำแบบซ้อนในการวนรับค่าคะแนนสอบของนักเรียน (score) ซึ่งมีค่าระหว่าง 0 – 100 และคำนวณคะแนนรวม (total) และคะแนนเฉลี่ย (average) ของคะแนนสอบที่ป้อนเข้ามาทั้งหมด หากผู้ใช้ป้อนค่า -1 ให้หยุดการวนรับค่าคะแนนสอบ และแสดงผลคะแนนรวม และคะแนนเฉลี่ยทางจอภาพ

60 Example 8 (cont.) Inner loop Outter loop

61 1: /* Program: nested_1.c */
5: #include <stdio.h> 6: 7: /* the main function of the program */ 8: void main(void) 9: { 10: int num = 0; 11: float score, total, avg; 12: total = avg = 0.0; 13: do 14: { 15: /* validate a student score */ 16: do 17: { 18: printf(“Enter your score (1-100) or -1 to exit: ”); 19: scanf(“%f”, &score); 20: } while ( score < -1 || score > 100 ); 21: 22: /* evaluate total score and increase student number */ 23: total = total + score; 24: num++; 25: } while ( score != -1 ); 26: 27: /* evaluate average score and display the results */ 28: total++, num--; 29: if ( num > 0 ) 30: avg = total / num; 31: 32: printf(“Total score is %.2f\n”, total); 33: printf(“Average score is %.2f\n”, avg); 34: 35: }

62 Outter loop Inner loop 12: total = avg = 0.0; 13: do 14: {
14: { 15: /* validate a student score */ 16: do 17: { 18: printf(“Enter your score (1-100) or -1 to exit: ”); 19: scanf(“%f”, &score); 20: } while ( score < -1 || score > 100 ); 21: 22: /* evaluate total score and increase student number */ 23: total = total + score; 24: num++; 25: } while ( score != -1 );

63 Result Enter your score (1-100) or -1 to exit:_ Total score is 255.00
Average score is 85.00

64 Comparison Iteration Loop
while และ do-while ไม่ได้กำหนดองค์ประกอบของโครงสร้างแบบวนซ้ำครบทั้ง 4 ส่วนในตัวเองเหมือน for while และ do-while มีเพียงส่วนตรวจสอบเงื่อนไขและการทำงานในวงรอบ for มีทั้ง 4 ส่วน: กำหนดค่าเริ่มต้น, ตรวจสอบเงื่อนไข, การทำงานในวงรอบ และการปรับปรุงค่า ดังนั้น while และ do-while มักจะมีส่วนกำหนดค่าเริ่มต้นก่อนประโยควนซ้ำ และแทรกส่วนปรับปรุงค่าไว้ในวงรอบเหมือนส่วนการทำงานในวงรอบ

65 Comparison Iteration Loop (cont.)
while และ for จะคล้ายกันคือเป็นแบบ pre-test loop ส่วน do-while จะมีการทำงานแบบ post-test loop for เหมาะกับงานที่รู้จำนวนรอบที่แน่นอน เช่น การใช้ตัวนับรอบ while กับ do-while เหมาะกับงานที่ไม่รู้จำนวนรอบที่แน่นอน เช่น การวนรับค่าข้อมูลจนกว่าจะป้อนค่าพิเศษ หรือการวนรับค่าเรื่อยๆ จนกว่าจะได้ค่าในช่วงที่ต้องการ

66 Comparison Iteration Loop (cont.)
คำสั่งทางเลือก (if, if-else และ switch) และคำสั่งวนซ้ำ (while, for และ do-while) ใช้การตรวจสอบเงื่อนไข (ผลลัพธ์ของนิพจน์ที่เป็นจริงหรือเท็จ) ในการกำหนดทิศทางการทำงาน คำสั่ง break และ continue เป็นคำสั่งพิเศษที่ใช้กำหนดทิศทางการทำงานโดยไม่ขึ้นกับเงื่อนไข ซึ่งอาจทำให้โครงสร้างของโปรแกรมถูกทำลายได้ คำสั่ง break ทำให้โปรแกรมออกมาจากบล็อคของคำสั่งทางเลือก switch หรือคำสั่งวนซ้ำทุกชนิด คำสั่ง continue ทำให้โปรแกรมข้ามการทำงานที่เหลือภายในวงรอบ เพื่อวนกลับขึ้นไปเริ่มทำงานในวงรอบใหม่

67 Break Statement while (expr) { statement; break; }
for ( expr1; expr2; expr3 ) { statement; break; } do { statement; break; } while (expr);

68 Example 9 Example: ให้เขียนโปรแกรมโดยใช้คำสั่ง break ในประโยควนซ้ำแบบ do-while เพื่อตรวจสอบค่าตัวเลือกที่เป็นตัวอักษรระหว่าง ‘A’ – ‘F’ ให้ผู้ใช้ป้อนค่าตัวเลือกไม่เกิน 3 ครั้ง ถ้าป้อนครบ 3 ครั้ง ให้ใช้คำสั่ง break ออกมาจากวงรอบทันที

69 1: /* Program: break_1.c */
5: 6: #include <stdio.h> 7: 8: /* the main function of the program */ 9: void main(void) 10: { 11: int i = 1; 12: char choice; 13: 14: /* get a choice between ‘A’ – ‘F’ */ 15: do 16: { 17: printf(“Enter your choice (A-F):”); 18: choice = getchar(); 19: 20: /* break out from the loop if it is the 3rd time */ 21: if ( i == 3 ) 22: break; 23: 24: i++; /* count the number of choices */ 25: 26: } while ( choice < ‘A’ || choice > ‘F’ ); 27: 28: if ( choice < ‘A’ || choice > ‘F’ ) 29: printf(“Your’ve entered invalid choices for 3 times\n”); 30: else 31: printf(“Your choice %c is valid\n”, choice); 32: 33: }

70 Result Enter your choice (A-F):_
15: do 16: { 17: printf(“Enter your choice (A-F):”); 18: choice = getchar(); 19: 20: /* break out from the loop if it is the 3rd time */ 21: if ( i == 3 ) 22: break; 23: 24: i++; /* count the number of choices */ 25: 26: } while ( choice < ‘A’ || choice > ‘F’ ); 28: if ( choice < ‘A’ || choice > ‘F’ ) 29: printf(“Your’ve entered invalid choices for 3 times\n”); 30: else 31: printf(“Your choice %c is valid\n”, choice); Enter your choice (A-F):_ You’ve entered invalid choiced for 3 times

71 Result Enter your choice (A-F):_ Your choice A is valid T O A 15: do
16: { 17: printf(“Enter your choice (A-F):”); 18: choice = getchar(); 19: 20: /* break out from the loop if it is the 3rd time */ 21: if ( i == 3 ) 22: break; 23: 24: i++; /* count the number of choices */ 25: 26: } while ( choice < ‘A’ || choice > ‘F’ ); 28: if ( choice < ‘A’ || choice > ‘F’ ) 29: printf(“Your’ve entered invalid choices for 3 times\n”); 30: else 31: printf(“Your choice %c is valid\n”, choice); Enter your choice (A-F):_ Your choice A is valid T O A

72 Continue Statement while (expr) { statement; continue; }
for ( expr1; expr2; expr3 ) { statement; continue; } do { statement; continue; } while (expr);

73 Example 10 Example: ใช้ ประโยค continue ในประโยควนซ้ำ 2 แบบ ดังนี้ ใช้ในประโยควนซ้ำแบบ do-while เพื่อข้ามการแสดงผล เลขจำนวนเต็มคู่ ระหว่าง 1 – 10 (2, 4, 6, 8, 10) และใช้ในประโยควนซ้ำแบบ for เพื่อข้ามการแสดงผล เลขจำนวนเต็มคี่ ระหว่าง 1 – 10 (1, 3, 5, 7, 9)

74 1: /* Program: break_2.c */
5: #include <stdio.h> 6: 7: /* the main function of the program */ 8: void main(void) 9: { 10: int num = 0; 11: 12: printf(“The odd numbers between 1 – 10: ”); 13: 14: /* display the odd numbers between */ 15: do 16: { 17: num++; 18: 19: /* skip the even numbers */ 20: if ( (num % 2) == 0 ) 21: continue; 22: 23: printf(“%-2d”, num); 24: } while ( num < 10 ); 25: 26: printf(“\n\nThe even numbers between 1 – 10: ”); 27: 28: /* display the even numbers between 1 – 10 */ 29: for ( num = 1; num <= 10; num++ ) 30: { 31: /* skip the odd numbers */ 32: if ( (num % 2) == 1 ) 33: continue; 34: 35: printf(“%-2d”, num); 36: } 37: 38: printf(“\n\n”); 39: 40: }

75 Result The odd numbers between 1 – 10: 1 3 5 7 9
12: printf(“The odd numbers between 1 – 10: ”); 13: 14: /* display the odd numbers between */ 15: do 16: { 17: num++; 18: 19: /* skip the even numbers */ 20: if ( (num % 2) == 0 ) 21: continue; 22: 23: printf(“%-2d”, num); 24: } while ( num < 10 ); The odd numbers between 1 – 10:

76 Result The even numbers between 1 – 10: 2 4 6 8 10
26: printf(“\n\nThe even numbers between 1 – 10: ”); 27: 28: /* display the even numbers between 1 – 10 */ 29: for ( num = 1; num <= 10; num++ ) 30: { 31: /* skip the odd numbers */ 32: if ( (num % 2) == 1 ) 33: continue; 34: 35: printf(“%-2d”, num); 36: } The even numbers between 1 – 10:

77 How to choose iteration loop?
ใช้ for ในกรณีที่ทราบจำนวนรอบของการวนซ้ำ ใช้ while ในกรณีที่ต้องคิดเงื่อนไขก่อนการทำงานแบบวนซ้ำ ใช้ do-while ในกรณีที่ต้องคิดเงื่อนไขการทำงานภายหลังจากทำงานไปแล้วครั้งหนึ่ง การเขียนโปรแกรมที่มีความซับซ้อนมากขึ้นจำเป็นต้องอาศัยการเขียนโปรแกรมแบบวนรอบร่วมกับการเขียนโปรแกรมแบบมีเงื่อนไข โดยการเพิ่มเงื่อนไขการทำงานในส่วนของการวนรอบ หรือมีการตรวจสอบเงื่อนไขว่าจะให้โปรแกรมมีการวนรอบอย่างไร

78 Outline 1 Review the Last Topic 2 For Loop 3 While Loop 4
Do-While Loop 5 Assignment #4

79 Assignments #4 จงเขียนผังงานและโปรแกรมแสดงสูตรคูณแม่ 2
จงเขียนผังงาน และโปรแกรมเพื่อรับอักษรตัวเล็กมา 20 ตัว แล้วตรวจสอบว่ามีอักษรที่เป็นสระกี่ตัว และไม่ใช่สระกี่ตัว จงเขียนผังงาน และโปรแกรมการจ่ายเงินของตู้ ATMโดยให้ผู้ใช้กรอกจำนวนเงินเข้ามาแล้วโปรแกรมจะตรวจสอบว่าถูกต้องหรือไม่โดยมีเงื่อนไขว่า ATM จะจ่ายเงินให้ไม่เกิน และต้องมากกว่า 100 ซึ่งโปรแกรมจะต้องคำนวณว่าต้องจ่ายเป็นธนบัตรมูลค่าต่างๆอย่างละจำนวนกี่ใบโดยกำหนดให้ในเครื่อง ATM มีธนบัตรมูลค่าต่างๆดังนี้ ธนบัตรใบละ 1000 ธนบัตรใบละ 500 ธนบัตรใบละ 100

80 Assignments #4 (cont.) จงเขียนผังงานและโปรแกรมแสดงผลรูปสี่เหลี่ยมขนาด n x n โดยโปรแกรมจะรอรับจำนวนเต็มจากผู้ใช้งาน ดังตัวอย่าง Please enter number : 4 Output **** * * Please enter number : 9 Output ********* * *

81 Assignments #4 (cont.) จงเขียนผังงานและโปรแกรม เมนูรับรายการอาหารจากลูกค้าเพื่อคำนวณ ราคาอาหารทั้งหมด โดยกำหนดให้โปรแกรมมีรายการดังนี้ 1. Pizza 150 ฿ 2. Hamburger 50 ฿ 3. Sandwich 25 ฿ 4. Water 10 ฿ 0. Calculate money โดยทุกครั้งที่เลือกเมนูรายการอาหารจะแสดงจำนวนอาหารที่สั่งไปแล้วด้วย

82 Assignments #4 (cont.) ตัวอย่างการรันโปรแกรม เลือก 1 เพื่อสั่ง Pizza
แสดงจำนวนของ และราคา รอรายการ เลือก 2 เพื่อสั่ง Hamburger เพิ่ม

83 Assignments #4 (cont.) แสดงจำนวนของ และราคา รอรายการ
เลือก 4 เพื่อสั่ง Water แสดงจำนวนของ และราคา รอรายการ เลือก 2 เพื่อสั่ง Hamburger เพิ่ม

84 Assignments #4 (cont.) แสดงจำนวนของ และราคา รอรายการ
เลือก 0 เพื่อจบรายรายและสรุปจำนวนเงิน


ดาวน์โหลด ppt Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

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


Ads by Google