1 / 44

การไหลของโปรแกรม

START. START. Statement 1. Statement. Statement 2. END. Statement 3. โปรแกรมที่มีคำสั่งเดียว. Statement n. END. โปรแกรมที่มีหลายคำสั่ง. การไหลของโปรแกรม. โปรแกรมแบบง่าย ทำงานรวดเดียวจากบนลงล่าง. 1. Relational Operators.

minnie
Download Presentation

การไหลของโปรแกรม

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. START START Statement1 Statement Statement2 END Statement3 โปรแกรมที่มีคำสั่งเดียว Statementn END โปรแกรมที่มีหลายคำสั่ง การไหลของโปรแกรม • โปรแกรมแบบง่าย ทำงานรวดเดียวจากบนลงล่าง 1

  2. Relational Operators • ใช้สำหรับเปรียบเทียบความสัมพันธ์ของค่าคงที่หรือตัวแปร(Operand)ที่กำหนดให้

  3. Expression • ใช้เรียกแทนการนำค่าคงที่ หรือตัวแปร(Operand) เขียนร่วมกับตัวดำเนินการ (Operator)

  4. Relational Operators • ตัวอย่างการประมวลผลของ Relational Operators

  5. Logical (Bitwise) Operators • ใช้สำหรับเปรียบเทียบ ค่าคงที่หรือตัวแปร(Operand) เขียนร่วมกับตัวดำเนินการทางตรรกะ (LogicalOperator)

  6. AND Operators • ตัวดำเนินการ และ จะเป็นจริงก็ต่อเมื่อ ตัวแปรทุกตัวมีค่าเป็นจริงนอกจากนั้นเป็นเท็จทั้งหมด

  7. AND Operators • ตัวอย่างการประมวลผลของ AND Operators

  8. OR Operators • ตัวดำเนินการ หรือ จะเป็นเท็จก็ต่อเมื่อ ตัวแปรทุกตัวมีค่าเป็น เท็จนอกจากนั้นเป็นจริงทั้งหมด

  9. OR Operators • ตัวอย่างการประมวลผลของ OR Operators

  10. ตัวอย่างการเปรียบเทียบตัวอย่างการเปรียบเทียบ • ตัวอย่างการเปรียบเทียบ

  11. ลำดับความสำคัญ (Precedence)ของตัวดำเนินการ • แสดงลำดับความสำคัญ จากมากไปหาน้อย ในการประมวลผล ตัวดำเนินการต่างๆ

  12. ลำดับความสำคัญ (Precedence)ของตัวดำเนินการ(ต่อ) • แสดงลำดับความสำคัญ จากมากไปหาน้อย ในการประมวลผล ตัวดำเนินการต่างๆ

  13. ตัวอย่างการประมวลผล โดยพิจารณาลำดับความสำคัญ

  14. การควบคุมการไหลของโปรแกรมการควบคุมการไหลของโปรแกรม • คำสั่งกำหนดเงื่อนไข • โครงสร้าง if • โครงสร้าง if…else • โครงสร้าง ifแบบหลายเงื่อนไข • โครงสร้าง switch-case • คำสั่งวนซ้ำ • โครงสร้าง while loop • โครงสร้าง do…while loop • โครงสร้าง for loop • โปรแกรมย่อย (ฟังก์ชัน) 14

  15. C Syntax Flowchart if (condition) { statement1; : statementN; } START condition true Statement false Statement END โครงสร้าง if • ส่วนของ conditionตีความเป็นข้อมูลแบบ int • ทำคำสั่งใน {} หาก conditionเป็นจริง (ไม่เป็นศูนย์) • หากมีคำสั่งเดียวไม่จำเป็นต้องใช้วงเล็บปีกกา 15

  16. ตัวอย่าง if #include <stdio.h> int main() { int i=101,j=100; if(i>j) printf("I > J"); getch(); return 0; } if1.cpp I > J 16

  17. START true condition false Statementt1 Statementf1 Statementt2 Statementf2 END โครงสร้าง if…else Flowchart C Syntax if (condition) { statementt1; statementt2; } else { statementf1; statementf2; } 17

  18. ตัวอย่าง if…else #include <stdio.h> int main() { int i=101,j=102; if(i>j) printf("I > J"); else printf("I <= J"); getch(); return 0; } ifelse1.cpp I <= J 18

  19. true x==1 Action1; false true x==2 Action2; false true x==3 Action3; false true x==4 Action4; false Default_Action; โครงสร้างif แบบหลายเงื่อนไข if (x==1) Action1; else if (x==2) Action2; else if (x==3) Action3; else if (x==4) Action4; else Default_Action; 19

  20. ตัวอย่าง if แบบหลายเงื่อนไข #include <stdio.h> int main() { inti=7; if(i>7) printf("> 7"); else if(i>6) printf("> 6"); else if(i>5) printf("i> 5"); else printf("1 , 2 , 3"); getch(); return 0; } ifelse2.cpp > 6 20

  21. true x==1 Action1; false true x==2 Action2; false true x==3 Action3; false true x==4 Action4; false Default_Action; โครงสร้างswitch-case switch (x) { case 1: Action1; break; case 2: Action2; break; case 3: Action3; break; case 4: Action4; break; default: Default_Action; break; } 21

  22. ตัวอย่าง switch-case #include <stdio.h> int main() { int i=2; switch(i) { case 2 : printf("2"); break; case 1 : printf("1"); break; default : printf("NO MATCH"); break; } getch(); return 0; } switch1.cpp 2 22

  23. ทำใบงานที่ 3.1-3.5

  24. ตัวอย่าง การกำหนดค่าตัวนับ i++ = i = i+1 i-- = i = i-1 i+=5= i = i+5 i-=5= i = i-5

  25. START false true Statement Statement END โครงสร้าง whileลูป • วนทำคำสั่งstmt1 ถึง stmtNตราบเท่าที่conditionเป็นจริง while (condition) { stmt1; stmt2; : stmtN; } condition 25

  26. ตัวแปรที่ใช้นับ ส่วนกำหนดค่าเริ่มต้น คำสั่งที่ถูกทำซ้ำ เงื่อนไขของตัวนับ การปรับค่าตัวนับ ลูปวนนับ (Counting Loop) • หากพิจารณาโครงสร้างของลูปที่ใช้ในโปรแกรมส่วนใหญ่ มักจะเป็นลูปแบบวนนับ • ลูปวนนับจะมีส่วนประกอบดังตัวอย่างต่อไปนี้เสมอ int i, sum = 0; i = 1; while (i <= 10) { sum = sum + i; i = i + 1; } printf("Sum = %d\n", sum); 26

  27. ตัวอย่างโครงสร้าง whileลูป #include <stdio.h> int main() { int i=1; while(i<=10) { printf("Hello %d\n",i); i++; } getch(); return 0; } while1.c Hello 1 Hello 2 Hello 3 Hello 4 Hello 5 Hello 6 Hello 7 Hello 8 Hello 9 Hello 10 27

  28. โครงสร้าง whileลูป(INFINITY LOOP) #include <stdio.h> int main() { int i=1; while(1) { printf("Hello %d\n",i); if(i==10) break; i++; } getch(); return 0; } while2.c Hello 1 Hello 2 Hello 3 Hello 4 Hello 5 Hello 6 Hello 7 Hello 8 Hello 9 Hello 10 28

  29. START Statement1 StatementN true false โครงสร้าง do…whileลูป do { stmt1; stmt2; : stmtN; } while (condition); • ทำคำสั่ง stmt1...stmtNและวนทำซ้ำอีกตราบเท่าที่ conditionยังคงเป็นจริง • นั่นคือ stmt1...stmtNจะถูกกระทำอย่างน้อยหนึ่งครั้ง condition END 29

  30. ตัวอย่าง do…whileลูป #include <stdio.h> int main() { int i=1; do { printf("Hello %d\n",i); i++; } while(i<=10); getch(); return 0; } dowhile1.c Hello 1 Hello 2 Hello 3 Hello 4 Hello 5 Hello 6 Hello 7 Hello 8 Hello 9 Hello 10 30

  31. โครงสร้าง for ลูป • เป็นโครงสร้างที่ให้ความสะดวกในการเขียนลูปวนนับ • การทำงาน 1. ทำคำสั่ง init_stmtหนึ่งครั้ง 2. ถ้า conditionเป็นจริง ทำคำสั่ง statement1...statementN 3. ทำคำสั่งupdate_stmt จากนั้นกลับไปทำข้อ 2 for (init_stmt; condition; update_stmt) { statement1; statement2; : statementN; } 31

  32. START init_stmt condition true Statement1 StatementN END การทำงานของ for ลูป for (init_stmt; condition; update_stmt) { statement1; statement2; : statementN; } false update_stmt 32

  33. ตัวอย่าง forลูป #include <stdio.h> int main() { int i; for(i=1;i<=10;i++) { printf("Hello %d",i); printf("\n"); } getch(); return 0; } for1.c Hello 1 Hello 2 Hello 3 Hello 4 Hello 5 Hello 6 Hello 7 Hello 8 Hello 9 Hello 10 33

  34. โปรแกรมย่อย (Subroutine) • ในภาษาซีเรียกว่า "ฟังก์ชัน"(Function) • เป็นส่วนของโปรแกรมที่มีหน้าที่การทำงานชัดเจนในตัวเอง ซึ่งถูกเรียกใช้ในโปรแกรมหลักอีกทีหนึ่ง • การเขียนโปรแกรมโดยแยกเป็นฟังก์ชันมีข้อดีหลายประการ • ช่วยแบ่งงานที่ซับซ้อนเป็นงานย่อยหลายงาน • ลดการเขียนโค้ดที่ซ้ำซ้อน • ซ่อนรายละเอียดไว้ในส่วนอื่น ทำให้โปรแกรมเข้าใจได้ง่ายขึ้น • ฟังก์ชันที่เขียนขึ้นมาสามารถนำไปใช้ในโปรแกรมอื่นได้ 34

  35. ชนิดของฟังก์ชัน • ฟังก์ชันมาตรฐาน (Standard Functions) • เป็นฟังก์ชันที่อยู่ในชุดไลบรารีของภาษาซี เรียกใช้ได้ทันที • เช่น printf(), scanf(),... • ฟังก์ชันที่ผู้ใช้กำหนด (User Defined Functions) • เป็นฟังก์ชันที่ผู้ใช้สร้างขึ้นเอง • เรียกใช้ใน main() หรือจากฟังก์ชันอื่นๆ ได้เหมือนฟังก์ชันมาตรฐาน 35

  36. ตัวอย่างฟังก์ชันมาตรฐานตัวอย่างฟังก์ชันมาตรฐาน 36

  37. ฟังก์ชันที่ผู้ใช้กำหนดเองฟังก์ชันที่ผู้ใช้กำหนดเอง • แบบไม่ส่งค่ากลับ • ระบุชนิดข้อมูล void • ไม่ต้องมีคำสั่ง return • แบบส่งค่ากลับ • ระบุชนิดข้อมูลที่ต้องการ • ใช้คำสั่ง returnส่งค่าคืนตามชนิดที่ระบุ void say_hi(char *name) { printf("Hi, %s\n", name); } int max(int a, int b) { if (a > b) return a; else return b; } 37

  38. การไหลของโปรแกรมเมื่อใช้ฟังก์ชันการไหลของโปรแกรมเมื่อใช้ฟังก์ชัน #include <stdio.h> int incr(int i) { int j; j = i + 1; return j; } int main() { int k, m = 4; k = incr(m); printf ("k = %d, m = %d\n", k, m); getch(); return 0; } function1.c 38

  39. โปรแกรมแสดงข้อความ (Function) แบบไม่ส่งค่ากลับ #include <stdio.h> void print1() { printf(“Hello”); printf(“\n”); } int main() { print1(); print1(); getch(); return 0; } function2.c 39

  40. โปรแกรมคำนวณภาษี (Function)แบบส่งค่ากลับ #include <stdio.h> float cal_tax(float i) { float ctax; ctax = i*0.07; return ctax; } int main() { float money=7290,ff; ff = cal_tax(money); printf(“%f”,ff); getch(); return 0; } function3.c 40

  41. ขั้นตอนวิธีกับการโปรแกรมขั้นตอนวิธีกับการโปรแกรม • การออกแบบขั้นตอนวิธีเป็นส่วนสำคัญในการพัฒนาโปรแกรม • การ "เขียนโปรแกรม" เป็นเพียงการแปลงขั้นตอนวิธีให้อยู่ในรูปที่ยอมรับได้โดยตัวภาษา 41

  42. ตัวอย่าง • เขียนโปรแกรมเพื่อรับตัวเลขระบุขนาด และพิมพ์รูปสามเหลี่ยมที่มีขนาดตามที่กำหนด Enter N: 3 * ** *** Enter N: 5 * ** *** **** ***** 42

  43. ส่งค่า i ให้ scanf แทนที่จะส่งตำแหน่ง ใช้คำสั่งกำหนดค่า (=) แทนการเปรียบเทียบ (==) ข้อผิดพลาดที่พบบ่อย #include <stdio.h> int main () { int i; scanf("%d", i); if (i = 0) puts("false"); else puts("true"); return 0; } 43

  44. THANK YOU QUESTION ?

More Related