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

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

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

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


งานนำเสนอเรื่อง: "226101 Computer Programming การเขียนโปรแกรมคอมพิวเตอร์ สัปดาห์ที่ 5 การเขียนโปรแกรมแบบทางเลือก การเขียนโปรแกรมแบบวนซ้ำ."— ใบสำเนางานนำเสนอ:

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

2 วัตถุประสงค์ เข้าใจหลักการเขียนโปรแกรมแบบทางเลือก ด้วย การใช้ if เข้าใจหลักการเขียนโปรแกรมแบบทางเลือก ด้วย การใช้ switch สามารถประยุกต์แก้ปัญหา ด้วยการเขียนโปรแกรม แบบทางเลือก เข้าใจหลักการเขียนโปรแกรมวนซ้ำแบบ while ทั้ง 3 ประเภท เข้าใจหลักการเขียนโปรแกรมวนซ้ำด้วยการใช้ for สามารถประยุกต์แก้ปัญหา ด้วยการวนซ้ำได้ สามารถประยุกต์แก้ปัญหา ด้วยการเขียนโปรแกรม แบบทางเลือก และการวนซ้ำได้

3 Control Statement (Condition) ICT, University of Phayao3 ยังจำได้ไหม ? int a, b, c ; b = 1; c = 2; a = b < c ? 1 : 2 ; a = 2

4 Control Statement (Condition) ICT, University of Phayao4 ยังจำได้ไหม ? int a, b, c ; b = 1; c = 2; a = b < c ? 1 : 2 ; int a, b, c ; b = 1; c = 2; ถ้า b น้อยกว่า c ให้ a = 1 ถ้า b ไม่ได้น้อย กว่า c ให้ a = 2

5 Control Statement (Condition) ICT, University of Phayao5 int a, b, c ; b = 1; c = 2; if ( b < c) { a = 1; } else { a = 2; } if condition ถ้า b น้อยกว่า c ให้ a = 1 ถ้า b ไม่ได้น้อย กว่า c ให้ a = 2

6 Control Statement (Condition) ICT, University of Phayao6 if ( logical operator) { process ; } else { process ; } if condition :: formatting in c language logical operator( ตัวดำเนินการทางตรรกะ ) เป็นจริง ถ้า มีค่าไม่เท่ากับ 0 เป็นเท็จ ถ้า มีค่าเท่ากับ 0 process การกำหนดค่า หรือการคำนวณ แสดงผล รับค่า...

7 Control Statement (if) ICT, University of Phayao7 สิ่งที่เรียนไปแล้วเป็น 1 กรณี จาก 3 กรณี ของ if 1.if 2.if -> else 3.if -> else if กรณีที่ผ่านมาเป็นลักษณะของกรณีที่ 2

8 Control Statement (if) ICT, University of Phayao8 การใช้ if if ( เงื่อนไข ) { สิ่งที่ต้องการให้ทำ เช่น printf หรือ การ กำหนดค่า i++ } else// ถ้าเงื่อนไข if เป็นเท็จ หรือ เท่ากับ 0 { สิ่งที่ต้องการให้ทำ เช่น printf หรือ การ กำหนดค่า i++ }

9 Control Statement (if) ICT, University of Phayao9 กรณีที่ 1 ( if ) int rainy = 1; if ( rainy == 1 ){ printf("Keep your clothes"); } #include int main(void) { char rainy[100] = " ฝน ตก "; if ( rainy == " ฝนตก "){ printf(" ฝนตก "); } #include int main(void) { char rainy[100] = " ฝนตก "; if (strcmp(rainy," ฝน ตก ")==0){ printf(" ฝนตก "); }

10 Control Statement (if) ICT, University of Phayao10 กรณีที่ 2 ( if else) int rainy = 1; if ( rainy == 1 ){ printf("Keep your clothes"); } #include int main(void) { char rainy[100] = " ฝน ตก "; if ( rainy == " ฝนตก "){ printf(" ฝน ตก "); }else{ printf(" ฝนไม่ ตก "); } #include int main(void) { char rainy[100] = " ฝนตก "; if (strcmp(rainy," ฝน ตก ")==0){ printf(" ฝนตก "); }else{ printf(" ฝนไม่ตก "); }

11 Control Statement (if)  ข้อแนะนำการเปรียบเทียบข้อความ (String)  ในบางกรณีถ้าต้องการเปรียบเทียบข้อความ เช่น  ต้องการเปรียบเทียบ ชื่อที่รับมาโดยใช้ตัวแปร name  เปรียบเทียบว่า name เท่ากับ “John” หรือไม่  โดยปกติ เมื่อมีการพิมพ์ให้ name เป็น “John” ใช้ if (name == “John”) จะสามารถเปรียบเทียบได้  แต่มีบางกรณี ไม่สามารถใช้การเปรียบเทียบลักษณะนั้นได้ เพราะ เหตุผลหลายๆ ประการเช่น ขนาดการจองพื้นที่ char สำหรับ name หรือ special character หลัง ข้อความ “John”  ดังนั้นให้ใช้ if (strcmp(name,“John”) == 0 ) ในการเปรียบเทียบ โดยค่าจะเท่ากับ 0 เมื่อเท่ากัน และถ้าไม่เท่าจะไม่เท่ากับ 0

12 Control Statement (if) ICT, University of Phayao12 กรณีที่ 3 ( if else if, else if, …, [else,else if] ) #include int main(void) { char grade = 'b'; if (grade == 'A' || grade == 'a' ){ printf("You get A"); }else if (grade == 'B' || grade == 'b' ){ printf("You get B"); }else if (grade == 'C' || grade == 'c' ){ printf("You get C"); }if (grade == 'D' || grade == 'd' ){ printf("You get D"); } #include int main(void) { char grade ; scanf("%c", &grade); if (grade == 'A' || grade == 'a' ){ printf("You get A"); }else if (grade == 'B' || grade == 'b' ){ printf("You get B"); }else if (grade == 'C' || grade == 'c' ){ printf("You get C"); }else if (grade == 'D' || grade == 'd' ){ printf("You get D"); }else{ printf("You get F"); }

13 Control Statement (if) ICT, University of Phayao13 กรณีพิเศษ ( if ซ้อน if ) int score = 75; if (score >= 50 ) { if (score < 80) { printf(''You get B''); }else { printf(''You get A''); } }else { printf(''You get F''); } int score = 75; if (score >= 50 ){ if (score < 80){ printf(''You get B''); }else{ printf(''You get A''); } }else{ printf(''You get F''); } int score = 75; if (score >= 50 ){ if (score < 80) printf(''You get B''); else printf(''You get A''); }else printf(''You get F''); 1 2 3

14 กรณีพิเศษ ( if ซ้อน if ) int score = 75; if (score >= 50 ) if (score < 80) printf(''You get B''); else printf(''You get A''); else printf(''You get F''); Control Statement (if) ICT, University of Phayao14 int score = 75; if (score >= 50 ){ if (score < 80){ printf(''You get B''); } }else{ printf(''You get A''); }else{ printf(''You get F''); } int score = 75; if ( score >= 50 ){ if (score < 80) printf(''You get B''); else printf(''You get A''); }else printf(''You get F''); 3 4 4

15 Control Statement (if) ICT, University of Phayao15 ลองเขียนโปรแกรมตัดเกรดโดยถ้าคะแนน > 79.4 ได้ เกรด 4 >69.4 แต่ <= 79.4 ได้เกรด 3 >59.4 แต่ <= 69.4 ได้เกรด 2 >49.4 แต่ <= 59.4 ได้เกรด 1 นอกเหนือจากเงื่อนไขข้างต้น เกรด 0

16 Control Statement (if)

17 ICT, University of Phayao17 ลองเขียนโปรแกรมคำนวณค่าไฟฟ้า > ไม่ใช้เลยเสีย 5 บาท > ใช้ไม่เกิน 80 หน่วยเสียหน่วยละ 1 บาท > ใช้เกิน 80 หน่วยเสียหน่วยละ 1.5 บาท ให้คำนวณค่าไฟฟ้าทศนิยม 2 ตำแหน่ง โดยรับค่าหน่วยไฟฟ้าเข้ามา

18 ICT, University of Phayao18

19 Switch Case ICT, University of Phayao19 b = 1; c = 2; if ( b < c) { a = 0; } else { a = 1; } int a, b, c ; b = 1; c = 2; switch ( b < c ) { case 1 : a = 0 ; break ; default : a = 1 ; break ; }

20 Switch Case ICT, University of Phayao20 switch ( คำถาม ) { case คำตอบที่ 1 : สิ่งที่ต้องการให้ทำ ; break ; // ออกจากเงื่อนไข switch case คำตอบที่ 2 : สิ่งที่ต้องการให้ทำ ; break ;// ออกจากเงื่อนไข switch default :// กรณี ไม่มีคำตอบ สิ่งทีต้องการให้ทำ ; break ; // ออกจากเงื่อนไข switch } * ให้สังเกต - เครื่องหมาย หลัง case - เครื่องหมายหลังสิ่งที่ต้องการให้ทำ - และ เครื่องหมายหลัง break

21 Switch Case ICT, University of Phayao21 int b = 1,a; if ( b == 1) { a = 1; } else if ( b == 2) { a = 2; } else { a = 3; } int a, b, c ; b = 1; c = 2; switch ( b ) { case 1 : a = 1 ; break ; case 2 : a = 2 ; break ; default : a = 3 ; break ; }

22 Switch Case ICT, University of Phayao22 เขียนโปรแกรมเปรียบเทียบ เกรด ถ้า เกรด = A ได้ A ถ้า เกรด = B ได้ B ถ้า เกรด = C ได้ C ถ้า เกรด = D ได้ D ถ้า ไม่ใช่ ได้ F โดยใช้ Switch case

23 Switch Case ICT, University of Phayao23

24 ตัวอย่างโปรแกรม ICT, University of Phayao24

25 ตัวอย่างโปรแกรม ICT, University of Phayao 25

26 ตัวอย่างโปรแกรม  โปรแกรมคำนวณอัตราผ่อนรถยนต์ แบบดอกเบี้ย คงที่  เงื่อนไขเพิ่มเติม กรณี วงเงินกู้ มีค่าติดลบ หรือมากกว่า 1 ล้านล้านบาท ให้แจ้งผู้ใช้โปรแกรมว่า loan more than zero and less than one billion กรณี ระยะเวลา น้อยกว่า 3 และมากกว่า 7 ให้แจ้งผู้ใช้โปรแกรมว่า loan yearly payment more than 2 and less than 8 เงื่อนไขอื่นๆ ให้แสดงดอกเบี้ยต่อเดือนและอัตราผ่อนต่อเดือน โดยแสดงข้อมูลสอง บรรทัด ตามรูปแบบต่อไปนี้ interest/month = xxxx baht payment/month = yyyy baht เมื่อ xxxx เป็นจำนวนของดอกเบี้ยต่อเดือน yyyy เป็นจำนวนของเงินที่ต้องส่งต่อเดือน ICT, University of Phayao 26


ดาวน์โหลด ppt 226101 Computer Programming การเขียนโปรแกรมคอมพิวเตอร์ สัปดาห์ที่ 5 การเขียนโปรแกรมแบบทางเลือก การเขียนโปรแกรมแบบวนซ้ำ.

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


Ads by Google