การควบคุมทิศทางการทำงานของโปรแกรมแบบเลือกทำ � Control Structure (Selection)
1
บทที่ 4-1
การควบคุมทิศทางการทำงานของโปรแกรม
การควบคุมการทำงานของโปรแกรมประกอบด้วย 3 รูปแบบ
2
การควบคุมแบบเลือกทำ
การควบคุมทิศทางการทำงานแบบเลือกทำ (Selection)
3
การควบคุมทิศทางแบบเลือกทำ if
4
if(condition) statement;
if(condition)
{
statement_1;
statement_2;
...
statement_n;
}
ตัวอย่างการใช้งาน if
5
You pass
bye
You pass
bye
score = 60
ผลลัพธ์ที่ได้ ?
score = 59
ผลลัพธ์ที่ได้ ?
score = 80
ผลลัพธ์ที่ได้ ?
bye
1 2 3 4 | scanf("%d",&score); if (score >= 60) printf("You pass\n"); printf("bye"); |
ตัวอย่างการใช้งาน if
6
1 2 3 4 5 6 7 8 9 10 11 12 | #include <stdio.h> int x,y; int main(void) { printf("Enter total score : "); scanf("%d",&x); printf("Enter number of students : "); scanf("%d",&y); if(y==0) printf("Divided by zero !\n"); return(0); } |
ตัวอย่างการใช้งาน if
7
Enter your guess: 123
Enter your guess: 100
Enter your guess: 123
**Right**
Enter your guess: 100
🡨 ไม่แสดงค่าอะไรเลย
ผลลัพธ์ที่ได้ ?
ผลลัพธ์ที่ได้ ?
1 2 3 4 5 6 7 8 9 10 11 | #include <stdio.h> int main(void) { int magic = 123; // กำหนดค่า 123 ให้กับตัวแปร int guess; printf("Enter your guess: "); scanf("%d",&guess); if (guess == magic) printf("**Right**"); return(0); } |
ตัวอย่างการใช้งาน if
จงรับค่าจำนวนเต็มเข้ามา ไม่ว่าจะเป็นเลขบวกหรือลบ จากนั้นจะแสดงข้อมูลเป็นเลขบวกทางหน้าจอ โดยข้อมูลที่รับมานั้นจะถูกตรวจสอบว่าเป็นเลขลบหรือไม่ถ้าเป็นให้คูณด้วย -1
8
1 2 3 4 5 6 7 8 9 10 | int main(void) { int x; printf("INPUT NUMBER: "); scanf("%d",&x); ? ? printf("OUTPUT %d",x); return(0); } |
1 2 3 4 5 6 7 8 9 10 | int main(void) { int x; printf("INPUT NUMBER: "); scanf("%d",&x); if(x<0) x=x*-1; printf("OUTPUT %d",x); return(0); } |
INPUT NUMBER: 10
ผลลัพธ์ที่ได้ ?
INPUT NUMBER: -5
INPUT NUMBER: 10
OUTPUT 10
INPUT NUMBER: -5
OUTPUT 5
การควบคุมทิศทางแบบเลือกทำ if-else
9
if-else ใช้ในกรณีที่มีทางเลือกอยู่สองทางหาก condition เป็น true จะทำ statement ชุด A หาก condition เป็น false จะทำ statement ชุด B
if(condition)
{
statement_A1;
...
statement_An;
}
else
{
statement_B1;
...
statement_Bn;
}
if(condition)
statement_A;
else
statement_B;
ตัวอย่างการใช้งาน if-else
10
ผลลัพธ์ที่ได้ ?
79
You pass Have a nice day
ผลลัพธ์ที่ได้ ?
59
You fail Have a nice day
59
You fail Have a nice day
79
You pass Have a nice day
1 2 3 4 5 6 7 8 9 10 11 12 | #include <stdio.h> int main(void) { int score; scanf("%d",&score); if (score >= 60) printf("You pass "); else printf("You fail "); printf("Have a nice day"); return(0); } |
ตัวอย่างการใช้งาน if-else
11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <stdio.h> int choice; float radius, circum, area; int main(void) { printf("1.Cirumference of the circle\n"); printf("2.Area of the circle\n"); printf("Enter your choice 1 or 2 : "); scanf("%d",&choice); printf("Enter radius of the circle : "); scanf("%f",&radius); if(choice==1) { circum = 2*3.14156*radius; printf("Circumference of the circle =%f\n",circum); } else { area = 3.14156*radius*radius; printf("Area of the circle = %f\n",area); } return(0); } |
Enter your choice 1 or 2 : 1
คำนวณอะไร ?
Enter your choice 1 or 2 : 2
คำนวณอะไร ?
Enter your choice 1 or 2 : 3
คำนวณอะไร ?
1.Cirumference of the circle
2.Area of the circle
ผลลัพธ์ที่ได้ ?
ตัวอย่างการใช้งาน if-else
12
1 2 3 4 5 6 7 8 9 10 11 | int main(void) { int num; printf("enter a number: "); scanf("%d",&num);
return(0); } |
1 2 3 4 5 6 7 8 9 10 11 | int main(void) { int num; printf("enter a number: "); scanf("%d",&num); if (num < 0) //ตรวจสอบว่าค่าที่รับน้อยกว่า 0 หรือไม่ printf("number is negative");//ถ้าน้อยกว่า 0 บอกว่าเป็นค่าลบ else printf("number is positive");//ถ้าค่ามากกว่า 0บอกว่าเป็นค่าบวก return(0); } |
จงรับค่าเลขจำนวนเต็มจากแป้นพิมพ์ จากนั้นจะนำไปเปรียบเทียบกับ 0
ว่าค่าที่รับเข้ามานั้นเป็นบวกหรือลบ จากนั้นจะแสดงประเภทของตัวเลขออกมา
ตัวอย่างการใช้งาน if-else
13
1 2 3 4 5 6 7 8 9 10 11 12 13 | int main(void) { int Number; float cost; printf("Enter number : "); scanf("%d",&Number);
printf("Cost = %0.2f\n",cost); return(0); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | int main(void) { int Number; float cost; printf("Enter number : "); scanf("%d",&Number); if(Number > 10) cost = Number * 6.5; else cost = Number * 7; printf("Cost = %0.2f\n",cost); return(0); } |
ผลลัพธ์ที่ได้ ?
Enter number : 5
Enter number : 5
Cost = 35.00
Enter number : 11
Enter number : 11
Cost = 71.50
โปรแกรมต่อไปนี้เป็นโปรแกรมคำนวณราคาต้นทุนสินค้า ถ้าหากผลิตมากกว่า 10 ชิ้นจะชิ้นละ 6.5 บาท แต่ถ้าไม่เกิน 10 ชิ้นจะราคาชิ้นละ 7 บาท
กฎพื้นฐานการใช้คำสั่ง if
การใช้ statement-if ในการเลือกทำแบบต่างๆ มีกฎที่ควรจำดังต่อไปนี้
14
การใช้คำสั่งเลือกทำแบบ nested if
เป็นการใช้คำสั่งเลือกทำนี้สามารถนำหลายๆ คำสั่งมาซ้อนกันได้ ถ้าต้องการให้มีการเลือกทำหลายทางเลือก
expr1
expr2
statement 1
statement 2
statement 3
if (expr 1)
statement 1;
else
if (expr 2)
statement 2;
else
statement 3;
true
false
true
false
ตัวอย่างการใช้งาน Nested if
16
ผลลัพธ์ที่ได้ ?
Please enter two integer: -2 9
Please enter two integer: -2 9
-2 < 9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <stdio.h> int main(void) { int a,b; printf("Please enter two integer: "); scanf("%d %d",&a,&b); if(a>b) printf("%d > %d\n",a,b); else if(a<b) printf("%d < %d\n",a,b); else printf("%d = %d\n",a,b); return 0; } |
การควบคุมทิศทางแบบเลือกทำ if-else if
17
if(condition_A)
statement_A;
else if(condition_B)
statement_B;
else if(condition_C)
statement_C;
...
else if(condition_Y)
statement_Y;
else
statement_Z;
if(condition_A)
{
statement_A1;
...
}
else if(condition_B)
{
statement_B1;
...
}
else
{
statement_Z;
...
}
Tip : else ตัวสุดท้ายไม่จำเป็นต้องมี
แผนภาพแสดงการทำงาน if-else if
18
Condition A
Statement A
Condition B
Condition B
Statement B
Statement C
Statement D
False
False
False
True
True
True
ตัวอย่างการใช้งาน if-else if
19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <stdio.h> int point; int main(void) { printf("Enter your point : "); scanf("%d",&point); if((point>=80)&&(point<=100)) printf("Grade A\n"); else if((point>=70)&&(point<80)) printf("Grade B\n"); else if((point>=60)&&(point<70)) printf("Grade C\n"); else if((point>=50)&&(point<60)) printf("Grade D\n"); else printf("Grade F\n"); return(0); } |
ตัวดำเนินการเลือกค่า
expression 1 ? expression 2 : expression 3;
a == b ? c-- : c++;
20
ตัวอย่างการใช้งาน ตัวดำเนินการเลือกค่า
การเขียนโปรแกรมตัดเกรดแบบ “ผ่าน” หรือ “ไม่ผ่าน”
printf("%s\n",score >= 60 ? "Passed" : "Failed");
ถ้า x มีค่ามากกว่า 9 จะทำให้ตัวแปร y เท่ากับ 100 แต่ถ้า x มีค่าไม่มากกว่า 9 จะทำให้ตัวแปร y เท่ากับ 200 ตัวดำเนินการเลือกค่าดังกล่าวจะใช้แทนคำสั่ง if ที่มีการเขียนดังนี้
x = 10;
if (x > 9) y = 100;
else y = 200;
ถ้าต้องการเขียนแบบตัวดำเนินการเลือกค่า ทำได้ดังนี้
x = 10;
y = x > 9 ? 100 : 200;
21
ตัวอย่างการใช้งาน if-else และตัวดำเนินการเลือกค่า
จงทายตัวเลข ถ้าตัวเลขที่ใส่เข้าไปถูกต้องเครื่องจะแจ้งว่าถูกต้อง ถ้าไม่ถูกต้องเครื่องจะตอบว่าตัวเลขที่ใส่ไปมากกว่าหรือน้อยกว่าโดยใช้ตัวดำเนินการเลือกทำในโปรแกรม
22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | int main(void) { int magic = 123; //กำหนดตัวเลขเป็น 123 int guess; printf("Enter your guess : "); scanf("%d",&guess);
return(0); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | int main(void) { int magic = 123; //กำหนดตัวเลขเป็น 123 int guess; printf("Enter your guess : "); scanf("%d",&guess); if(guess == magic) { printf("**Right**\n"); printf("%d is the magic number ",magic); } else (guess > magic)? printf("High") : printf("Low"); return(0); } |
Enter your guess : 100
Low
Enter your guess : 321
High
Enter your guess : 123
**Right**
123 is the magic number
ผลลัพธ์ที่ได้ ?
การควบคุมทิศทางแบบเลือกทำของ switch
23
switch ใช้ในกรณีมีทางเลือกให้ทำหลายทางจากเงื่อนไขร่วมกัน
switch(variable)
{
case constant_A : Statement_A1;
Statement_A2;
...
break;
case constant_B : Statement_B1;
Statement_B2;
...
break;
case constant_C : Statement_C1;
Statement_C2;
...
break;
...
default : statement ZZ;
}
การควบคุมทิศทางแบบเลือกทำด้วยคำสั่ง switch
24
การควบคุมทิศทางแบบเลือกทำด้วยคำสั่ง switch
25
แผนภาพแสดงการทำงานของ switch
26
case 1
Statement 1
break
Expression
case 2
Statement 2
break
case n
Statement n
break
default
Statement s
break
Matched
Matched
Matched
Matched
Unmatched
Unmatched
Unmatched
ตัวอย่างการใช้งาน switch
27
Enter your grade : A
80-100
Enter your grade : D
50-59
Enter your grade : b
0-49
ทำไมป้อน b แล้วได้ผล
0-49 ?
ผลลัพธ์ที่ได้ ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <stdio.h> char grade; int main(void) { printf("Enter your grade : "); scanf("%c",&grade); switch(grade) { case 'A': printf("80-100\n"); break; case 'B': printf("70-79\n"); break; case 'C': printf("60-69\n"); break; case 'D': printf("50-59\n"); break; default: printf("0-49\n"); } return(0); } |
ตัวอย่างการใช้งาน switch
28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <stdio.h> int choice; float radius, circum, area; int main(void) { printf("1.Cirumference of the circle\n"); printf("2.Area of the circle\n"); printf("Enter your choice 1 or 2 : "); scanf("%d",&choice); printf("Enter radius of the circle : "); scanf("%f",&radius); switch(choice) { case 1 : circum = 2*3.14156*radius; printf("Circumference of the circle =%f\n",circum); break; case 2 : area = 3.14156*radius*radius; printf("Area of the circle = %f\n",area); break; } return(0); } |
Enter your choice 1 or 2 : 1
คำนวณอะไร ?
Enter your choice 1 or 2 : 2
คำนวณอะไร ?
Enter your choice 1 or 2 : 3
คำนวณอะไร ?
1.Cirumference of the circle
2.Area of the circle
ผลลัพธ์ที่ได้ ?
การควบคุมทิศทางการทำงานของโปรแกรมแบบเลือกทำ � Control Structure (Selection)
29
จบบทที่ 4-1