1 / 32

สรุปคำสั่ง if( เงื่อนไข )

สรุปคำสั่ง if( เงื่อนไข ). i f( เงื่อนไข ) { คำสั่ง ; }. if( เงื่อนไข ) { คำสั่ง1 ; } e lse { คำสั่ง2 ; }. scanf (“% d”,&a );. a = (2+3)*4/5; b= a*5/100;. if(a<0) ……. printf (“hello”); printf (“% d”,a );. โครงสร้างควบคุม. 1. โครงสร้างควบคุมแบบวนทางเลือก

clark
Télécharger la présentation

สรุปคำสั่ง if( เงื่อนไข )

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. สรุปคำสั่ง if(เงื่อนไข)

  2. if(เงื่อนไข) { คำสั่ง; }

  3. if(เงื่อนไข) { คำสั่ง1; } else { คำสั่ง2; }

  4. scanf(“%d”,&a); a = (2+3)*4/5; b= a*5/100; if(a<0) …….. printf(“hello”); printf(“%d”,a);

  5. โครงสร้างควบคุม • 1. โครงสร้างควบคุมแบบวนทางเลือก 1.1 คำสั่ง if • 2. โครงสร้างควบคุมแบบวนซ้ำ 2.1 คำสั่ง while 2.2 คำสั่ง do… while 2.3 คำสั่ง for http://itd.htc.ac.th/st_it50/it5016/nidz/Web_C/unit7.html

  6. โครงสร้างควบคุมแบบวนซ้ำโครงสร้างควบคุมแบบวนซ้ำ

  7. คำสั่งวนลูป ความหมายของลูป(loop) ลูป(loop)ในที่นี้มีความหมายว่า การวนซ้ำ ซึ่งการวนซ้ำในทาง ภาษาคอมพิวเตอร์ คือ การทำคำสั่งหรือชุดคำสั่งนั้นซ้ำกันหลายๆครั้ง

  8. Pre-test Loop   การกระทำการกำหนดค่า                                   1จำนวนครั้งในการตรวจสอบ                n+1จำนวนครั้งในการทำคำสั่ง                    nจำนวนครั้งในการปรับปรุง                   nจำนวนครั้งที่น้อยที่สุดในการวน          0 • Post-Test loop                         การกระทำการกำหนดค่า                               1จำนวนครั้งในการตรวจสอบ                nจำนวนครั้งในการทำคำสั่ง                   nจำนวนครั้งในการปรับปรุง                  nจำนวนครั้งที่น้อยที่สุดในการวน         1 • n คือ จำนวนของการวนซ้ำ

  9. คำสั่ง while • คำสั่ง while จะใช้เงื่อนไขเป็นตัวควบคุมลูป ลูป while นี้จะเป็นลูปแบบ pre-test loop ซึ่งจะทำการตรวจสอบเงื่อนไขก่อนที่จะไปทำคำสั่งในลูป while(เงื่อนไข) { คำสั่ง; }

  10. คำสั่ง if คำสั่ง while

  11. a=1; while(a<=10) { printf(“%d”,a); a=a+1; }

  12. int main() { int a; a = 1; ส่วนกำหนดค่า ส่วนเงื่อนไข while(a<=10) { printf(“ a= %d \n”,a); a = a+1; } ส่วนปรับเปลียนค่า printf(“End of job”); }

  13. scanf(“%d”,&a); scanf(“%d”,&b); while (a>b) { b=b+1; a=a-1; printf(“a= %d”,a); printf(“b= %d”,b); } printf(“End of job”);

  14. int count = 1,sum=0; float average; while(count<=5) { scanf (“%d”,&data); sum = sum+data; count = count+1; } average=sum/5.0; printf(“%.2f”,average);

  15. จากผังงาน ให้แก้ไขและเขียนโปรแกรมเพิ่มเติม โดยให้แสดงผลว่ามีเลขคี่และเลขคู่อย่างละกี่จำนวน

  16. สรุป คำสั่ง while ต้องมีอย่างน้อย 3 ส่วนคือ 1 ส่วนกำหนดค่า 2 ส่วนเงื่อนไข 3. ส่วนปรับเปลี่ยนค่า

  17. แบบฝึกหัด • 1. จงเขียนผังงานและโปรแกรม รับค่าจำนวนเต็ม 1 จำนวน แล้วแสดงผลเป็นสูตรคูณ (โดยใช้ pre-test loop ) ตัวอย่าง Enter number : 5 5 * 1= 5 5 * 2 = 10 5 * 3 = 15 : : 5*12 = 60 End of job…. Thanks.

  18. แบบฝึกหัด • 2. จากข้อ 1 กำหนดให้รับเฉพาะค่า 2-12 เท่านั้น (ถ้าไม่ใช้ 2-12 ต้องรับเลขใหม่ ตัวอย่าง Enter number : 15 Enter number : 5 5 * 1= 5 5 * 2 = 10 5 * 3 = 15 : : 5*12 = 60 End of job…. thanks.

  19. แบบฝึกหัด • 3. จงเขียนผังงานและโปรแกรมแสดงการหาผลบวกของเลข 1 ถึง 100 ออกมาบนจอภาพ • 4. จงเขียนโปรแกรมเปลี่ยนค่าองศาเซลเซียส ให้เป็นองศาฟาเรนไฮท์ จาก 0 ถึง 20 องศา โดยให้เพิ่มค่าองศาเซลเซียสขึ้นครั้งละ 1 ให้แสดงเป็นค่าองศาเซลเซียสและองศาฟาเรนไฮท์ ทุกครั้ง ดังนี้ Celsius Fahrenheit (F = C/5*9+32) 0 32.00 1 33.00 2 35.00 3 37.40 (แสดงถึง 20 68.00)

More Related