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

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

Control Statement if..else switch..case for while do-while.

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


งานนำเสนอเรื่อง: "Control Statement if..else switch..case for while do-while."— ใบสำเนางานนำเสนอ:

1 Control Statement if..else switch..case for while do-while

2 คำสั่ง if

3 การทำงานของโปรแกรมโดยปกติ โครงสร้างลำดับ (Sequence Structure)
เป็นการดำเนินการ คำสั่ง (statement) จากบนลงล่าง ทีละคำสั่ง add grade to total total = total + grade; count = count + 1; add 1 to count ผังงาน (flowchart) คำสั่งใน C

4 คำสั่ง if คำสั่งสำหรับการตรวจสอบเงื่อนไข if if..else if..else..if
ใช้ควบคุมการทำงานแบบมีทางเลือก หากเงื่อนไขเป็นจริง  ให้ทำงานคำสั่งต่อจากเงื่อนไข หากเงื่อนไขเป็นเท็จ  ให้ทำงานคำสั่งในชุดถัดไป คำสั่งสำหรับการตรวจสอบเงื่อนไข if if..else if..else..if

5 สัญลักษณ์ที่ใช้ การเปรียบเทียบ เครื่องหมาย เท่ากัน == ไม่เท่ากัน !=
การเปรียบเทียบ เครื่องหมาย เท่ากัน == ไม่เท่ากัน != น้อยกว่า < น้อยกว่าหรือเท่ากับ <= มากกว่า > มากกว่าหรือเท่ากับ >=

6 การเปรียบเทียบ ค่าคงที่กับค่าคงที่ 5 > 3 20.5 < 30.25
ตัวแปรกับค่าคงที่ x != 10 a <= 150.5 ตัวแปรกับตัวแปร num1 = = num2 a >= b นิพจน์กับค่าคงที่ x + y > 5 y / 2 != 0

7 ในกรณีที่มีหลายคำสั่ง ต้องใส่เครื่องหมาย { }ครอบไว้
1.คำสั่ง if รูปแบบ if (เงื่อนไข) คำสั่ง; //กรณีที่เงื่อนไขเป็นจริง คำสั่งชุดต่อไป; ผลลัพธ์ของการตรวจสอบ - จริง (true) จะทำงานคำสั่งใต้ if ไม่จริง (false) จะข้ามไปทำชุดคำสั่งต่อไป ในกรณีที่มีหลายคำสั่ง ต้องใส่เครื่องหมาย { }ครอบไว้

8 โครงสร้างการเลือกแบบ if
grade >= 50 N Y write “Passed” if ( grade >= 50 ) printf( “Passed\n” ); ผังงาน (flowchart) คำสั่งใน C

9 printf(“You failed in this subject\n”);
ตัวอย่าง if(grade < 50) printf(“You failed in this subject\n”); if(age > 60) printf(“You are old”);

10 ตัวอย่าง Input an integer value for x: 10
#include <stdio.h> #include <conio.h> #include<stdlib.h> void main() { int x, y; system(“cls”); printf( "Input an integer value for x: " ); scanf( "%d", &x ); printf( "Input an integer value for y: " ); scanf( "%d", &y ); if ( x == y ) printf( "x is equal to y\n" ); if ( x > y ) printf( "x is greater than y\n" ); if ( x < y ) printf( "x is smaller than y\n" ); getch(); } Input an integer value for x: 10 Input an integer value for y: 5 x is greater than y Input an integer value for x: 5 Input an integer value for y: 10 x is smaller than y Input an integer value for x: 5 Input an integer value for y: 5 x is equal to y

11 ตัวอย่าง How old are you? 16 You are less than 18 years old
/* The use of if with a block of statements */ #include <stdio.h> #include <conio.h> void main() { int age; clrscr(); printf( "How old are you? "); scanf( "%d", &age ); if ( age < 18 ) printf( "You are less than 18 years old\n" ); printf( "You are so young\n" ); } printf( "You are %d years old\n", age ); How old are you? 16 You are less than 18 years old You are so young You are 16 years old How old are you? 22 You are 22 years old

12 2. คำสั่ง if..else if(เงื่อนไข)
รูปแบบ if(เงื่อนไข) คำสั่งชุดที่ 1 ; //กรณีเงื่อนไขเป็นจริง else คำสั่งชุดที่ 2; //กรณีเงื่อนไขเป็นเท็จ คำสั่งชุดต่อไป; ผลลัพธ์ของการตรวจสอบ - จริง (true) จะทำงานคำสั่งชุดที่ 1 ไม่จริง (false) จะข้ามไปทำชุดคำสั่งชุดที่ 2

13 ตัวอย่าง ถ้า มีค่าเท่ากับ 5 ให้แสดงคำว่า Equal แต่ถ้าไม่ใช่ ให้แสดงคำว่า Not Equal ถ้า Number หารด้วย 10 ลงตัว ให้แสดงคำว่า Good แต่ถ้าไม่ใช่ ให้แสดงคำว่า Bad ตอบ if (10+20==5) printf(“Equal”); else printf(“Not Equal”); ตอบ if (Number%10==0) printf(“Good”); else printf(“Bad”);

14 ตัวอย่าง Enter number : 20 This Number Less than or equal 100
#include<stdio.h> int main() { int number; printf(“Enter number : ”); scanf(“%d”,&number); if (number > 100) printf(“This Number more than 100\n”); else printf(“This Number Less than or equal 100\n”); return 0; } ตัวอย่าง Enter number : 20 This Number Less than or equal 100

15 ตัวอย่าง /* Determine if a number is odd or even */
#include <stdio.h> #include <conio.h> #include <stdlib.h> void main() { int num; system(“cls”); printf( "Enter a number: " ); scanf( "%d", &num ); if ( num % 2 == 0 ) printf( "Even number\n" ); else printf( "Odd number\n" ); } Enter a number: 5 Odd number Enter a number: 10 Even number

16 ข้อควรระวัง อย่าให้การเว้นระยะ ย่อหน้า (indentation)
int x = 5, y = 5; ข้อควรระวัง คำสั่ง (Statements) ค่า x y อย่าให้การเว้นระยะ ย่อหน้า (indentation) ทำให้เกิดความสับสนได้ if (x == 10) x = x + 1; y = y + 1; 5 6 if ( x == 10 ) x = x + 1; y = y + 1; 5 6 ภาษา C จะไม่สนใจ ช่องว่าง (space), แท็บ (tab), การขึ้นบรรทัดใหม่ (newline) if ( x == 10 ) { x = x + 1; y = y + 1; } 5 5 การใส่เครื่องหมาย ; หลัง เงื่อนไข (condition) ใน if มีผลทำให้เกิด ความผิดพลาดทางตรรกะ (logic error) ได้ if ( x == 10 ); x = x + 1; y = y + 1; 6 6

17 3. คำสั่ง if..else..if รูปแบบ if(เงื่อนไขที่ 1)
คำสั่งชุดที่ 1 ; //กรณีเงื่อนไขที่ 1 เป็นจริง else if(เงื่อนไขที่ 2) คำสั่งชุดที่ 2; //กรณีเงื่อนไขที่ 2 เป็นจริง ... else คำสั่งชุดที่ n;//กรณีที่นอกเหนือจากเงื่อนไขข้างต้น คำสั่งชุดต่อไป;

18 ตัวอย่าง Enter score: 76 Grade is C Enter score: 92 Grade is A
#include <stdio.h> #include <conio.h> #include <stdlib.h> void main() { int score; char grade; system(“cls”); printf( "Enter score: " ); scanf( "%d", &score ); if ( score >= 90 ) grade = 'A'; else if ( score >= 80 ) grade = 'B'; else if ( score >= 70 ) grade = 'C'; else if ( score >= 60 ) grade = 'D'; else grade = 'F'; printf( "Grade is %c\n", grade ); } Enter score: 76 Grade is C Enter score: 92 Grade is A

19 ตัวอย่าง #include<stdio.h> int main() { int score;
printf(“Enter score : ”); scanf(“%d”,&score); if (score >= 80) printf(“Excellent\n”); else if (score >= 70) printf(“Good\n”); else printf(“Fair\n”); return 0; } Enter score: 76 Good

20 ข้อสังเกต การจับคู่ if/else ใช้ หลักที่ว่า else จะจับคู่
if ( condition1 ) { if ( condition2 ) statement1; else statement2; } statement3; if ( condition1 ) if ( condition2 ) statement1; else statement2; statement3; การจับคู่ if/else ใช้ หลักที่ว่า else จะจับคู่ กับ if ตัวก่อนหน้านี้ที่ ใกล้ที่สุด if ( conditionA ) statementA; else if ( conditionB ) statementB; statementC; if ( conditionA ) statementA; else { if ( conditionB ) statementB; statementC; } เราสามารถใช้ เครื่องหมายวงเล็บ { } กับการใช้ if แบบซ้อน (nested if) ได้ เพื่อ ความชัดเจน

21 เพิ่มเติม การเชื่อมระหว่าง condition (ถ้ามีเงื่อนไขหลายอันที่ต้องตรวจสอบ) แต่ละเงื่อนไขไม่จำเป็นต้องใส่วงเล็บ AND -> && เงื่อนไขทุกเงื่อนไขต้องเป็นจริงทั้งหมด - จริง OR -> || จริงบางเงื่อนไข - จริง ตัวอย่าง if (x > 10 && x <= 15) printf(“OK”); if ((x > 10) || (y > 15)) x ต้องมากกว่า 10 และ x ต้องน้อยกว่าหรือเท่ากับ 15 จึงทำให้เงื่อนไขเป็นจริง x ต้อง > 10 หรือ y ต้อง >15 เป็นจริงอันใดอันนึง จึงทำให้เงื่อนไขเป็นจริง

22 แบบฝึกหัด จงเขียนโปรแกรมรับค่าเลขจำนวนเต็ม 2 จำนวนจากแป้นพิมพ์ จากนั้นให้ตรวจสอบเลขจำนวนเต็มตัวแรกมากกว่าเลขจำนวนเต็มตัวที่สองหรือไม่ ถ้าใช่ให้หาผลบวกของตัวเลข 2 จำนวนและแสดงผลออกทางจอภาพ ถ้าไม่ใช่ให้นำตัวเลขตัวแรกคูณตัวที่สองและแสดงผลออกทางจอภาพ ตัวอย่างหน้าจอการรับค่าและแสดงผล Input Enter Number1 : 10 Enter Number2 : 7 Output Result = 17 22

23 แบบฝึกหัด จงเขียนโปรแกรมรับค่าตัวเลข 2 ตัวและ choiceจากแป้นพิมพ์จากนั้นให้ตรวจสอบค่าchoice - ถ้าchoice เป็น ‘A’ ให้นำเลข 2 ตัวนั้นมาบวกกัน - ถ้าchoice เป็น ‘S’ ให้นำเลข 2 ตัวนั้นมาลบกัน - ถ้าchoice เป็น‘M’ ให้นำเลข 2 ตัวนั้นมาคูณกัน เมื่อได้ผลลัพธ์ให้แสดงผลลัพธ์นั้นออกทางหน้าจอ


ดาวน์โหลด ppt Control Statement if..else switch..case for while do-while.

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


Ads by Google