1 / 16

Chapter 4 คำสั่งควบคุมทิศทาง

Chapter 4 คำสั่งควบคุมทิศทาง. PROGRAMMIMG I. คำสั่งควบคุมทิศทาง. ใช้ในกรณีที่โจทย์ปัญหาที่ต้องมีการเลือก หรือมีเงื่อนไขในการเลือกทำงาน เช่น ถ้าสถานการณ์เป็น A ให้ทำงานอย่างหนึ่ง ส่วนถ้าสถานการณ์เป็น B ก็ให้ทำงานอีกอย่างหนึ่งแทน

moe
Download Presentation

Chapter 4 คำสั่งควบคุมทิศทาง

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. Chapter 4คำสั่งควบคุมทิศทาง PROGRAMMIMG I

  2. คำสั่งควบคุมทิศทาง • ใช้ในกรณีที่โจทย์ปัญหาที่ต้องมีการเลือก หรือมีเงื่อนไขในการเลือกทำงาน เช่น ถ้าสถานการณ์เป็น A ให้ทำงานอย่างหนึ่ง ส่วนถ้าสถานการณ์เป็น B ก็ให้ทำงานอีกอย่างหนึ่งแทน • ในภาษาซี คำสั่งที่ใช้ควบคุมทิศทางการทำงานของโปรแกรม มี 2 ประเภทหลักๆ • If-else statement • Switch-case statement

  3. if - else statement • if (condition) statement; • if (condition) { statement 1; statement 2; . . statement n; } • if (condition) statement 1; else statement 2;

  4. if - else statement condition condition เท็จ เท็จ จริง จริง statement statement statement

  5. ตัวอย่าง Start Get C n C != D y ‘a’ <= C <= ‘z’ n y C ==‘\n’ n Print C y Print double new line Stop

  6. ตัวอย่าง • จงเขียนโปรแกรมภาษาซี เพื่อหาค่ามากที่สุดของเลขจำนวนเต็ม 3 จำนวน • วิเคราะห์ • ผลลัพธ์ ค่าที่มากที่สุด (max) • ข้อมูลนำเข้า เลขจำนวนเต็ม 3 จำนวน (a, b, c) • วิธีประมวล • รับข้อมูล • เปรียบเทียบหาค่าที่มากที่สุด • แสดงผล

  7. start จริง เท็จ a > b Get a, b, c max = a max = b max < c เท็จ จริง max = c Print max stop หมายเหตุ a, b, c เป็นเลขจำนวนเต็ม

  8. #include <stdio.h> void main( ) { int a, b, c, max; printf(“Enter integer \na = ”); scanf(“%d”, &a); printf(“b = ”); scanf(“%d”, &b); printf(“c = ”); scanf(“%d”, &c); if (a > b) max = a; else max = b; if (max < c) max = c; printf(“The maximum is %d”, max); }

  9. ตัวอย่าง จงเขียนโปรแกรมเพื่อเปรียบเทียบตัวเลข 2 จำนวน โดยโปรแกรมจะให้ผู้ใช้ป้อนจำนวนเต็มเข้ามา 2 จำนวนเพื่อเปรียบเทียบ แล้วจึงแสดงผลการเปรียบเทียบออกทางหน้าจอเพื่อแจ้งว่า จำนวนแรกมากกว่า, น้อยกว่า, หรือเท่ากับจำนวนหลัง

  10. #include<stdio.h> Main() { int first, second; printf(“Enter first number: ”); scanf(“%d”,&first); printf(“Enter second number: ”); scanf(“%d”,&second); if(first > second) printf(“%d is bigger than %d\n”,first,second); else if(first == second) printf(“%d is equal %d\n”,first,second); else printf(“%d is less than %d\n”,first,second); }

  11. switch–case statement • เป็นคำสั่งเพื่อเลือกการทำงานจะใช้ในกรณีที่มีทางเลือกให้ทำงานหลายทาง โดยใช้การตรวจสอบเงื่อนไขร่วมกันเพียงครั้งเดียว ผลการตรวจสอบเงื่อนไข จะถูกนำไปพิจารณาเพื่อเลือกว่าจะทำงานตามทางเลือกใด • รูปแบบ switch (expression ) { caseinteger-const-1:statement-1; break; caseinteger-const-2:statement-2; break; caseinteger-const-n:statement-n; break; : : default: statement; }

  12. ตัวอย่าง #include<stdio.h> main() { charchoice; scanf(“%c”, &choice); switch (choice) { case ‘1’ : printf(“You get 1\n”); case ‘2’ : printf(“You get 2\n”); break; default : printf(“You get neither 1 nor 2\n”); }

  13. การบ้านบทที่ 3 • จากส่วนของโปรแกรมต่อไปนี้ ข้อใดเป็นผลลัพธ์จากการทำงานของโปรแกรม 1) ByeGood Luck 2) HelloGood Luck 3) Hello4) Bye int a=2,b=5,c=a++*b++; if(c++ >= 10) printf(“Hello”); Else Printf(“Bye”); Printf(“Good Luck”);

  14. int a; scanf(“%d”,&a); switch(a) { case 0: case 1: printf(“red”); case 2: printf(“blue”); case 3: printf(“green”); case 4: printf(“yell”); } 2. จากส่วนของโปรแกรมต่อไปนี้ จงหาว่าผลลัพธ์จะออกมาเป็นอย่างไร ถ้าป้อนค่าให้ตัวแปร a เป็น 2 1) red 2) blue 3) green 4) bluegreenyellow

  15. 3. จงเขียนโปรแกรมเพื่อคิดเกรดให้นักศึกษา โดยมีการรับค่าคะแนนเข้ามา (คะแนนต้องไม่เกิน 100 คะแนน) แล้วแสดงเกรดออกทางจอภาพ

  16. 4. จงเขียนโปรแกรมเพื่อตรวจสอบอักขระที่รับเข้ามาว่าเป็นสระ(vowel)(A,a,E,e,I,i,O,o,U,u) หรือพยัญชนะ (letter) พร้อมทั้งแสดงข้อความออกทางหน้าจอ

More Related