Chapter3 Control 154 p4 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

บทที่ 3 วัตถุประสงค
• เขาใจการทํางานของคําสั่งแบบทางเลือก ไดแก
โครงสรางควบคุม − if, if–else ,switch-case

Control Structures • เขาใจการทํางานของคําสั่งแบบทําซ้ํา ไดแก


− for , while และ do-while
คณาจารยภาควิชาวิศวกรรมคอมพิวเตอร • สามารถนําคําสั่งตางๆไปประยุกตใชเพื่อควบคุมการทํางานของ
introcom@coe.psu.ac.th
โปรแกรมตามที่ตองการได

Department of Computer Engineering, PSU Department of Computer Engineering, PSU 2

242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

เนื้อหาในบทเรียน โครงสรางควบคุม
• เครื่องหมายที่ใชในการเปรียบเทียบ
โดยปกติโปรแกรมจะทํางานตอเนื่องตามลําดับคําสั่ง
เรียกวา Sequence หรือการทํางานแบบ Sequential
• โครงสรางการทํางานของคําสั่ง if else และ switch case
โครงสรางควบคุม เปนการกําหนดการทํางานของโปรแกรม
• ทําไมตองทําซ้ํา?? ใหเปนไปในทิศทางที่ตองการ หรือเรียกวา flow of control
• โครงสรางการทํางานของคําสั่ง for, while และ do while คําสัง่ ที่ใชในการควบคุมแบงไดเปน 2 ประเภทคือ
• การเปลี่ยนคําสั่งจาก for เปน do และ จากคําสั่ง do เปน for โครงสรางแบบทางเลือก (Selection)
โครงสรางแบบทําซ้าํ (Repetition)

Department of Computer Engineering, PSU 3 Department of Computer Engineering, PSU 4


242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

เงื่อนไข Condition เครื่องหมายเปรียบเทียบ


ทั้งโครงสรางแบบทางเลือก และการทําซ้ํา มีเงื่อนไข เปน
•เครือ่ งหมายที่ใชในการเปรียบเทียบ (relational operators)และใหผลลัพธเปน
ตัวกําหนด การดําเนินไปของโปรแกรม
จริง หรือ เท็จ ไดแก
เงื่อนไข หรือ condition เปนนิพจน ที่มีคาทางตรรกะ(logic) หรือ
เรียกวาคา boolean คือ คา จริง(True) หรือ เท็จ(False) ซึ่งใน ภาษาซี ตัวดําเนินการ ความหมาย ตัวอยาง
== เทากันหรือไม x == 3
จะใชคา 1 หรือ 0
> มากกวาหรือไม x > y
ในภาษาซี คาที่ไมใชศูนยจะถือวามีคาความจริงเปน จริง (True) < นอยกวาหรือไม x < y
ตัวอยาง เงื่อนไข เชน >= มากกวาหรือเทากันหรือไม x >= y

x คาตัวแปรเดี่ยว ถา x เปนศูนย แสดงวา เปนเท็จ <= นอยกวาหรือเทากันหรือไม x <= y


!= x != y
x>y เปรียบเทียบคาของ x กับ y ไมเทากันหรือไม
x>0 && x <10 คาของ x ตองมากกวาศูนย และ นอยกวาสิบ หมายเหตุ เครื่องหมาย = นัน้ เปนการกําหนดคา (assignment statement)
x != 10 คาของ x ตองไมใชสิบ แต == นั้น เปนการเปรียบเทียบการเทากัน (equality test)
Department of Computer Engineering, PSU 5 Department of Computer Engineering, PSU 6

242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

การแปลความหมายของนิพจน ตัวดําเนินการทางตรรกะ (Logical Operators)


กําหนดให x = 5 และ y = 10
• เปนการดําเนินการทางตรรกะ ใหผลลัพธออกมาเปน คาจริง
นิพจน ผลลัพธ การแปลความหมาย
x==y 0 เท็จ
และ คาเท็จ เทานั้น
x>y 0 เท็จ ! นิเสธ (NOT) unary operator
x<y 1 จริง && และ (AND) binary operator
x>=y 0 เท็จ || หรือ (OR) binary operator
x<=y 1 จริง
x!=y 1 จริง เชน (x > 10) && (x < 20)
((x >= 10)||(x < 15)) && !(x==12)

Department of Computer Engineering, PSU 7 Department of Computer Engineering, PSU 8


242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

! นิเสธ (NOT) && และ (AND) , || หรือ (OR)


• ตัวดําเนินการนี้ จะตองการตัวถูกดําเนินการเพียงตัวเดียว • Binary operators ตองการตัวถูกดําเนินการ(operand) สองตัว
(unary operator)
• a && b จะมีคาเปนจริง เพียงกรณีเดียวคือ คาทั้งสองของนิพจน เปนจริง
• ผลลัพธที่ไดมาจะเปนคาตรงกันขามกับ คาทางตรรกะเดิม
• a || b จะมีคาเปนเท็จ เพียงกรณีเดียวคือ คาทั้งสองของนิพจน เปนเท็จ
a b a && b a || b
นิพจน a !a
true false true true true true
true false false true
false true false true false true
false false false false

Department of Computer Engineering, PSU 9 Department of Computer Engineering, PSU 10

242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

ลําดับความสําคัญของตัวดําเนินการ ตัวอยาง
หากในนิพจนมีตัวดําเนินการ(operator)มากกวาหนึ่ง และไมมี กําหนดให int i=5, j=7, k=12; float x=22.5;
วงเล็บ ความสําคัญของ operator เปนไปดังนี้ นิพจน expression Equivalent expression value
operator association priority i+2==k-1 (i+2)==(k-1) 0
! – ++ -- right to left Highest 3*i-j<22 ( (3*i) - j ) < 22 1
* / % left to right i+2*j>k ( i+(2*j) ) > k 1
+ - left to right k+3<=-j+3*i (k+3) <= ( (-j) + (3*i) ) 0
< <= > >= left to right ’a’+1 == ’b’ (’a’+1 ) == ’b’ 1
== != left to right 25>=x+4.0 25 >= (x+4.0)
0
&& left to right i+j<=k==12-k ( (i+j) <= k )== (12-k) 0
|| left to right Lowest i>6 || j-6 && k (i>6) || ( (j-6) && k ) 1
Department of Computer Engineering, PSU 11 Department of Computer Engineering, PSU 12
242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

โครงสรางแบบทางเลือก คําสั่งเงื่อนไข if
โปรแกรมจะเลือกการทํางาน โดยพิจารณาจากเงื่อนไขที่กําหนด • จะเลือกทําคําสั่ง (หรือกลุมคําสั่ง) ก็ตอเมื่อ ตรวจสอบ
เงื่อนไขแลวเปนจริง
•ถาเงื่อนไขเปนเท็จ ก็ไมทําอะไร
คําสั่งเงื่อนไขที่ใชในควบคุมการเลือกไดแก
ตัวอยาง Flowchart ของโครงสรางแบบทางเลือก if
• if, if-else ถา, ถา-ไมเชนนั้นแลว
• switch-case เลือกไปตามกรณี เท็จ
เงื่อนไข
จริง
คําสั่ง

Department of Computer Engineering, PSU 13 Department of Computer Engineering, PSU 14

242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

โครงสรางแบบทางเลือก if คําสั่งเงือ่ นไข if


• ตัวอยางของการใชคําสั่ง if
เงื่อนไขในการตรวจสอบจะเปนชนิด
int x,abs_x;
boolean คือ จริง (true) หรือ เท็จ (false)
abs_x=x;
if (เงื่อนไขที่ตองการตรวจสอบ) if (x < 0)
abs_x = -x;
if เปนคําสงวน
คําสั่ง; printf(“The absolute value x is %d”,abs_x);
โปรแกรมจะตรวจสอบวาคาของ x วานอยกวา 0 หรือไม
• ถาเงื่อนไขเปนจริง คําสั่งนี้จะถูกทํางาน
• หากนอยกวา(จริง) ตัวแปร abs_x จะถูกเปลี่ยนคาเปน -x
• ถาเงื่อนไขเปนเท็จ โปรแกรมจะไมทําคําสั่งนี้
• หากมากกวา(เท็จ) คา abs_x ยังคงเดิม ไมเปลี่ยนแปลง
• หลังจากการทํางานของคําสัง่ if ในสวนของ printf ก็จะถูก เรียกใชงานเปนลําดับตอมา
ขอผิดพลาดที่เกิดขึ้นบอย: หลังวงเล็บเงื่อนไข หามใสเครื่องหมาย ; (ไมวา เงือ่ นไขจะจริงหรือเท็จ)
Department of Computer Engineering, PSU 15 Department of Computer Engineering, PSU 16
242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

ตัวอยาง ตัวอยาง
#include<stdio.h>
void main()
{
#include<stdio.h> int score;
int main() printf(“Enter your score :");
{ int age; scanf("%d",&score);
printf(”Enter your age: ”); if(score>=49)
printf("You pass :-)\n");
scanf(“%d”,&age);
if(age < 0 || age > 150) printf("Good bye!!\n");
printf(“Your age is not valid!\n"); }
printf(”Goodbye\n”);
return 0; ผลลัพธของโปรแกรม 1 ผลลัพธของโปรแกรม 2
} Enter your score : 67
Enter your score : :34
You pass :-)
Good bye!!
Good bye!!
Department of Computer Engineering, PSU 17 Department of Computer Engineering, PSU 18

242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

เงื่อนไขของคําสั่ง if แบบฝกหัด
• เงื่อนไขที่ตองการตรวจสอบสามารถนํามารวมกันได เชน • จงเขี ย นโปรแกรมคํ า นวณการคิ ด เกรด โดยใหรั บ
คะแนนจากผูใช หากคะแนนมีคามากกวาหรือเทากับ 80
#include<stdio.h> #include<stdio.h>
int main () int main () ใหแสดงเกรด ‘A’
{ {
int a =2,b=7;
#include<stdio.h>
if(a>0) int a =2,b=7;
int main ()
{ if((a>0)&& (b>0)) {
if (b>0) printf("OK."); int score;
printf("OK.");
printf("Enter your score") ;
}
scanf("%d",&score);
} } if(score >=80)
printf("You get grade A \n");
}
Department of Computer Engineering, PSU 19 Department of Computer Engineering, PSU 20
242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

คําสั่งเงื่อนไข if-else โครงสรางแบบทางเลือก if-else


• else สามารถถูกเพิ่มในคําสัง่ เงื่อนไขของ if ได โปรแกรมจะ if (เงื่อนไขที่ตองการตรวจสอบ)
ทํางานที่คําสั่ง else เมื่อเงื่อนไขถูกตรวจสอบวาเปนเท็จ คําสั่งที่ 1;
ตัวอยาง Flowchart ของโครงสรางแบบทางเลือก if-else else
คําสั่งที่ 2;
เงื่อนไข
จริง เท็จ • หากเงื่อนไขเปน “จริง” โปรแกรมจะทํางานใน คําสั่งที่ 1
คําสั่งที่ 1 คําสั่งที่ 2 • หากเงื่อนไขเปน “เท็จ” โปรแกรมจะทํางานใน คําสั่งที่ 2

คําสั่งที่ 3
Department of Computer Engineering, PSU 21 Department of Computer Engineering, PSU 22

242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

#include<stdio.h>
int main ()
{ แบบฝกหัด
int a,b;
printf("Enter integer 1: ") ;
scanf("%d",&a);
printf("Enter integer 2: ") ;
จงเขียนโปรแกรมประเมินผลการเรียนในรายวิชาหนึ่ง
scanf("%d",&b);
if(a>b)
โดยใหรับคะแนนจากผูใช หากคะแนนมีคามากกวา
else
printf("%d is greater than %d\n",a,b);
หรือเทากับ 50 ใหแสดงเกรด ‘P’ (pass) แตหากได
}
printf("%d is less than %d\n",a,b);
คะแนนนอยกวา 50 ใหแสดงเกรด ‘F’ (fail)
ผลลัพธของโปรแกรม รัน1 ผลลัพธของโปรแกรม รัน2
Enter integer 1: 77 Enter integer 1: 22
Enter integer 2: 22 Enter integer 2: 77
77 is greater than 22 22 is less than 77
Department of Computer Engineering, PSU 23 Department of Computer Engineering, PSU 24
242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

ตัวอยางโปรแกรม Conditional Operator ? :


#include<stdio.h> • ภาษาซีมีตัวดําเนินการเงื่อนไข (Conditonal operator) ทีใ่ ชเลือกคา
int main () ขอมูลคาใดคาหนึ่งจากตัวเลือกสองคา โดยพิจารณาจากเงื่อนไข
{
int score;
• รูปแบบ: X ? Y : Z
printf("Enter your score ") ;
ถา X เปนจริง นิพจนนี้จะมีคาเทากับ Y
scanf("%d",&score); แตถา X เปนเท็จ นิพจนนี้จะมีคาเทากับ Z
if (score >=50) • ตัวอยางการใชงาน
printf("You get grade P \n"); a = b>0?5:10; a จะมีคาเปน 5 ถา b มากกวา 0 แต a
else
printf("You get grade F \n");
จะมีคาเปน 10 ถา b นอยกวาหรือเทากับ 0
printf(“abs x = %.2f\n”, x>=0.0?x:-x);
return 0; ประโยคนีจ้ ะพิมพคาสัมบูรณของ x
}
Department of Computer Engineering, PSU 25 Department of Computer Engineering, PSU 26

242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

Conditional Operator vs if-else


• เราสามารถแปลงนิพจนที่มี conditional operator โดยใชคําสั่ง
แบบฝกหัด
if-else แทนได
• การใช conditional operator จะไดนิพจนที่สั้น กะทัดรัดกวา
จงเขียนโปรแกรมประเมินผลการเรียนในรายวิชาหนึ่ง
• ตัวอยาง โดยใหรับคะแนนจากผูใช หากคะแนนมีคามากกวา
a = b>0?5:10; ใช if-else แทนจะได หรือเทากับ 50 ใหแสดงเกรด ‘P’ (pass) แตหากได
if(b>0) a = 5; else a=10;
คะแนนนอยกวา 50 ใหแสดงเกรด ‘F’ (fail)
printf(“abs x = %.2f\n”,x>=0.0?x:-x);
ใช if-else แทนจะได กําหนดใหใช Conditional operator
if(x>=0.0) printf(abs x = %.2f\n”,x);
else printf(abs x = %.2f\n”,-x);;

Department of Computer Engineering, PSU 27 Department of Computer Engineering, PSU 28


242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

ตัวอยางโปรแกรม conditonal operator


#include<stdio.h>
กลุมคําสั่ง (Block Statements)
int main ()
{ int score; • คําสั่งที่อยูภายใตคําสั่ง if-else สามารถมีไดมากกวา 1 คําสั่ง
printf("Enter your score ") ; เรียกวา กลุมคําสั่ง หรือ Block Statements
scanf("%d",&score);
• กลุมคําสั่งจะอยูภายในเครือ่ งหมาย {……}
printf("You get grade %c \n“,
score>=50 ? ’P’ : ’F’);

return 0;
}
Department of Computer Engineering, PSU 29 Department of Computer Engineering, PSU 30

242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming
#include<stdio.h>
int main ()
{
int points= 44;
คําสั่งเงื่อนไข if-else-if-else (Nested if)
if (points>=50) ตัวอยาง Flowchart ของโครงสรางแบบทางเลือก nested if
{
printf("Pass exam......\n"); (if ที่ซอนกันเปนชั้นๆ)
printf("Congratulations!\n"); กลุมคําสั่ง
}
else เท็จ
{ เท็จ เท็จ
เงื่อนไขที่ 1 เงื่อนไขที่ 2 เงื่อนไขที่ 3
printf("Fail......\n");
printf("Attempt again\n"); กลุมคําสั่ง
} จริง จริง จริง
printf("Bye bye....See you again next semester\n");
} คําสั่งที่ 1 คําสั่งที่ 2 คําสั่งที่ 3 คําสั่งที่ 4
Fail......
Attempt again
Bye bye....See you again next semester
คําสั่งที่ 5
Department of Computer Engineering, PSU 31 Department of Computer Engineering, PSU 32
242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

โครงสรางของ if-else-if-else #include<stdio.h>


int main ()
{ int a,b;
if (เงื่อนไขที่ 1) printf("Enter integer 1: ") ;
scanf("%d",&a);
คําสั่งที่ 1; หรือ กลุมคําสั่งที่ 1 printf("Enter integer 2: ") ;
scanf("%d",&b);
else if (เงื่อนไขที่ 2) if(a>b)
คําสั่งที่ 2; printf("%d is greater than %d\n",a,b);
else if (a==b)
else if (เงื่อนไขที่ 3) printf("%d is equal to %d\n",a,b);
else
คําสั่งที่ 3; printf("%d is less than %d\n",a,b);
else คําสั่งที่ 4; }

คําถาม: 1. คําสั่งที่2 จะกระทํา เมื่อเงื่อนไข1,2,3 เปนอยางไร? ผลลัพธของโปรแกรม รันครั้งที1่ ผลลัพธของโปรแกรม รันครั้งที2่


2. คําสั่งที่4 จะกระทํา เมื่อเงื่อนไข1,2,3 เปนอยางไร? Enter integer 1: 22 Enter integer 1: 22

3. ถาเงือ่ นไข 1 เท็จ และ เงื่อนไข 2 และ 3 เปนจริง Enter integer 2: 22 Enter integer 2: 77

โปรแกรมจะทําคําสั่งใดบาง? 4.หากเงื่อนไขเปนจริงทั้งหมด? 22 is equal to 22 22 is less than 77


Department of Computer Engineering, PSU 33 Department of Computer Engineering, PSU 34

242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

แบบฝกหัด #include<stdio.h>
int main()
{
float points;
ใหนศ.เขียนโปรแกรมคํานวณการคิดเกรด โดยใหรับคะแนนจากผูใช หากคะแนนมีคา printf("Please Enter Your Score ");
scanf("%f",&points);
-มากกวาหรือเทากับ 80 ใหแสดงเกรด ‘A’ if (points>=80.0){
printf("Congratulations!\n");
printf("You get grade A\n");
-มากกวาหรือเทากับ 70 ใหแสดงเกรด ‘B’ }
else if (points>=70.0)
-มากกวาหรือเทากับ 60 ใหแสดงเกรด ‘C’ printf("You get grade B\n");
else if (points>=60.0)
-มากกวาหรือเทากับ 50 ใหแสดงเกรด ‘D’ printf("You get grade C\n");
else if (points>=50.0)
-นอยกวา 50 ใหแสดงขอความวา fail…………… printf("You get grade D\n");
else
-และไมวาจะไดเกรดอะไรก็ตาม ใหแสดงขอความ Bye bye....See you again next printf("Fail......\n");
printf("Bye bye....See you again next semester\n");
semester ทุกกรณี }

Department of Computer Engineering, PSU 35 Department of Computer Engineering, PSU 36


242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

คําสั่งทางเลือก switch-case โครงสรางแบบทางเลือก switch-case


• ใชควบคุมใหโปรแกรมเลือกดําเนินการไปในเสนทางใดเสนทางหนึ่ง(case) จาก
ทางเลือกหลายๆทาง โดยใชคาที่ตองการตรวจสอบวาตรงกับคาใด
switch ( นิพจนทีต่ องการตรวจสอบ )
• นิพจนที่อยูในคําสั่ง switch จะถูกตรวจสอบวาตรงกับ case ใด แลวโปรแกรมก็จะ
ทํางานตามคําสั่งที่อยูใน case นั้น และคําสั่งอื่นๆที่ตามมาจนจบโครงสราง {
switch-case หรือเจอคําสั่ง break ก็จะออกจากโครงสราง switch-case case คาที่ 1 : คําสั่งที่ 1;
• โดยคาที่ใชตรวจสอบ จะตองเปนจํานวนเต็มหรือตัวอักษรเทานั้น (int, char) case คาที่ 2 : คําสั่งที่ 2; switch และ case
case คาที่ 3 : คําสั่งที่ 3; คือคําสงวน
case … …
คาสําหรับ
}
ตรวจสอบ
•นิพจนที่อยูในคําสั่ง switch จะถูกตรวจสอบตามลําดับวาตรงกับ case ใด โปรแกรมก็จะ
คาที่ 1 คาที่ 2 คาที่ 3 ทํางานตามคําสั่งที่อยูใน case นั้น รวมถึงคําสั่งอื่นๆที่ตามมา ใน case ที่เหลือจนจบ
คําสั่งที่ 1 คําสั่งที่ 2 คําสั่งที่ 3 โครงสราง switch-case หรือเจอคําสัง่ break ก็จะออกจากโครงสราง switch-case ได
Department of Computer Engineering, PSU 37 Department of Computer Engineering, PSU 38

242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming
#include<stdio.h>
int main()
{
คําสั่ง break
int c; • คําสั่ง break จะใชสําหรับการควบคุมการกระทํา โดยบังคับการกระทํา
printf("Enter integer 1 or 2 or 3:");
scanf("%d",&c); • บอยครัง้ ที่จะใชคําสั่ง break เปนคําสั่งสุดทายในแตละ case
switch(c) • หากไมมีคําสั่ง break ในชุดคําสั่งของ case ใด โปรแกรมจะทํางานตอไป ใน
{
คําสั่งของทุกๆ case ถัดไปดวยจนจบ
case 1: printf("ONE\n");
switch (นิพจนที่ตองการตรวจสอบ)
case 2: printf("TWO\n");
{ case คาที่ 1 : คําสั่งที่ 1; break;
case 3: printf("THREE\n"); case คาที่ 2 : คําสั่งที่ 2; break;
} case คาที่ 3 : คําสั่งที่ 3; break;
} case … … break;
ใชคําสั่ง break; มาชวย }

Department of Computer Engineering, PSU 39 Department of Computer Engineering, PSU 40


242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming
#include<stdio.h>
int main()
{
int c;
คําสั่งเงือ่ นไข switch-case
printf("Enter integer 1 or 2 or 3:");
scanf("%d",&c); • default เปนคําสงวน
switch(c)
{ • default เปนอีกกรณีหนึ่งในคําสั่ง switch-case
case 1: printf("ONE\n");
break;
มักวางไวเปนกรณีสดุ ทาย
case 2: printf("TWO\n");
break;
• ในกรณีทตี่ รวจสอบแลวพบวา นิพจน มีคาไมตรงกับ case
case 3: printf("THREE\n"); ใดๆเลยขางตน โปรแกรมจะเขาไปทํางานในสวนของ
break;
} default
}
• ไมจําเปนตองใส break หลังชุดคําสั่งของ default
Department of Computer Engineering, PSU 41 Department of Computer Engineering, PSU 42

242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming
#include<stdio.h> #include<stdio.h>
int main() int main()
{ { char grade;
int c; printf("Enter your grade: ");
printf("Enter integer 1 or 2 or 3 "); scanf("%c",&grade);
scanf("%d",&c); switch(grade)
switch(c) { case ’a’:
{ case ’A’: printf(“Very Good\n");
case 1: printf("ONE\n"); break;
break; case ’b’:
case 2: printf("TWO\n"); case ’B’: printf(“Good\n");
break; break;
case 3: printf("THREE\n"); case ’c’:
break; case ’C’: printf(“Fair\n");
default: printf("Out of range"); break;
} default: printf(“No good!\n");
} }
}
Department of Computer Engineering, PSU 43 Department of Computer Engineering, PSU 44
242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

if-else vs switch-case จบ if-else และ switch-case


• switch-case ใชในกรณีที่มีทางเลือกหลายทาง โดยขึ้นอยูกับคาของตัวแปร
(หรือนิพจน)หนึ่ง ที่มคี าเปน int หรือ char เทานั้น • จงเขียนโปรแกรม รับตัวอักษรหนึ่งตัว แลวตรวจสอบวาเปน สระ(vowel) หรือ
• if-else ใชตรวจสอบเงื่อนไข ไดหลากหลายกวา ตามตองการ (เชน พยัญชนะ (consonant) สมมติวาผูใชใสเฉพาะตัวอักษร a-z เทานั้น
เปรียบเทียบคานอยกวา มากกวา หรืออยูในชวงใดชวงหนึง่ รวมถึงการใช • ใหเติมสวนของโปรแกรมนี้ใหสมบูรณ
logical operator สรางเงื่อนไขไดซับซอนขึ้น)แตถามีหลายทางเลือก ตองใช
if ซอนกันหลายๆชั้น หรือสรางเงื่อนไขที่ซับซอนขึ้น char ch;
• บางกรณีสามารถแปลงคําสั่ง switch-case เปน if-else ได เชน printf(”Enter a character(a-z): ”);
scanf(”%c”,&ch);
if(a==b)
switch(a)
{ case b: คําสั่ง_1; break; คําสั่ง_1;
else if (a==c) เติมเต็มโปรแกรมในสวนนี้ จะเลือกใชโครงสราง if-else หรือ switch-case ก็ได
case c: คําสั่ง_2; break;
case d: คําสั่ง_3; break; คําสั่ง_2; คําแนะนํา ใหตรวจสอบคา ch วาเปนตัวอักษร สระ a e i o u หรือไม ถาใชก็พิมพ
default: คําสั่ง_4; else if (a==d) vowel ถาไมใช ก็ใหพิมพวาวาเปน Consonant
} คําสั่ง_3;
else คําสั่ง_4; printf(”End of the program.\n”);
Department of Computer Engineering, PSU 45 Department of Computer Engineering, PSU 46

242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

เฉลย โครงสรางแบบทําซ้ํา
char ch;
printf(”Enter a character(a-z): ”);
scanf(”%c”,&ch);
• ทําไมตองทําซ้ํา?
if (ch==’a’||ch==’e’||ch==’i’||ch==’o’||ch==’u’)
printf(”Vowel\n”);
• งานบางอยาง อาศัยการทํางานซ้ําๆกัน อาจเหมือนกันทุกครั้ง
else
printf(”Consonant\n”);
หรือในการทําแตละครั้ง มีคาบางอยางเปลี่ยนไป อยางมีรูปแบบ
printf(”End of the program.\n”);

switch (ch)
• ตัวอยาง ตองการแสดงขอความวา Hello จํานวน 200 ขอความ
{ case ’a’: printf(”Vowel\n”); break;
case ’e’: printf(”Vowel\n”); break; printf(“Hello ");
case ’i’: printf(”Vowel\n”); break; printf(“Hello ");
case ’o’:
case ’u’:
printf(”Vowel\n”); break;
printf(”Vowel\n”); break;
printf(“Hello
printf(“Hello
");
"); 200 ครั้ง
default : printf(”Consonant\n”); printf(“Hello ");
} ……….
printf(”End of the program.\n”);

Department of Computer Engineering, PSU 47 Department of Computer Engineering, PSU 48


242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

โครงสรางแบบทําซ้ํา โครงสรางแบบทําซ้ํา
• ตัวอยาง ตองการแสดง ตารางการคูณ เชน
i=1; printf(“2 x %d = %d \n“,i,2*i);
• เปนการสั่งใหโปรแกรมทํางานอยางใดอยางหนึ่งซ้ําๆกัน
i++; printf(“2
i++; printf(“2
x
x
%d
%d
=
=
%d
%d
\n“,i,2*i);
\n“,i,2*i); 12 ครั้ง ตามจํานวนรอบที่ตองการ หรือ ตามเงื่อนไขที่กําหนด
i++; printf(“2 x %d = %d \n“,i,2*i);
……….
• การวนรอบบางครั้งจะเรียกวา loop หรือการวนลูป
ผลการรันโปรแกรม
2 x 1 = 2 • กลุมคําสั่งการทําซ้ํา มีสามประเภท ไดแก for loop,
2 x 2 = 4
2 x 3 = 6 while loop และ do-while loop
2 x 4 = 8
……….
2 x 11 = 22
2 x 12 = 24

Department of Computer Engineering, PSU 49 Department of Computer Engineering, PSU 50

242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

การทําซ้ํา (ขําขัน)
คําสั่งการทําซ้ํา for
for (การกําหนดคาเริ่มตนตัวแปร ; เงื่อนไข ; ปรับคาตัวแปร)
คําสั่ง; หรือ กลุมคําสั่ง
จากตัวอยางกอนหนา สามารถเขียนโปรแกรมใหอยูในรูปของ for loop ไดดังนี้
พิมพ Hello สองรอยครั้ง เด็ก: เสร็จแลวคะครู
int i;
ครู : ... พยายามดีเหลือเกิน (กะจะลองดีกับฉันใชไหม!?!)
for (i=0; i<200; i=i+1) printf(“Hello");
เด็กฉลาด (แกมโกง)
พิมพตารางการคูณ แมสอง ครูทาํ โทษใหเด็กคัดบนกระดาน
int i;
“ฉันจะไมเลนปาเครื่องบินกระดาษในหองเรียนอีกแลว” 500 จบ
for (i=1; i<=12; i++ )
แตเด็ก(หัวใส) เขียนเปนโคดโปรแกรมภาษาซีทจ ี่ ะทําใหไดผลลัพธ
{ printf(”2 x %d = %d \n”,i, 2*i); }
เหมือนกัน

Department of Computer Engineering, PSU 51 Department of Computer Engineering, PSU 52


242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

Flow chart การทํางาน คําสั่งการทําซ้ํา for


คําสั่งการทําซ้ํา for
หากเงื่อนไขเปนจริง loop ก็จะยังคงทํางานตอไป จนกระทั่ง
การกําหนดคาเริ่มตนตัวแปร
เงื่อนไขเปนเท็จ ตัวอยางเชน
การปรับคาตัวแปร (ลด/เพิ่ม) อาจจะสงผลใหมีเกิดทํางานไมรู
ตรวจสอบเงื่อนไข
จบของ loop ได (infinite loop)
จริง เท็จ
#include<stdio.h>
ผลลัพธ
int main()
*
คําสั่ง
{
*
int i;
for (i=0;i<5;i++)
* ปรับคาตัวแปร
*
printf("* \n"); *
}
Department of Computer Engineering, PSU 53 Department of Computer Engineering, PSU 54

242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

การใชงานคําสั่งfor คําสั่งการทําซ้ํา while


มักใช ในกรณี รูจํานวนรอบการทําซ้ํา ที่แนนอน หรือ มีตวั นับจํานวนรอบ
(counter) กํากับ • รูปแบบของ while loop คือ
ตัว counter ตองถูกกําหนดคาเริ่มตน, และมีการปรับเปลี่ยนคา
ควรระวังการกําหนดเงื่อนไขการทําซ้ํา ที่จะตองกลายเปนเท็จได เพื่อไมทําให
เกิดลูปอนันต (infinite loop) while (เงื่อนไข)
การกําหนด จํานวนรอบ อยางงาย
while เปนคําสงวน คําสั่ง;
ถาตองการทํา n รอบ
ในขณะที่ เงื่อนไข เปนจริง
int i,n; int i,n; หากเงื่อนไขเปนจริง โปรแกรมจะ
for (i=0; i<n; i++) for (i=1; i<=n; i++) ใหทําคําสั่ง
{ printf(”%d \n”,i); } { printf(”%d \n”,i); } ทํางานตามคําสั่งซ้ํา จนกระทั่ง
เงื่อนไขเปนเท็จ หรือ ทําคําสั่งนั้นจนกวา
เงื่อนไขจะกลายเปนเท็จ
Department of Computer Engineering, PSU 55 Department of Computer Engineering, PSU 56
242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

Flowchart คําสั่ง while คําสั่งการทําซ้ํา while


for loop while loop
ตรวจสอบเงื่อนไข #include<stdio.h> #include<stdio.h>
การทําซ้ํา int main() int main()
{ { int i=0;
จริง เท็จ int i; while (i<5)
for (i=0;i<5;i++) {
คําสั่ง
printf("* \n"); printf("* \n");
} i++;
}
}
Department of Computer Engineering, PSU 57 Department of Computer Engineering, PSU 58

242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

เปรียบเทียบโครงสรางของ for loop กับ while loop คําสั่งการทําซ้ํา do-while


for loop • โครงสรางแบบการทําซ้ํา while และ for จะตองมีการ
for (การกําหนดคาเริ่มตนตัวแปร ; เงื่อนไข ; ปรับคาตัวแปร)
ตรวจสอบคาของเงื่อนไขกอนวาเปนจริงหรือเท็จ กอนที่จะ
คําสั่ง;
ทําคําสั่ง (กลุมคําสั่ง) ภายในรอบ
while loop • ถาตองทําคําสั่งภายในรอบกอนอยางนอย 1 ครั้ง แลวจึง
การกําหนดคาเริ่มตนตัวแปร;
ตรวจสอบเงื่อนไข เมื่อจบรอบ ใหใชโครงสราง do-while
while (เงื่อนไข){
คําสั่ง;
ปรับคาตัวแปร;
}
Department of Computer Engineering, PSU 59 Department of Computer Engineering, PSU 60
242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

โครงสราง do while loop ตัวอยาง do while loop


ลูปนี้ทํากี่รอบ? รอบเดียว
#include<stdio.h> ถาตองการใหทํา 6 รอบ ตองแกไข อยางไร
do int main() #include<stdio.h>
{ {
int i=6;
int main()
{
กลุมคําสั่ง; do int i=6;
{
} printf("* \n");
do
{
i++;
while ( เงื่อนไข ) ; }
printf("* \n");
i++;
while (i<5); }
}
มี ; เพื่อจบคําสัง่ while (i<12);
do-while }
Department of Computer Engineering, PSU 61 Department of Computer Engineering, PSU 62

242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

เปรียบเทียบโครงสราง do while กับ while loop จงเขียนโปรแกรมคํานวณ คาเฉลี่ยคะแนนของ นร. 10 คน


while loop Enter score of student 1: 98
do loop int n; float score,avg,sum;
Enter score of student 2 : 76
n=1; sum=0;
Enter score of student 3 : 71 do {
printf(”Score of st %d”,n);
Enter score of student 4: 65
ตรวจสอบเงื่อนไข คําสั่ง scanf(”%f”,&score);
Enter score of student 5 : 89 sum = sum + score;
การทําซ้ํา n++;
จริง Enter score of student 6 : 75
} while (n<=10);
จริง เท็จ Enter score of student 7: 78
ตรวจสอบเงื่อนไข avg = sum/10.0;
การทําซ้ํา Enter score of student 8 : 76 printf(”Class average is %.2f”,
คําสั่ง Enter score of student 9 : 71 avg);

เท็จ Enter score of student 10: 80


Class average is 77.90
Department of Computer Engineering, PSU 63 Department of Computer Engineering, PSU 64
242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

จงเขียนโปรแกรมเพือ่ แสดงผลตามดานลาง จงหาผลลัพธของโปรแกรมดานลาง


Enter Result (1=pass, 2=fail): 1 int i,res, npass,nfail;
Enter Result (1=pass, 2=fail): 1 npass=0; nfail=0;
Enter Result (1=pass, 2=fail): 1 for (i=0;i<10;i++) #include<stdio.h>
*
Enter Result (1=pass, 2=fail): 2
{ printf(”Enter result(1=pass, #define num 5
2=fail)\n”); **
Enter Result (1=pass, 2=fail): 1 scanf(”%d”,&res);
int main(){
Enter Result (1=pass, 2=fail): 1 if (res == 1) npass++; int i,j; ***

Enter Result (1=pass, 2=fail): 1 else nfail++; for (i=0;i<num;i++){ ****

Enter Result (1=pass, 2=fail): 1 } for(j=0; j<=i; j++){ *****


Enter Result (1=pass, 2=fail): 1 printf(”Passed = %d \n”,npass); printf("*");
Enter Result (1=pass, 2=fail): 1 printf(”Failed = %d \n”,nfail); }
Passed = 9 printf("\n");
Failed = 1 }
return 0;
}
Department of Computer Engineering, PSU 65 Department of Computer Engineering, PSU 66

242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

โปรแกรมรอรับขอมูลจนกวาจะไดคาในขอบเขต
จงหาผลลัพธของโปรแกรมดานลาง
ที่กําหนด Enter a grade: A
A OK
#include<stdio.h> #include<stdio.h>
----
#define num 5 ***** int main(){
Enter a grade: e
char grade;
int main(){ **** printf(”Enter a grade:”);
Grade must be A-E
Enter a grade again: E
int i,j; *** scanf(”%c”,&grade); E OK
for (i=num;i>0;i--){ ** while( grade<’A’|| grade>’E’) ----
for(j=0;j<i;j++){ { printf(”Grade must be A-E\n”);
Enter a grade: Z
* printf(”Enter a grade again:”);
printf("*"); Grade must be A-E
scanf(”%c”,&grade); Enter a grade again: 4
} } Grade must be A-E
printf("\n"); printf(”%c OK\n”,grade); Enter a grade again: b
} return 0; Grade must be A-E
return 0; } Enter a grade again: B
B OK
} ----

Department of Computer Engineering, PSU 67 Department of Computer Engineering, PSU 68


break และ continue
242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

โปรแกรมรอรับขอมูลจนกวาจะไดคาในขอบเขต
ใช do-while loop • continue และ break เปนคําสั่งที่ใชในโครงสรางลูป สามารถใชไดทั้งใน
#include<stdio.h> Enter a grade: A ลูป for, while และ do-while
A is OK
int main(){
char grade ; int ok;
----
Enter a grade: e
• คําสั่ง break (นอกจากใชในโครงสราง switch-case แลว) ใชเพื่อเปน
do{ Grade must be A-E
ok=1; Please enter again.
ทางลัดในออกจากลูปไดทันทีเมื่อตองการ โดยไมตองรอจนเงื่อนไขเปน
printf(”Enter a grade:”); Enter a grade: E
scanf(”%c”,&grade); E is OK
เท็จ
if(grade<’A’|| grade>’E’) ----
ok=0; Enter a grade: Z
• คําสั่ง continue ใชเมื่อตองการจบการทํางานรอบปจจุบัน แลวไปเริ่มตน
if (!ok) Grade must be A-E
{ printf(”Grade must be A-E\n”); Please enter again.
ทํางานในรอบใหม(ไปยังสวนการตรวจสอบเงื่อนไข) โดยจะไมทําทุก
printf(“Please enter again.”); Enter a grade: b
} Grade must be A-E
คําสั่งที่เหลือจนครบรอบตามปกติ
} while(!ok); Please enter again.
printf(”%c is OK\n”,grade); Enter a grade again: B • continue ในลูป for จะไปยังสวนของการปรับคาตัวแปรกอน แลวจึง
B is OK
return 0;
} Department of Computer Engineering, PSU
---- ตรวจสอบเงื่อนไขตอไป
69 Department of Computer Engineering, PSU 70

242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

ตัวอยางคําสั่ง break ตัวอยางคําสั่ง continue


do { ...
if (n>10) break; // ใหออกจากลูปไดทันทีที่ n>10 ...
... while (x>0)
} while ( ); //infinite loop เพราะไมมีเงื่อนไข { ...
if (y>100) continue;
... // ถา y>100 ใหไปเริ่มตนลูปใหมทันทีไมตองทําคําสัง่ ที่เหลือจนครบรอบ
while (x>0) printf(”y = %d\n”, y);
{ ... ...
if (x>100) break; // ใหออกจากลูปไดทันทีที่ x>100 }
... ...
} for(n=0; n<100; n++)
... { ...
for(n=0; n<100; n++) if (n%10==0) continue;
{ ... // ถา n หารสิบลงตัวใหไปเริ่มตนลูปรอบใหมทันที (รอบถัดไปที่ n++)
if (n > x) break; // หาก n>x ใหออกจากลูปไดทันที ไมตองทําจนครบ 100 รอบ ...
... }
}

Department of Computer Engineering, PSU 71 Department of Computer Engineering, PSU 72


242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

ตัวอยางโปรแกรม ลูปซอนกัน Nested loop


...
int i;
- หากคําสั่ง break และ continue ปรากฏอยูในโครงสรางของลูป
for (i=1; i<=99; i++) ที่ซอนกันอยูหลายชั้น คําสั่งนั้นมีผลกับลูปในสุดที่ครอมมันอยูเทานั้น
{
printf(”%02d”, i); (ไมสงผลตอลูปภายนอก)
if(i%3==0) { printf(”#\n”); continue; } ...
printf(“xxx”); do {
... break;
if(i>10) break;
while (x>0)
} { ... break;
จากสวนของโปรแกรม ไดผลลัพธดังนี้ if (x>100) continue;
...
01xxx02xxx03# }
04xxx05xxx06# for(j=0; j<x; j++) หมายเหตุ คําสั่ง break ที่ใชใน
{ ...
07xxx08xxx09# if(x==50) break; โครงสราง switch-case มี
10xxx11xxx } ขอบเขตในโครงสรางนั้นเทานั้น
...
} while (n>0); ไมสงผลตอลูปภายนอกใดๆ
Department of Computer Engineering, PSU 73 Department of Computer Engineering, PSU 74

242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

โจทยแบบฝกหัด 1. โปรแกรมรับจํานวนเต็มแลวหาผลรวมและผลคูณ
1. จงเขียนโปรแกรมรับเลขจํานวนเต็ม 5 จํานวน แลวหาคาผลรวม และผลคูณ
ของทุกจํานวน #include<stdio.h>
#define NUM 5
(ควรใช loop ชนิดใด? for, while, do-while? ) int main(){
2. จงเขียนโปรแกรมรับจํานวนเต็มบวก เพื่อหาคาที่นอยที่สุด และคาที่มากสุด ที่ int i,n, sum, product;
sum=0; product=1;
ผูใชใสเขามา โปรแกรมจะจบการรับขอมูลเมื่อผูใชใสจํานวนลบ จากนัน้ จึง for(i=0;i<NUM;i++)
แสดงคานอยทีส่ ุด และคามากที่สุด { printf(”Enter a number: ”);
scanf(”%d”, &n);
3. จงเขียนโปรแกรมรับจํานวนเต็มบวก n ( n มีคามากกวาหรือเทากับ 2) แลวหาผลรวมของ sum += n; product *= n;
ทุกจํานวน(ตั้งแต 1 ถึง n-1) ที่หาร n ไดลงตัวและแสดงผลรวมนั้น จากนั้นจึงวนรับคา n }
คาใหมจนกวาผูใชจะใสคา n ที่นอยกวา 2 จึงหยุด แตหากวา n เปนจํานวนเฉพาะ (Prime printf(”Sum = %d\n”, sum);
printf(”Product = %d\n”,product);
number) ใหแสดงวา n เปน Prime number โดยไมตองแสดงผลรวมและใหจบการทํางาน return 0;
ของโปรแกรมในทันที }
(หมายเหตุ หากผลรวมของตัวหารของ n มีคาเปน 1 แสดงวามีเฉพาะ 1 ที่หาร n ไดลงตัว ดังนั้น n จึงเปน
จํานวนเฉพาะ)

Department of Computer Engineering, PSU 75 Department of Computer Engineering, PSU 76


242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming

2. โปรแกรมหาคา min, max 3. ตัวอยางผลการรันโปรแกรมหาคา sum of divisors


Enter n : 25 Enter n : 18
#include<stdio.h> 1 5 1 2 3 6 9
int main() Sum of divisors of n = 6 Sum of divisors of n = 21
{ int n,min,max; Enter n : 28 Enter n : 33
printf(”Enter a number: ”); 1 2 4 7 14 1 3 11
scanf(”%d”, &n); Sum of divisors of n = 28 Sum of divisors of n = 15
min=n; max=n; Enter n = 0 Enter n = -1
while(n>=0) End of program. End of program.
{ if(n<min) min=n; Enter n : 40 Enter n : 97
if(n>max) max=n; 1 2 4 5 8 10 20 1
printf(”Enter a number: ”); Sum of divisors of n = 50 97 is prime number
scanf(”%d”, &n); Enter n : 17 End of program.
} 1
printf(”Min = %d\n”, min); 17 is prime number
printf(”Max = %d\n”, max); End of program.
return 0;
}

Department of Computer Engineering, PSU 77 Department of Computer Engineering, PSU 78

242-101, 241-101 Introduction to Computer Programming

#include<stdio.h>
3. โปรแกรมหาคา sum of divisors
int main()
{ int n, sum, d;
do
{ printf("Enter n : "); scanf("%d",&n);
sum = 0;
for(d=1; d<n; d++)
if (n%d == 0)
{ sum += d;
printf("%d ",d); //ใสบรรทัดนี้หากตองการแสดงตัวหาร(divisor) ออกมาดวย
}
if (sum == 1)
{ printf("\n %d is prime number\n", n);
break; // ใหออกจากลูป do-while ไดทันที หากพบวา n เปนจํานวนเฉพาะ
}
if(sum>0)
printf("\n Sum of divisors of n = %d\n", sum);
} while( n > 2 );
printf("End of program.");
return 0;
} //ถาเปลีย่ นจากคําสัง่ break; เปน continue; จะเปนอยางไร?
Department of Computer Engineering, PSU 79

You might also like