1 of 29

การควบคุมทิศทางการทำงานของโปรแกรมแบบเลือกทำ � Control Structure (Selection)

1

บทที่ 4-1

2 of 29

การควบคุมทิศทางการทำงานของโปรแกรม

การควบคุมการทำงานของโปรแกรมประกอบด้วย 3 รูปแบบ

  • การควบคุมทิศทางการทำงานแบบลำดับ (Sequence)
  • การควบคุมทิศทางการทำงานแบบเลือกทำ (Selection)
  • การควบคุมทิศทางการทำงานแบบทำซ้ำ (Repetition)

2

3 of 29

การควบคุมแบบเลือกทำ

การควบคุมทิศทางการทำงานแบบเลือกทำ (Selection)

  • คำสั่ง if
  • คำสั่ง if-else
  • คำสั่ง if-else if
  • คำสั่ง switch

3

4 of 29

การควบคุมทิศทางแบบเลือกทำ if

4

  • if ใช้ในกรณีที่มีทางเลือกอยู่เพียงทางเลือกเดียวคือทำหรือไม่ทำ

  • condition : เงื่อนไขที่ให้ผลลัพธ์เป็น true หรือ false
  • statement : คำสั่งที่จะให้ทำหากผลลัพธ์ของเงื่อนไขเป็น true
  • หากมีคำสั่งมากกว่าหนึ่งคำสั่งจะต้องมีเครื่องหมายปีกกาครอบคำสั่งไว้

if(condition) statement;

if(condition)

{

statement_1;

statement_2;

...

statement_n;

}

5 of 29

ตัวอย่างการใช้งาน 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");

6 of 29

ตัวอย่างการใช้งาน 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);

}

7 of 29

ตัวอย่างการใช้งาน 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);

}

8 of 29

ตัวอย่างการใช้งาน 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

9 of 29

การควบคุมทิศทางแบบเลือกทำ 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;

10 of 29

ตัวอย่างการใช้งาน 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);

}

11 of 29

ตัวอย่างการใช้งาน 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

ผลลัพธ์ที่ได้ ?

12 of 29

ตัวอย่างการใช้งาน 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

ว่าค่าที่รับเข้ามานั้นเป็นบวกหรือลบ จากนั้นจะแสดงประเภทของตัวเลขออกมา

13 of 29

ตัวอย่างการใช้งาน 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 บาท

14 of 29

กฎพื้นฐานการใช้คำสั่ง if

การใช้ statement-if ในการเลือกทำแบบต่างๆ มีกฎที่ควรจำดังต่อไปนี้

  • นิพจน์(condition) ของ if จะต้องอยู่ในวงเล็บและให้ผลลัพธ์ออกมาเป็นจริงหรือเท็จ
  • ในส่วนของ if และ else ไม่ต้องมีเครื่องหมาย semicolon
  • Statement หลัง if และหลัง else สามารถเป็น Statement รวมได้แต่ต้องอยู่ในเครื่องหมายปีกกา
  • Statement หลัง if และ statement หลัง else สามารถสลับกันได้แต่ต้องทำ complement กับ นิพจน์(condition) ของ if

14

15 of 29

การใช้คำสั่งเลือกทำแบบ 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

16 of 29

ตัวอย่างการใช้งาน 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;

}

17 of 29

การควบคุมทิศทางแบบเลือกทำ if-else if

17

  • if-else ใช้ในกรณีที่มีทางเลือกอยู่มากกว่าสองทางเลือก โดยแต่ละทางเลือกมีเงื่อนไขต่างกัน

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 ตัวสุดท้ายไม่จำเป็นต้องมี

18 of 29

แผนภาพแสดงการทำงาน if-else if

18

Condition A

Statement A

Condition B

Condition B

Statement B

Statement C

Statement D

False

False

False

True

True

True

19 of 29

ตัวอย่างการใช้งาน 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);

}

20 of 29

ตัวดำเนินการเลือกค่า

  • จะมีการใช้เครื่องหมาย question mark (?) และเครื่องหมาย colon (:) กระทำกับนิพจน์สามนิพจน์ เราสามารถนำมาใช้แทนการใช้คำสั่ง if แบบง่ายๆ ได้ โดยมีรูปแบบคือ

expression 1 ? expression 2 : expression 3;

  • ถ้าหากการกระทำของนิพจน์แรกเป็นจริงหรือมีค่าไม่เท่ากับศูนย์ โปรแกรมจะไปทำนิพจน์ที่สอง ถ้าเป็นเท็จจะไปทำนิพจน์ที่สาม ดังตัวอย่างต่อไปนี้

a == b ? c-- : c++;

  • จากตัวอย่างถ้าหากค่าในตัวแปร a เท่ากับ b จะทำการลดค่าตัวแปร c ลงหนึ่งค่า ถ้าหากค่าในตัวแปร a ไม่เท่ากับ b จะทำการเพิ่มค่าตัวแปร c ขึ้นหนึ่งค่า

20

21 of 29

ตัวอย่างการใช้งาน ตัวดำเนินการเลือกค่า

การเขียนโปรแกรมตัดเกรดแบบ “ผ่าน” หรือ “ไม่ผ่าน”

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

22 of 29

ตัวอย่างการใช้งาน 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

ผลลัพธ์ที่ได้ ?

23 of 29

การควบคุมทิศทางแบบเลือกทำของ 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;

}

24 of 29

การควบคุมทิศทางแบบเลือกทำด้วยคำสั่ง switch

24

  • variable : ตัวแปรหรือนิพจน์ที่ให้ผลเป็นข้อมูลแบบ int หรือ char
  • constant_A, constant_B,… : ค่าคงที่ชนิดเดียวกับตัวแปร variable หากค่าของ variable เท่ากับค่าคงที่ตัวใด โปรแกรมจะทำคำสั่ง case นั้น
  • break : ใช้คั่นระหว่าง case ให้โปรแกรมออกจากทำงานของ switch หากไม่ใส่ตัวคั่น โปรแกรมจะทำการเปรียบเทียบ variable กับ constant ตัวถัดไปเรื่อยๆ
  • default : หาก variable ไม่ตรงกับ constant ใดเลย โปรแกรมจะทำตามคำสั่งใน default

25 of 29

การควบคุมทิศทางแบบเลือกทำด้วยคำสั่ง switch

  • คำสั่ง if สามารถตรวจสอบความสัมพันธ์ หรือลอจิกได้
  • switch ไม่สามารถตรวจสอบหลายๆ เงื่อนไขภายในนิพจน์เดียวกันได้
  • ถ้าค่าคงที่เป็นตัวอักษร คำสั่ง switch จะมองเป็นเลขจำนวนเต็ม
  • ค่า default จะมีหรือไม่มีก็ได้

25

26 of 29

แผนภาพแสดงการทำงานของ 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

27 of 29

ตัวอย่างการใช้งาน 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);

}

28 of 29

ตัวอย่างการใช้งาน 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

ผลลัพธ์ที่ได้ ?

29 of 29

การควบคุมทิศทางการทำงานของโปรแกรมแบบเลือกทำ � Control Structure (Selection)

29

จบบทที่ 4-1