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

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

คำสั่งเงื่อนไข (Condition Statement)

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


งานนำเสนอเรื่อง: "คำสั่งเงื่อนไข (Condition Statement)"— ใบสำเนางานนำเสนอ:

1 คำสั่งเงื่อนไข (Condition Statement)
ชื่อ ... เลขที่ ... ชั้น ...

2 คำสั่งเงื่อนไข if if (เงื่อนไข) { คำสั่งที่ 1 ; } คำสั่งที่ 2 ; รูปแบบ

3 โฟลชาร์ตแสดงการทำงานของคำสั่งเงื่อนไข if
เท็จ หากเงื่อนไขที่กำหนดเป็นจริงแล้ว คำสั่ง ต่างๆ ที่อยู่ภายในบล็อกของเงื่อนไข if ก็จะ ได้รับการประมวลผล (ซึ่งอาจมีมากกว่า 1 คำสั่ง) แต่ถ้าตรวจสอบแล้วพบว่าเงื่อนไข เป็นเท็จ คำสั่งที่อยู่ภายในบล็อกของเงื่อนไข if ก็จะไม่ได้รับการประมวลผล คือ จะข้าม ไปทำการประมวลผลคำสั่งที่อยู่ถัดจาก บล็อกของ if ทันที จริง คำสั่งที่ 1 คำสั่งที่ 2

4 ตัวอย่างโปรแกรมแสดงการทำงานของคำสั่งเงื่อนไข if
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: #include <stdio.h> #include <stdlib.h> main() { int age; printf("How old are you? : "); scanf("%d", &age ); if(age<18) printf("You are young\n"); printf("You are %d years old", age); system("PAUSE"); } ... ... ผลลัพธ์ของโปรแกรม รันครั้งที่ 1 (ให้ป้อนค่าเป็น 15) รันครั้งที่ 2 (ให้ป้อนค่าเป็น 18)

5 ตัวอย่างโปรแกรมแสดงการทำงานของคำสั่งเงื่อนไข if
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: #include <stdio.h> #include <stdlib.h> main() { int age; printf("How old are you? : "); scanf("%d", &age ); if(age<18) { printf("You are less than 18 years old\n"); printf("You are young\n"); } printf("You are %d years old"); system("PAUSE");

6 ผลลัพธ์ของโปรแกรม รันครั้งที่ 1 (ให้ป้อนค่าเป็น 15) รันครั้งที่ 2 (ให้ป้อนค่าเป็น 18) ตัวอย่างโปรแกรมที่ 1 และ 2 ต่างกันอย่างไร ...

7 อธิบายโปรแกรม ...

8 คำสั่งเงื่อนไข if-else
คำสั่งที่ 1 ; } else { คำสั่งที่ 2 ; คำสั่งที่ 3 ; รูปแบบ

9 โฟลชาร์ตแสดงการทำงานของคำสั่งเงื่อนไข if-else
เป็นคำสั่งที่ช่วยให้การตรวจสอบเงื่อนไข สมบูรณ์ขึ้น โดยหากตรวจสอบเงื่อนไขของคำสั่ง if แล้วเป็นเท็จ ก็จะเข้ามาทำงานภายในบล็อกของคำสั่ง else แทน กล่าวคือ หากตรวจสอบเงื่อนไขแล้วเป็นจริง ก็จะประมวลผลคำสั่งในบล็อกของ if แต่หากเงื่อนไขเป็นเท็จ ก็จะประมวลผลคำสั่งในบล็อกของ else แทน และเมื่อตรวจสอบเงื่อนไขและประมวลผลตามคำสั่งเงื่อนไข if-else เรียบร้อยแล้ว ก็จะทำงานตามคำสั่งที่อยู่ถัดจาก if-else นั้นต่อไป เงื่อนไข เท็จ จริง คำสั่งที่ 1 คำสั่งที่ 2 คำสั่งที่ 3

10 ตัวอย่างโปรแกรมแสดงการทำงานของคำสั่งเงื่อนไข if-else
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: #include <stdio.h> #include <stdlib.h> main() { int points; printf("Please enter points : "); scanf("%d", &points ); if(points >= 50) printf("Pass exam…Congratulations\n"); else printf("Fail…Attempt\n"); printf("Bye bye…see you again next semester"); system("PAUSE"); }

11 ผลลัพธ์ของโปรแกรม รันครั้งที่ 1 (ให้ป้อนค่าเป็น 49) รันครั้งที่ 2 (ให้ป้อนค่าเป็น 79)

12 อธิบายโปรแกรม ...

13 ตัวอย่างโปรแกรมแสดงการทำงานของคำสั่งเงื่อนไข if-else
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: #include <stdio.h> #include <stdlib.h> #define THIS_YEAR 2009 main() { int year; printf("What year were you born? : "); scanf("%d", &year); if(year > THIS_YEAR) { printf("Please insert year\n"); printf("Insert in C.E. format\n"); } else { year = THIS_YEAR – year; printf("You are %d years old\n", year); printf("Finish! Goodbye"); system("PAUSE");

14 ผลลัพธ์ของโปรแกรม รันครั้งที่ 1 (ให้ป้อนค่าเป็น 2532) รันครั้งที่ 2 (ให้ป้อนค่าเป็น 1991)

15 อธิบายโปรแกรม ...

16 คำสั่งเงื่อนไข if ซ้อน if
คำสั่งที่ 1 ; } else if (เงื่อนไขที่ 2) { คำสั่งที่ 2 ; else if (เงื่อนไขที่ 3) { คำสั่งที่ 3 ; else { คำสั่งที่ 4 ; คำสั่งที่ 5 ; รูปแบบ

17 โฟลชาร์ตแสดงการทำงานของคำสั่งเงื่อนไข if ซ้อน if
เท็จ เงื่อนไขที่ 1 จริง เงื่อนไขที่ 1 เงื่อนไขที่ 1 คำสั่งที่ 1 คำสั่งที่ 2 คำสั่งที่ 3 คำสั่งที่ 4 คำสั่งที่ 5

18 ตัวอย่างโปรแกรมแสดงการทำงานของคำสั่งเงื่อนไข if-else
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: #include <stdio.h> #include <stdlib.h> main() { int points; printf("Please enter points : "); scanf("%d", &points ); if(points >= 80) { printf("Congratulations\n"); printf("C Language programming subject\n"); printf("You get grade A\n"); } else if(points >= 70) printf("You get grade B\n"); if(points >= 60) printf("You get grade C\n"); if(points >= 50) printf("You get grade D\n"); printf("You get grade F\n"); printf("See you again! Next course"); system("PAUSE");

19 ตัวอย่างโปรแกรมแสดงการทำงานของคำสั่งเงื่อนไข if ซ้อน if
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: #include <stdio.h> #include <stdlib.h> main() { int points; printf("Please enter points : "); scanf("%d", &points ); if(points >= 80) { printf("Congratulations\n"); printf("C Language programming subject\n"); printf("You get grade A\n"); } else if(points >= 70) printf("You get grade B\n"); else if(points >= 60) printf("You get grade C\n"); else if(points >= 50) printf("You get grade D\n"); else printf("You get grade F\n"); printf("See you again! Next course"); system("PAUSE");

20 ผลลัพธ์ของโปรแกรม รันครั้งที่ 1 (ให้ป้อนค่าเป็น 55) รันครั้งที่ 2 (ให้ป้อนค่าเป็น 81) รันครั้งที่ 2 (ให้ป้อนค่าเป็น 45)

21 ตัวอย่างโปรแกรมที่ 1 และ 2 ต่างกันอย่างไร
...


ดาวน์โหลด ppt คำสั่งเงื่อนไข (Condition Statement)

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


Ads by Google