Professional Documents
Culture Documents
Chapter3 Control 154 p4 1
Chapter3 Control 154 p4 1
Chapter3 Control 154 p4 1
บทที่ 3 วัตถุประสงค
• เขาใจการทํางานของคําสั่งแบบทางเลือก ไดแก
โครงสรางควบคุม − if, if–else ,switch-case
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)
242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming
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 เลือกไปตามกรณี เท็จ
เงื่อนไข
จริง
คําสั่ง
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
คําสั่งที่ 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
242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming
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
3. ถาเงือ่ นไข 1 เท็จ และ เงื่อนไข 2 และ 3 เปนจริง Enter integer 2: 22 Enter integer 2: 77
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 ทุกกรณี }
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; มาชวย }
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
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”);
โครงสรางแบบทําซ้ํา โครงสรางแบบทําซ้ํา
• ตัวอยาง ตองการแสดง ตารางการคูณ เชน
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
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); }
เหมือนกัน
242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming
242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming
242-101, 241-101 Introduction to Computer Programming 242-101, 241-101 Introduction to Computer Programming
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
} ----
โปรแกรมรอรับขอมูลจนกวาจะไดคาในขอบเขต
ใช 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
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 จึงเปน
จํานวนเฉพาะ)
#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