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

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

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 เพื่อจบรายรายและสรุปจำนวนเงิน

85 สูตรคูณแม่ 2 Output Analysis สูตรคูณแม่ 2 Input Analysis ไม่มี
Solution: จงเขียนผังงานและโปรแกรมแสดงสูตรคูณแม่ 2 Output Analysis สูตรคูณแม่ 2 Input Analysis ไม่มี Process Analysis โปรแกรมวนรอบเพื่อแสดงสูตรคูณแม่ 2

86 สูตรคูณแม่ 2 (ต่อ) 2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 … 2 * 12 = 24
2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 2 * 12 = 24 Variable Define num เป็นจำนวนเต็มเพื่อนับค่า

87 สูตรคูณแม่ 2 (ต่อ) START num num = 1 False num<=12 True 2, num
END num num<=12 True False 2, num 2*num num = 1 num++

88 สูตรคูณแม่ 2 (ต่อ) #include<stdio.h> int main() { int num;
printf ("Multiplication table\n"); for (num=1; num<=12; num++) printf ("%4d * %-2d = %-3d\n",2,num,2*num); } return 0;

89 สูตรคูณแม่ 2 (ต่อ) Multiplication table 2 * 1 = 2 2 * 2 = 4 2 * 3 = 6
2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18 2 * 10 = 20 2 * 11 = 22 2 * 12 = 24

90 ตรวจสอบสระ Output Analysis Input Analysis Process Analysis
Solution: จงเขียนผังงาน และโปรแกรมเพื่อรับอักษรตัวเล็กมา 20 ตัว แล้วตรวจสอบว่ามีอักษรที่เป็นสระกี่ตัว และไม่ใช่สระกี่ตัว Output Analysis จำนวนอักษรที่เป็นสระและไม่ใช่สระ Input Analysis อักษรที่ผู้ใช้ป้อนมาจำนวน 20 ตัว Process Analysis โปรแกรมทำงานแบบวนรอบเพื่อรับค่าจำนวนอักขระ แล้วตรวจสอบว่าเป็นสระ หรือไม่ แล้วนับจำนวนไว้จนครบ 20 ตัว

91 ตรวจสอบสระ (ต่อ) Variable Define
vowel เป็นจำนวนเต็มเพื่อใช้นับจำนวนสระ alphabet เป็นจำนวนเต็มเพื่อใช้นับจำนวนที่ไม่ใช่สระ count เป็นจำนวนเต็มเพื่อใช้นับว่าครบ 20 ตัวหรือไม่ letter เป็นอักขระเพื่อรับตัวอักษร

92 ตรวจสอบสระ (ต่อ) int vowel=0,alphabet=0,count; char letter;
START int vowel=0,alphabet=0,count; char letter; vowel=0,alphabet=0, count,letter False count<20 True count ++ ??? letter for (count=0; count<20; count++) { } ??? letter = getche();

93 ตรวจสอบสระ (ต่อ) letter = getche();
letter is vowel vowel++ alphabet++ True false letter = getche(); if ((letter=='a')||(letter=='e')||(letter=='i') ||(letter=='o')||(letter=='u')) else vowel++; alphabet++;

94 ตรวจสอบสระ (ต่อ) printf ("\n***Result***\n");
printf ("Vowel (a,e,i,o,u) = %d\n",vowel); printf ("Other letter = %d",alphabet); vowel alphabet END

95 ตรวจสอบสระ (ต่อ) START vowel=0,alphabet=0, count,letter count = 0
END vowel=0,alphabet=0, count,letter count = 0 letter letter is vowel vowel++ alphabet++ count ++ count<20 vowel alphabet True false False

96 ตรวจสอบสระ (ต่อ) #include<stdio.h> #include<conio.h>
int main() { int vowel=0,alphabet=0,count; char letter; for (count=0; count<20; count++) printf ("\nEnter letter a-z : "); letter = getche(); if ((letter=='a')||(letter=='e')||(letter=='i') ||(letter=='o')||(letter=='u')) vowel++; else alphabet++; } printf ("\n***Result***\n"); printf ("Vowel (a,e,i,o,u) = %d\n",vowel); printf ("Other letter = %d",alphabet); return 0;

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

98 Variable Define โดยพิจารณาว่าส่วนที่อยู่ในกรอบให้แสดงเป็นช่องว่าง
….. บรรทัดที่ n แสดงผล '\n' แสดงผล '*' เฉพาะตำแหน่งขอบ ที่เหลือแสดงผล ' ' Variable Define num เป็นจำนวนเต็มเพื่อใช้เก็บค่าตัวเลขที่ผู้ใช้ป้อน i เป็นจำนวนเต็มเพื่อใช้นับจำนวนบรรทัด j เป็นจำนวนเต็มเพื่อใช้นับจำนวนอักษรในบรรทัด

99 โปรแกรมแสดงผลรูปสี่เหลี่ยมกลวง (ต่อ)
START num,i,j,space=' ' num i=1 i<=num END True False Process of each row i++ for (i=1; i<=num; i++) { } int num,i,j; char space=' '; printf ("Enter number : "); scanf ("%d",&num);

100 โปรแกรมแสดงผลรูปสี่เหลี่ยมกลวง (ต่อ)
Process of each row "\n" j=1 j<=num j++ Process of printing each character. True False printf ("\n"); for (j=1; j<=num; j++) { }

101 โปรแกรมแสดงผลรูปสี่เหลี่ยมกลวง (ต่อ)
Process of printing each character. (i==1)|| (i==num)|| (j==1)|| (j==num) ' ' '*' True False if (i==1 || i==num || j==1 || j==num) printf ("*"); else printf ("%c",space);

102 โปรแกรมแสดงผลรูปสี่เหลี่ยมกลวง (ต่อ)
#include<stdio.h> int main() { int num,i,j; char space=' '; printf ("Enter number : "); scanf ("%d",&num); for (i=1; i<=num; i++) printf ("\n"); for (j=1; j<=num; j++) if (i==1 || i==num || j==1 || j==num) printf ("*"); else printf ("%c",space); } return 0;

103 โปรแกรมตู้ ATM Output Analysis Input Analysis Process Analysis
แสดงผลลัพธ์ของจำนวนธนบัตรมูลค่าต่างๆ Input Analysis จำนวนเงินที่ผู้ใช้ต้องการกรอก Process Analysis โปรแกรมทำงานแบบวนรอบ เพื่อรับค่าจำนวนเงินที่ถูกต้อง ถ้าจำนวนเงินถูกต้องให้คำนวณว่ามีธนบัตรใดจำนวนเท่าใด

104 โปรแกรมตู้ ATM (ต่อ) Variable Define
ตรวจสอบว่าจำนวนเงินควรมีธนบัตรมูลค่า 1000 ฿ กี่ใบ แสดงผลและหักจำนวนเงินที่จ่ายธนบัตรมูลค่า 1000 ฿ ตรวจสอบว่าจำนวนเงินที่เหลือควรมีธนบัตรมูลค่า 500 ฿ กี่ใบ แสดงผลและหักจำนวนเงินที่จ่ายธนบัตรมูลค่า 500 ฿ ตรวจสอบว่าจำนวนเงินที่เหลือควรมีธนบัตรมูลค่า 100 ฿ กี่ใบ แสดงผล Variable Define money เป็นจำนวนเต็มสำหรับเก็บค่าเงิน amount เป็นจำนวนเต็มสำหรับเก็บจำนวนธนบัตรที่จ่ายออกไป

105 โปรแกรมตู้ ATM (ต่อ) START money,amount money (money > 20000) True
|| (money%100!=0) False money True 2

106 โปรแกรมตู้ ATM (ต่อ) money/1000!=0 amount = money/1000
money = money%1000 "1000฿" amount True 2 False X A money/500!=0 amount = money/500 money = money%500 "500฿" 3

107 โปรแกรมตู้ ATM (ต่อ) 3 money/100!=0 False True amount = money/100
END money/100!=0 amount = money/100 money = money%100 "100฿" amount True 3 False X

108 โปรแกรมตู้ ATM (ต่อ) #include<stdio.h> int main() {
int money,amount; printf ("Enter money : "); scanf ("%d",&money); while ((money > 20000) || (money%100!=0)) printf ("Sorry, please enter money : "); scanf ("%d",&money); } printf ("\nTotal bank note\n"); if (money/1000!=0) amount = money/1000; money = money%1000; printf ("Banknote 1000 : %d\n",amount); //continue

109 โปรแกรมตู้ ATM (ต่อ) if (money/500!=0) { amount = money/500;
money = money%500; printf ("Banknote 500 : %d\n",amount); } if (money/100!=0) amount = money/100; money = money%100; printf ("Banknote 100 : %d\n",amount); return 0;

110 โปรแกรมคำนวณค่าอาหาร
Output Analysis แสดงผลจำนวนรายการอาหาร และราคา Input Analysis จำนวนรายการอาหารที่ผู้ซื้อเลือก Process Analysis โปรแกรมทำงานแบบวนรอบ เพื่อแสดงรายการอาหาร และรับข้อมูลรายการอาหารที่เลือก แล้วรวมผลของรายการแต่ละชนิด และราคารวม

111 โปรแกรมคำนวณค่าอาหาร (ต่อ)
Variable Define menu เป็นจำนวนเต็มสำหรับรับรายการอาหาร m1,m2,m3,m4 เป็นจำนวนเต็มสำหรับนับจำนวนรายการอาหาร money เป็นจำนวนทศนิยมสำหรับเก็บจำนวนเงินรวม i เป็นจำนวนเต็มเพื่อควบคุมการเว้นบรรทัด

112 โปรแกรมคำนวณค่าอาหาร (ต่อ)
START menu, money=0 m1=0,m2=0,m3=0 money END menu menu == 1 money=money+150 m1++ money=money+50 m2++ money=money+25 m3++ menu!=0 m1,m2 m3,m4 money=money+10 m4++ menu == 2 menu == 3 menu == 4 True False

113 โปรแกรมคำนวณค่าอาหาร (ต่อ)
#include<stdio.h> int main() { int menu,m1=0,m2=0,m3=0,m4=0,i; float money=0; printf ("Welcome to Restaurant\n"); do printf ("1. Pizza B\n"); printf ("2. Hamburger 50 B\n"); printf ("3. Sandwich 25 B\n"); printf ("4. Water B\n"); printf ("0. Calculate money\n"); printf ("\nSelect menu : "); scanf ("%d",&menu); //continue

114 โปรแกรมคำนวณค่าอาหาร (ต่อ)
for (i=0;i<34;i++) printf ("\n"); if (menu==1) { money = money + 150; m1++; } else if (menu==2) money = money + 50; m2++; else if (menu==3) money = money + 25; m3++; //continue

115 โปรแกรมคำนวณค่าอาหาร (ต่อ)
else if (menu==4) { money = money + 10; m4++; } printf ("\n\nU have :\n"); printf ("Pizza %d * 150 : %d\n",m1,m1*150); printf ("Hamburger %d * 50 : %d\n",m2,m2*50); printf ("Sandwich %d * 25 : %d\n",m3,m3*25); printf ("Water %d * 10 : %d\n\n",m4,m4*10); while (menu!=0); printf ("\nTotal payment = %.2f\n",money); return 0;

116 Text Book Assignments 2.3 Write a single C statement to accomplish each of the following: a) Define the variables c, thisVariable, q76354 and number to be of type int. b) Prompt the user to enter an integer. End your prompting message with a colon (:) followed by a space and leave the cursor positioned after the space. c) Read an integer from the keyboard and store the value entered in integer variable a. d) If number is not equal to 7, print "The variable number is not equal to 7."

117 Text Book Assignments (cont.)
e) Print the message "This is a C program." on one line. f) Print the message "This is a C program." on two lines so that the first line ends with C. g) Print the message "This is a C program." with each word on a separate line. h) Print the message "This is a C program." with the words separated by tabs.

118 Text Book Assignments (cont.)
2.4 Write a statement (or comment) to accomplish each of the following: a) State that a program will calculate the product of three integers. b) Define the variables x, y, z and result to be of type int. c) Prompt the user to enter three integers. d) Read three integers from the keyboard and store them in the variables x, y and z. e) Compute the product of the three integers contained in variables x, y and z, and assign the result to the variable result. f) Print "The product is" followed by the value of the integer variable result.

119 Text Book Assignments (cont.)
2.6 Identify and correct the errors in each of the following statements: a) printf( "The value is %d\n", &number ); b) scanf( "%d%d", &number1, number2 ); c) if ( c < 7 );{ printf( "C is less than 7\n" ); } d) if ( c => 7 ) { printf( "C is equal to or less than 7\n" );

120 Text Book Assignments (cont.)
2.7 Identify and correct the errors in each of the following statements. (Note: There may be more than one error per statement.) a) scanf( "d", value ); b) printf( "The product of %d and %d is %d"\n, x, y ); c) firstNumber + secondNumber = sumOfNumbers d) if ( number => largest ) largest == number; e) */ Program to determine the largest of three integers /*

121 Text Book Assignments (cont.)
f) Scanf( "%d", anInteger ); g) printf( "Remainder of %d divided by %d is\n", x, y, x % y ); h) print( "The sum is %d\n," x + y ); i) if ( x = y ); printf( %d is equal to %d\n", x, y ); j) Printf( "The value you entered is: %d\n, &value );

122 Text Book Assignments (cont.)
2.12 What, if anything, prints when each of the following statements is performed? If nothing prints, then answer “Nothing.” Assume x = 2 and y = 3. a) printf( "%d", x ); b) printf( "%d", x + x ); c) printf( "x=" ); d) printf( "x=%d", x ); e) printf( "%d = %d", x + y, y + x ); f) z = x + y; g) scanf( "%d%d", &x, &y ); h) /* printf( "x + y = %d", x + y ); */ i) printf( "\n" );

123 Text Book Assignments (cont.)
2.13 Which, if any, of the following C statements contain variables whose values are replaced? a) scanf( "%d%d%d%d%d", &b, &c, &d, &e, &f ); b) p = i + j + k + 7; c) printf( "Values are replaced" ); d) printf( "a = 5" );

124 Text Book Assignments (cont.)
2.14 Given the equation y = ax3 + 7, which of the following, if any, are correct C statements for this equation? a) y = a * x * x * x + 7; b) y = a * x * x * ( x + 7 ); c) y = ( a * x ) * x * ( x + 7 ); d) y = ( a * x ) * x * x + 7; e) y = a * ( x * x * x ) + 7; f) y = a * x * ( x * x + 7 );

125 Text Book Assignments (cont.)
2.19 (Arithmetic, Largest Value and Smallest Value) Write a program that inputs three different integers from the keyboard, then prints the sum, the average, the product, the smallest and the largest of these numbers. Use only the single-selection form of the if statement you learned in this chapter. The screen dialogue should appear as follows: Input three different integers: Sum is 54 Average is 18 Product is 4914 Smallest is 13 Largest is 27

126 Text Book Assignments (cont.)
2.20 (Diameter, Circumference and Area of a Circle) Write a program that reads in the radius of a circle and prints the circle’s diameter, circumference and area. Use the constant value for π. Perform each of these calculations inside the printf statement(s) and use the conversion specifier %f. [Note: In this chapter, we have discussed only integer constants and variables. In Chapter 3 we’ll discuss floating-point numbers, i.e., values that can have decimal points.]

127 Text Book Assignments (cont.)
2.21 (Shapes with Asterisks) Write a program that prints the following shapes with asterisks.


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

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


Ads by Google