241-101 Introduction to Computer Programming

Download Report

Transcript 241-101 Introduction to Computer Programming

241-101 Introduction to Computer Programming
บทที่ 3
โครงสร้างควบคุม
Control Structures
คณาจารย์ภาควิชาวิศวกรรมคอมพิวเตอร์
[email protected]
241-101 Introduction to Computer Programming
วัตถุประสงค์
• เข้าใจการทางานของคาสังแบบทางเลื
่
อก ได้แก่
− if, if–else ,switch-case
• เข้าใจการทางานของคาสังแบบท
่
าซ้ า ได้แก่
− for , while และ do-while
• สามารถนาคาสังต่
่ างๆไปประยุกต์ใช้เพือ่ ควบคุมการทางานของ
โปรแกรมตามทีต่ อ้ งการได้
2
241-101 Introduction to Computer Programming
เนื้ อหาในบทเรียน
• เครือ่ งหมายทีใ่ ช้ในการเปรียบเทียบ
• โครงสร้างการทางานของคาสัง่ if else และ switch case
• ทาไมต้องทาซ้ า??
• โครงสร้างการทางานของคาสัง่ for, do และ do while
• การเปลีย่ นคาสังจาก
่
for เป็ น do และ จากคาสัง่ do เป็ น for
3
241-101 Introduction to Computer Programming
โครงสร้างควบคุม

โดยปกติโปรแกรมจะทางานต่อเนือ่ งตามลาดับคาสัง่
เรียกว่า Sequence หรือการทางานแบบ Sequential
 โครงสร้างควบคุม เป็ นการกาหนดการทางานของโปรแกรม
ให้เป็ นไปในทิศทางทีต่ อ้ งการ หรือเรียกว่า flow of control
 คาสังที
่ ใ่ ช้ในการควบคุมแบ่งได้เป็ น 2 ประเภทคือ
 โครงสร้างแบบทางเลือก (Selection)
 โครงสร้างแบบทาซ้ า (Repetition)
4
241-101 Introduction to Computer Programming

เงื่อนไข Condition
ทั้งโครงสร้างแบบทางเลือก และการทาซ้ า มีเงือ่ นไข เป็ น
ตัวกาหนด การดาเนินไปของโปรแกรม
 เงือ
่ นไข หรือ condition เป็ นนิพจน์ ทีม่ ีค่าทางตรรกะ(logic) หรือ
เรียกว่าค่า boolean คือ ค่า จริง(True) หรือ เท็จ(False) ซึ่งใน
ภาษาซี จะใช้ค่า 1 หรือ 0
 ในภาษาซี ค่าที่ไม่ใช่ศูนย์จะถือว่ามีค่าความจริงเป็ น จริง (True)
 ตัวอย่าง เงือ่ นไข เช่น
x ค่าตัวแปรเดีย่ ว ถ้า x เป็ นศูนย์ แสดงว่า เป็ นเท็จ
x>y เปรียบเทียบค่าของ x กับ y
x>0 && x <10 ค่าของ x ต้องมากกว่าศูนย์ และ น้อยกว่าสิบ
x != 10 ค่าของ x ต้องไม่ใช่สิบ
5
241-101 Introduction to Computer Programming
เครื่องหมายเปรียบเทียบ
•เครื่องหมายที่ใช้ ในการเปรียบเทียบ (relational operators)และให้ ผลลัพธ์เป็ น
จริง หรือ เท็จ ได้ แก่
ตัวดาเนินการ
ความหมาย
ตัวอย่าง
==
เท่ากันหรือไม่
x == 3
>
มากกว่าหรือไม่
x > y
<
น้อยกว่าหรือไม่
x < y
>=
มากกว่าหรือเท่ากันหรือไม่
x >= y
<=
น้อยกว่าหรือเท่ากันหรือไม่
x <= y
!=
ไม่เท่ากันหรือไม่
x != y
หมายเหตุ เครื่องหมาย = นั้นเป็ นการกาหนดค่ า (assignment statement)
แต่ == นั้น เป็ นการเปรียบเทียบการเท่ ากัน (equality test)
6
241-101 Introduction to Computer Programming
การแปลความหมายของนิพจน์
กาหนดให้ x = 5 และ y = 10
นิพจน์
ผลลัพธ์
การแปลความหมาย
x==y
0
เท็จ
x>y
0
เท็จ
x<y
1
จริง
x>=y
0
เท็จ
x<=y
1
จริง
x!=y
1
จริง
7
241-101 Introduction to Computer Programming
ตัวดาเนินการทางตรรกะ (Logical Operators)
• เป็ นการดาเนินการทางตรรกะ ให้ ผลลัพธ์ออกมาเป็ น ค่าจริง
และ ค่าเท็จ เท่านั้น
!
&&
||
เช่ น (x
นิเสธ (NOT) unary operator
และ (AND) binary operator
หรือ (OR) binary operator
> 10) && (x < 20)
((x >= 10)||(x < 15)) && !(x==12)
8
241-101 Introduction to Computer Programming
! นิเสธ (NOT)
• ตัวดาเนินการนี้ จะต้ องการตัวถูกดาเนินการเพียงตัวเดียว
(unary operator)
• ผลลัพธ์ท่ไี ด้ มาจะเป็ นค่าตรงกันข้ ามกับ ค่าทางตรรกะเดิม
นิพจน์ a
!a
true
false
false
true
9
241-101 Introduction to Computer Programming
&& และ (AND) ,
|| หรือ (OR)
• Binary operators ต้องการตัวถูกดาเนินการ(operand) สองตัว
• a && b จะมีค่าเป็ นจริ ง เพียงกรณี เดียวคือ ค่าทั้งสองของนิพจน์ เป็ นจริ ง
• a || b จะมีค่าเป็ นเท็จ เพียงกรณี เดียวคือ ค่าทั้งสองของนิพจน์ เป็ นเท็จ
a
b
a && b
a || b
true
true
false
false
true
false
true
false
true
false
false
false
true
true
true
false
10
241-101 Introduction to Computer Programming
ลาดับความสาคัญของตัวดาเนินการ
หากในนิพจน์มีตวั ดาเนินการ(operator)มากกว่าหนึง่ และไม่มีวงเล็บ
ความสาคัญของ operator เป็ นไปดังนี้
operator
association
priority
! – ++ --
right to left
Highest
* / %
left to right
+ -
left to right
< <= > >=
left to right
== !=
left to right
&&
left to right
||
left to right
Lowest
11
241-101 Introduction to Computer Programming
ตัวอย่าง
กาหนดให้ int
i=5, j=7, k=12; float x=22.5;
นิพจน์ expression
Equivalent expression
value
i+2==k-1
(i+2)==(k-1)
0
3*i-j<22
( (3*i) - j ) < 22
i+2*j>k
( i+(2*j) ) > k
k+3<=-j+3*i
(k+3) <= ( (-j) + (3*i) )
1
1
0
’a’+1 == ’b’
(’a’+1 ) == ’b’
1
25>=x+4.0
25 >= (x+4.0)
i+j<=k==12-k
( (i+j) <= k )== (12-k)
i>6 || j-6 && k
(i>6) || ( (j-6) && k )
0
0
1
12
241-101 Introduction to Computer Programming
โครงสร้างแบบทางเลือก
 โปรแกรมจะเลือกการทางาน โดยพิจารณาจากเงื่อนไขที่กาหนด
คาสังเงื
่ อ่ นไขที่ใช้ในควบคุมการเลือกได้แก่
• if, if-else
• switch-case
ถ้า, ถ้า-ไม่เช่นนั้นแล้ว
เลือกไปตามกรณี
13
241-101 Introduction to Computer Programming
คาสัง่ เงื่อนไข if
• จะเลือกทาคาสัง่ (หรือกลุ่มคาสัง)่ ก็ต่อเมือ่ ตรวจสอบเงือ่ นไข
แล้วเป็ นจริง
•ถ้าเงือ่ นไขเป็ นเท็จ ก็ไม่ทาอะไร
ตัวอย่าง Flowchart ของโครงสร้างแบบทางเลือก if
เงือ่ นไข
เท็จ
จริง
คาสัง่
14
241-101 Introduction to Computer Programming
โครงสร้างแบบทางเลือก if
เงือ่ นไขในการตรวจสอบจะเป็ นชนิด
boolean คือ จริง (true) หรือ เท็จ (false)
if
if เป็ นคาสงวน
(เงือ่ นไขที่ตอ้ งการตรวจสอบ)
คาสัง่ ;
• ถ้ าเงือ่ นไขเป็ นจริง คาสั่ งนีจ้ ะถูกทางาน
• ถ้ าเงือ่ นไขเป็ นเท็จ โปรแกรมจะไม่ ทาคาสั่ งนี้
ข้อผิดพลาดที่เกิดขึ้นบ่อย: หลังวงเล็บเงื่อนไข ห้ามใส่ ;
15
241-101 Introduction to Computer Programming
คาสัง่ เงื่อนไข if
• ตัวอย่างของการใช้คาสัง่ if
int x,abs_x;
abs_x=x;
if (x < 0)
abs_x = -x;
printf(“The absolute value x is %d”,abs_x);
โปรแกรมจะตรวจสอบว่าค่าของ x ว่าน้อยกว่า 0 หรื อไม่
• หากน้อยกว่า(จริ ง) ตัวแปร abs_x จะถูกเปลี่ยนค่าเป็ น -x
• หากมากกว่า(เท็จ) ค่า abs_x ยังคงเดิม ไม่เปลี่ยนแปลง
• หลังจากการทางานของคาสัง่ if
ในส่ วนของ printf ก็จะถูก เรี ยกใช้งานเป็ นลาดับต่อมา
(ไม่วา่ เงื่อนไขจะจริ งหรื อเท็จ)
16
241-101 Introduction to Computer Programming
ตัวอย่าง
ผลลัพธ์ของโปรแกรม
รันครั้งที่ 1
Enter your age: 21
Goodbye
รันครั้งที่ 2
Enter your age:
#include<stdio.h>
Your age is not
Goodbye
int main()
{ int age;
รันครั้งที่ 3
your age:
printf(”Enter your age: ”); Enter
Your age is not
Goodbye
scanf(“%d”,&age);
if(age < 0 || age > 150)
printf(“Your age is not valid!\n");
printf(”Goodbye\n”);
return 0;
}
210
valid
-10
valid
17
241-101 Introduction to Computer Programming
ตัวอย่าง
#include<stdio.h>
void main()
{
int score;
printf(“Enter your score :");
scanf("%d",&score);
if(score>=49)
printf("You pass :-)\n");
printf("Good bye!!\n");
}
ผลลัพธ์ของโปรแกรม 1
ผลลัพธ์ของโปรแกรม 2
Enter your score : 67
Enter your score : :34
You pass :-)
Good bye!!
Good bye!!
18
241-101 Introduction to Computer Programming
เงื่อนไขของคาสัง่ if
• เงือ่ นไขทีต่ อ้ งการตรวจสอบสามารถนามารวมกันได้ เช่ น
#include<stdio.h>
int main ()
{
int a =2,b=7;
if(a>0)
{
if (b>0)
printf("OK.");
}
}
#include<stdio.h>
int main ()
{
int a =2,b=7;
if((a>0)&& (b>0))
printf("OK.");
}
19
241-101 Introduction to Computer Programming
แบบฝึ กหัด
• จงเขียนโปรแกรมคานวณการคิดเกรด โดยให้ รับ
คะแนนจากผู้ใช้ หากคะแนนมีค่ามากกว่ าหรือเท่ ากับ 80
ให้ แสดงเกรด ‘A’
#include<stdio.h>
int main ()
{
int score;
printf("Enter your score") ;
scanf("%d",&score);
if(score >=80)
printf("You get grade A \n");
}
20
241-101 Introduction to Computer Programming
คาสัง่ เงื่อนไข if-else
• else สามารถถูกเพิม
่ ในคาสังเงื
่ อ่ นไขของ if ได้ โปรแกรมจะ
ทางานทีค่ าสัง่ else เมือ่ เงือ่ นไขถูกตรวจสอบว่าเป็ นเท็จ
ตัวอย่ าง Flowchart ของโครงสร้ างแบบทางเลือก if-else
เงือ่ นไข
จริง
คาสั่ งที่ 1
เท็จ
คาสั่ งที่ 2
คาสั่ งที่ 3
21
241-101 Introduction to Computer Programming
โครงสร้างแบบทางเลือก if-else
if (เงือ่ นไขทีต
่ อ้ งการตรวจสอบ)
คาสังที
่ ่ 1;
else
คาสังที
่ ่ 2;
• หากเงือ่ นไขเป็ น “จริง” โปรแกรมจะทางานใน คาสังที
่ ่1
• หากเงือ่ นไขเป็ น “เท็จ” โปรแกรมจะทางานใน คาสังที
่ ่2
22
241-101 Introduction to Computer Programming
#include<stdio.h>
int main ()
{
int a,b;
printf("Enter integer
scanf("%d",&a);
printf("Enter integer
scanf("%d",&b);
if(a>b)
printf("%d is
else
printf("%d is
}
1: ") ;
2: ") ;
greater than %d\n",a,b);
less than %d\n",a,b);
ผลลัพธ์ของโปรแกรม 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
23
241-101 Introduction to Computer Programming
แบบฝึ กหัด
ให้ นศ.เขียนโปรแกรมคานวณการคิดเกรด โดยให้
รับคะแนนจากผู้ใช้ หากคะแนนมีค่ามากกว่ าหรือ
เท่ ากับ 80 ให้ แสดงเกรด ‘A’ หากน้ อยกว่ า 80 ให้
แสดงเกรด ‘B’
24
241-101 Introduction to Computer Programming
#include<stdio.h>
int main ()
{
int score;
printf("Enter your score ") ;
scanf("%d",&score);
if(score >=80)
printf("You get grade A \n");
else
printf("You get grade B \n");
}
25
241-101 Introduction to Computer Programming
กลุ่มคาสัง่ (Block Statements)
• คาสั่งที่อยู่ภายใต้ คาสั่ง if-else สามารถมีได้ มากกว่า 1 คาสั่ง
เรียกว่า กลุ่มคาสั่ง หรือ Block Statements
• กลุ่มคาสั่งจะอยู่ภายในเครื่องหมาย {……}
26
241-101 Introduction to Computer Programming
#include<stdio.h>
int main ()
{
int points= 44;
if (points>=50)
{
printf("Pass exam......\n");
printf("Congratulations!\n");
}
else
{
printf("Fail......\n");
printf("Attempt again\n");
}
printf("Bye bye....See you again next semester\n");
กลุ่มคาสั่ ง
กลุ่มคาสั่ ง
}
Fail......
Attempt again
Bye bye....See you again next semester
27
241-101 Introduction to Computer Programming
คาสัง่ เงื่อนไข if-else-if-else (Nested if)
ตัวอย่าง Flowchart ของโครงสร้างแบบทางเลือก nested if
(if ทีซ่ อ้ นกันเป็ นชั้นๆ)
เงือ่ นไขที่ 1
จริง
คาสั่งที่ 1
เท็จ
เงือ่ นไขที่ 2
จริง
คาสั่งที่ 2
เท็จ
เงือ่ นไขที่ 3
เท็จ
จริง
คาสั่งที่ 3
คาสั่งที่ 4
คาสั่ งที่ 5
28
241-101 Introduction to Computer Programming
โครงสร้างของ if-else-if-else
if (เงือ่ นไขที่ 1)
คาสังที
่ ่ 1; หรือ กลุ่มคาสังที
่ ่1
else if (เงือ่ นไขที่ 2)
คาสังที
่ ่ 2;
else if (เงือ่ นไขที่ 3)
คาสังที
่ ่ 3;
else คาสังที
่ ่ 4;
คาถาม: 1. คาสังที
่ ่2 จะกระทา เมือ่ เงือ่ นไข1,2,3 เป็ นอย่างไร?
2. คาสังที
่ ่4 จะกระทา เมือ่ เงือ่ นไข1,2,3 เป็ นอย่างไร?
3. ถ้าเงือ่ นไข 1 เท็จ และ เงือ่ นไข 2 และ 3 เป็ นจริง
โปรแกรมจะทาคาสังใดบ้
่ าง? 4.หากเงือ่ นไขเป็ นจริงทั้งหมด?
29
241-101 Introduction to Computer Programming
#include<stdio.h>
int main ()
{
int a,b;
printf("Enter integer
scanf("%d",&a);
printf("Enter integer
scanf("%d",&b);
if(a>b)
printf("%d is
else if (a==b)
printf("%d is
else
printf("%d is
}
1: ") ;
2: ") ;
greater than %d\n",a,b);
equal to %d\n",a,b);
less than %d\n",a,b);
ผลลัพธ์ของโปรแกรม รันครั้งที่1 ผลลัพธ์ของโปรแกรม รันครั้งที่2
Enter integer 1: 22
Enter integer 1: 22
Enter integer 2: 22
Enter integer 2: 77
22 is equal to 22
22 is less than 77
30
241-101 Introduction to Computer Programming
แบบฝึ กหัด
ให้ นศ.เขียนโปรแกรมคานวณการคิดเกรด โดยให้ รับคะแนนจากผู้ใช้ หากคะแนนมีค่า
-มากกว่ าหรือเท่ ากับ 80 ให้ แสดงเกรด ‘A’
-มากกว่ าหรือเท่ ากับ 70 ให้ แสดงเกรด ‘B’
-มากกว่ าหรือเท่ ากับ 60 ให้ แสดงเกรด ‘C’
-มากกว่ าหรือเท่ ากับ 50 ให้ แสดงเกรด ‘D’
-น้ อยกว่ า 50 ให้ แสดงข้ อความว่ า fail……………
-และไม่ ว่าจะได้ เกรดอะไรก็ตาม ให้ แสดงข้ อความ Bye bye....See you again next
semester ทุกกรณี
31
241-101 Introduction to Computer Programming
#include<stdio.h>
int main()
{
float points;
printf("Please Enter Your Score ");
scanf("%f",&points);
if (points>=80.0){
printf("Congratulations!\n");
printf("You get grade A\n");
}
else if (points>=70.0)
printf("You get grade B\n");
else if (points>=60.0)
printf("You get grade C\n");
else if (points>=50.0)
printf("You get grade D\n");
else
printf("Fail......\n");
printf("Bye bye....See you again next semester\n");
}
ผลลัพธ์ รันครั้งที่ 1
Please Enter Your Score 89
Congratulations!
You get grade A
Bye bye....See you again next semester
ผลลัพธ์ รันครั้งที่ 2
Please Enter Your Score 55
You get grade D
Bye bye....See you again next semester
32
241-101 Introduction to Computer Programming
คาสังทางเลื
่
อก switch-case
•
•
•
ใช้ ควบคุมให้ โปรแกรมเลือกดาเนินการไปในเส้ นทางใดเส้ นทางหนึ่ง(case) จาก
ทางเลือกหลายๆทาง โดยใช้ ค่าที่ต้องการตรวจสอบว่าตรงกับค่าใด
นิพจน์ท่อี ยู่ในคาสั่ง switch จะถูกตรวจสอบว่าตรงกับ case ใด แล้ วโปรแกรมก็
จะทางานตามคาสั่งที่อยู่ใน case นั้น และคาสัง่ อื่นๆทีต่ ามมาจนจบโครงสร้ าง
switch-case หรือเจอคาสั่ง break ก็จะออกจากโครงสร้ าง switch-case
โดยค่าที่ใช้ ตรวจสอบ จะต้ องเป็ นจานวนเต็มหรือตัวอักษรเท่านั้น (int, char)
ค่าสาหรับ
ตรวจสอบ
ค่าที่ 1
คาสั่ งที่ 1
ค่าที่ 2
คาสั่ งที่ 2
ค่าที่ 3
คาสั่ งที่ 3
33
241-101 Introduction to Computer Programming
โครงสร้างแบบทางเลือก
switch-case
switch ( นิพจน์ ทตี่ ้ องการตรวจสอบ )
{
case ค่าที่ 1 :
case ค่าที่ 2 :
case ค่าที่ 3 :
case … …
คาสั่งที่ 1;
คาสั่งที่ 2;
คาสั่งที่ 3;
switch และ case
คือคาสงวน
}
•นิพจน์ท่อี ยู่ในคาสั่ง switch จะถูกตรวจสอบตามลาดับว่าตรงกับ case ใด โปรแกรมก็
จะทางานตามคาสั่งที่อยู่ใน case นั้น รวมถึงคาสั่งอื่นๆที่ตามมา ใน case ที่เหลือจนจบ
โครงสร้ าง switch-case หรือเจอคาสั่ง break ก็จะออกจากโครงสร้ าง switch-case ได้
34
241-101 Introduction to Computer Programming
#include<stdio.h>
int main()
{
int c;
printf("Enter integer 1 or 2 or 3:");
scanf("%d",&c);
ผลลัพธ์
switch(c)
Enter
{
THREE
case 1: printf("ONE\n");
integer 1 or 2 or 3: 3
case 2: printf("TWO\n");
case 3: printf("THREE\n");
}
}
ผลลัพธ์
ผลลัพธ์
Enter integer 1 or 2 or 3: 2
TWO
THREE
ไม่ ต้องการ
Enter integer 1 or 2 or 3: 1
ONE
TWO
ไม่ ต้องการ
THREE
35
241-101 Introduction to Computer Programming
คาสัง่ break
•
•
•
คาสั่ง break จะใช้ สาหรับการควบคุมการกระทา โดยบังคับการกระทา
บ่อยครั้งที่จะใช้ คาสั่ง break เป็ นคาสั่งสุดท้ ายในแต่ละ case
หากไม่มีคาสั่ง break ในชุดคาสั่งของ case ใด โปรแกรมจะทางานต่อไป ใน
คาสั่งของ case ถัดๆไป ด้ วยจนจบ
switch (นิพจน์ ที่ต้องการตรวจสอบ)
{
case
ค่าที่ 1 : คาสั่งที่ 1;
ค่าที่ 2 : คาสั่งที่ 2;
ค่าที่ 3 : คาสัง่ ที่ 3;
case
… … break;
case
case
break;
break;
break;
}
36
241-101 Introduction to Computer Programming
#include<stdio.h>
ผลลัพธ์
int main()
Enter integer 1 or 2 or 3:3
{
THREE
int c;
printf("Enter integer 1 or 2 or 3:");
scanf("%d",&c);
switch(c)
{
case 1: printf("ONE\n");
break;
case 2: printf("TWO\n");
break;
case 3: printf("THREE\n");
break;
}
ผลลัพธ์
}
ผลลัพธ์
Enter integer 1 or 2 or 3:2
TWO
Enter integer 1 or 2 or 3:1
ONE
37
241-101 Introduction to Computer Programming
คาสังเงื
่ อ่ นไข
•
•
•
switch-case
default
เป็ นคาสงวน
default เป็ นอีกกรณีหนึง่ ในคาสัง่ switch-case
มักวางไว้เป็ นกรณีสุดท้าย
ในกรณีทีต่ รวจสอบแล้วพบว่า นิพจน์ มีค่าไม่ตรงกับ case
ใดๆเลยข้างต้น โปรแกรมจะเข้าไปทางานในส่วนของ
default
•
ไม่จาเป็ นต้องใส่ break หลังชุดคาสังของ
่
default
38
241-101 Introduction to Computer Programming
#include<stdio.h>
ผลลัพธ์
int main()
Enter integer 1 or 2 or 3 4
{
Out of range
int c;
printf("Enter integer 1 or 2 or 3 ");
scanf("%d",&c);
switch(c)
{
case 1: printf("ONE\n");
break;
case 2: printf("TWO\n");
break;
case 3: printf("THREE\n");
break;
default: printf("Out of range");
}
}
39
241-101 Introduction to Computer Programming
#include<stdio.h>
int main()
{
char grade;
printf("Enter your grade: ");
scanf("%c",&grade);
switch(grade)
{ case ’a’:
case ’A’: printf(“Very Good\n");
break;
case ’b’:
ผลลัพธ์
case ’B’: printf(“Good\n");
Enter your
break;
Very good
case ’c’:
Enter your
case ’C’: printf(“Fair\n");
No good!
break;
default: printf(“No good!\n"); Enter your
}
Fair
}
grade: a
grade: F
grade: C
Enter your grade: 3
No good!
40
241-101 Introduction to Computer Programming
•
•
•
if-else vs switch-case
switch-case ใช้ในกรณีที่มีทางเลือกหลายทาง โดยขึ้ นอยู่กบั ค่าของตัวแปร
(หรือนิพจน์)หนึง่ ที่มีค่าเป็ น int หรือ char เท่านั้น
if-else ใช้ตรวจสอบเงือ่ นไข ได้หลากหลายกว่า ตามต้องการ (เช่น
เปรียบเทียบค่าน้อยกว่า มากกว่า หรืออยู่ในช่วงใดช่วงหนึง่ รวมถึงการใช้
logical operator สร้างเงือ่ นไขได้ซบั ซ้อนขึ้ น)แต่ถา้ มีหลายทางเลือก ต้องใช้
if ซ้อนกันหลายๆชั้น หรือสร้างเงือ่ นไขที่ซบั ซ้อนขึ้ น
บางกรณีสามารถแปลงคาสัง่ switch-case เป็ น if-else ได้ เช่น
switch(a)
{ case b: คาสั่ง_1; break;
case c: คาสั่ง_2; break;
case d: คาสั่ง_3; break;
default: คาสั่ง_4;
}
if(a==b)
คาสั่ ง_1;
else if (a==c)
คาสั่ ง_2;
else if (a==d)
คาสั่ ง_3;
else คาสั่ ง_4;
41
241-101 Introduction to Computer Programming
จบ if-else และ switch-case
•
จงเขียนโปรแกรม รับตัวอักษรหนึง่ ตัว แล้วตรวจสอบว่าเป็ น สระ(vowel) หรือ
พยัญชนะ (consonant) สมมติว่าผูใ้ ช้ใส่เฉพาะตัวอักษร a-z เท่านั้น
•
ให้เติมส่วนของโปรแกรมนี้ ให้สมบูรณ์
char ch;
printf(”Enter a character(a-z): ”);
scanf(”%c”,&ch);
เติมเต็มโปรแกรมในส่ วนนี ้ จะเลือกใช้โครงสร้าง if-else หรื อ switch-case ก็ได้
คำแนะนำ ให้ตรวจสอบค่า ch ว่าเป็ นตัวอักษร สระ a e i o u หรื อไม่ ถ้าใช่กพ็ ิมพ์
vowel ถ้าไม่ใช่ ก็ให้พิมพ์วา่ ว่าเป็ น consonant
printf(”End of the program.\n”);
42
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)
{
case ’a’:
printf(”Vowel\n”); break;
case ’e’:
printf(”Vowel\n”); break;
case ’i’:
printf(”Vowel\n”); break;
case ’o’:
printf(”Vowel\n”); break;
case ’u’:
printf(”Vowel\n”); break;
default :
printf(”Consonant\n”);
}
printf(”End of the program.\n”);
43
241-101 Introduction to Computer Programming
โครงสร้างแบบทาซ้ า
•
ทาไมต้องทาซ้ า?
•
งานบางอย่าง อาศัยการทางานซ้ าๆกัน อาจเหมือนกันทุกครั้ง
หรือในการทาแต่ละครั้ง มีค่าบางอย่างเปลีย่ นไป อย่างมีรูปแบบ
•
ตัวอย่าง ต้องการแสดงข้อความว่า Hello จานวน 200 ข้อความ
printf(“Hello
printf(“Hello
printf(“Hello
printf(“Hello
printf(“Hello
……….
");
");
");
");
");
200 ครั้ง
44
241-101 Introduction to Computer Programming
โครงสร้างแบบทาซ้ า
•
ตัวอย่าง ต้องการแสดง ตารางการคูณ เช่น
i=1; printf(“2
i++; printf(“2
i++; printf(“2
i++; printf(“2
……….
x
x
x
x
%d
%d
%d
%d
=
=
=
=
%d
%d
%d
%d
\n“,i,2*i);
\n“,i,2*i);
\n“,i,2*i);
\n“,i,2*i);
12 ครั้ง
ผลการรันโปรแกรม
2
2
2
2
x
x
x
x
1 = 2
2 = 4
3 = 6
4 = 8
……….
2 x 11 = 22
2 x 12 = 24
45
241-101 Introduction to Computer Programming
การทาซ้ า (ขาขัน)
เด็ก: เสร็ จแล้วค่ะครู
ครู : อืม... พยายามดีนะ (กะจะลองดีกับฉั นใช่ ไหม!?!)
เด็กฉลาด (แกมโกง)
ครูทาโทษให ้เด็กคัดบนกระดาน
“ฉั นจะไม่เล่นปาเครือ
่ งบินกระดาษในห ้องเรียนอีกแล ้ว” 500 จบ
ี ี่ ใช ้ for loop ให ้
แต่เด็ก(หัวใส) แอบเขียนเป็ นโค ้ดโปรแกรมภาษาซท
เขียนข ้อความนัน
้ 500 ครัง้
46
241-101 Introduction to Computer Programming
โครงสร้างแบบทาซ้ า
•
•
•
เป็ นการสังให้
่ โปรแกรมทางานอย่างใดอย่างหนึง่ ซ้ าๆกัน
ตามจานวนรอบทีต่ อ้ งการ หรือ ตามเงือ่ นไขทีก่ าหนด
การวนรอบบางครั้งจะเรียกว่า loop หรือการวนลูป
กลุ่มคาสังการท
่
าซ้ า มีสามประเภท ได้แก่ for loop,
while loop และ do-while loop
47
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;
for (i=1; i<=12; i++ )
{
printf(”2 x %d = %d \n”,i, 2*i);
}
48
241-101 Introduction to Computer Programming
คาสังการท
่
าซ้ า
for
 หากเงือ่ นไขเป็ นจริง loop ก็จะยังคงทางานต่อไป จนกระทัง่
เงือ่ นไขเป็ นเท็จ ตัวอย่างเช่น
 การปรับค่าตัวแปร (ลด/เพิม่ ) อาจจะส่งผลให้มีเกิดทางานไม่รู ้
จบของ loop ได้ (infinite loop)
ผลลัพธ์
#include<stdio.h>
#include<stdio.h>
int main()
int main()
{
{
int i;
int i;
for (i=0;i<5;i--)
for (i=0;i<5;i++)
printf("*
printf("*
\n"); \n");
}
}
*
*
*
*
*
เกิดการวนรอบ
แบบไม่รูจ้ บ
49
241-101 Introduction to Computer Programming
Flow chart การทางาน คาสังการท
่
าซ้ า for
การกาหนดค่ าเริ่มต้ นตัวแปร
ตรวจสอบเงือ่ นไข
จริง
เท็จ
คาสั่ ง
ปรับค่ าตัวแปร
50
241-101 Introduction to Computer Programming
การใช้งานคาสัง่ for

มักใช้ ในกรณี รูจ้ านวนรอบการทาซ้ า ที่แน่นอน หรือ มีตวั นับจานวนรอบ
(counter) กากับ
 ตัว counter ต้องถูกกาหนดค่าเริ่มต้น, และมีการปรับเปลีย่ นค่า
 ควรระวังการกาหนดเงือ่ นไขการทาซ้ า ที่จะต้องกลายเป็ นเท็จได้ เพือ่ ไม่ทาให้
เกิดลูปอนันต์ (infinite loop)
 การกาหนด จานวนรอบ อย่างง่าย
ถ้าต้องการทา n รอบ
int i,n;
for (i=0; i<n; i++)
{ printf(”%d \n”,i); }
int i,n;
for (i=1; i<=n; i++)
{ printf(”%d \n”,i); }
51
241-101 Introduction to Computer Programming
คาสังการท
่
าซ้ า
•
while
รูปแบบของ while loop คือ
(เงือ่ นไข)
คาสัง;่
while
while เป็ นคาสงวน
หากเงื่อนไขเป็ นจริ ง โปรแกรมจะ
ทางานตามคาสั่งซ้ า จนกระทัง่
เงื่อนไขเป็ นเท็จ
ในขณะที่ เงือ่ นไข เป็ นจริง
ให้ ทาคาสั่ ง
หรือ ทาคาสั่ งนั้นจนกว่ า
เงือ่ นไขจะกลายเป็ นเท็จ
52
241-101 Introduction to Computer Programming
Flowchart คาสัง่ while
ตรวจสอบเงือ่ นไข
การทาซ้า
จริง
เท็จ
คาสั่ ง
53
241-101 Introduction to Computer Programming
คาสังการท
่
าซ้ า
while
while loop
for 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++;
}
}
54
241-101 Introduction to Computer Programming
เปรียบเทียบโครงสร้ างของ for loop กับ while loop
for loop
for
(การกาหนดค่าเริม่ ต้นตัวแปร ; เงือ่ นไข ; ปรับค่าตัวแปร)
คาสัง;่
while loop
การกาหนดค่ าเริ่มต้ นตัวแปร;
while (เงื่อนไข){
คาสั่ ง;
ปรับค่ าตัวแปร;
}
55
241-101 Introduction to Computer Programming
คาสังการท
่
าซ้ า do-while
•
โครงสร้างแบบการทาซ้ า while และ for จะต้องมีการ
ตรวจสอบค่าของเงือ่ นไขก่อนว่าเป็ นจริงหรือเท็จ ก่อนทีจ่ ะ
ทาคาสัง่ (กลุ่มคาสัง)่ ภายในรอบ
•
ถ้ าต้ องทาคาสั่งภายในรอบก่อนอย่างน้ อย 1 ครั้ง แล้ วจึง
ตรวจสอบเงื่อนไข เมื่อจบรอบ ให้ ใช้ โครงสร้ าง do-while
56
241-101 Introduction to Computer Programming
โครงสร้าง do while loop
do
{
กลุ่มคาสัง;
่
}
while ( เงื่อนไข ) ;
มี ; เพื่อจบคาสัง่
do-while
57
241-101 Introduction to Computer Programming
ตัวอย่าง do while loop
รอบเดียว
ลูปนีท้ ากีร่ อบ?
ถ้ าต้ องการให้ ทา 6 รอบ ต้ องแก้ ไข อย่ างไร
#include<stdio.h>
int main()
{
int i=6;
do
{
printf("* \n");
i++;
}
while (i<5);
}
#include<stdio.h>
int main()
{
int i=6;
do
{
printf("* \n");
i++;
}
while (i<12);
}
58
241-101 Introduction to Computer Programming
เปรียบเทียบโครงสร้ าง do while กับ while loop
while loop
do loop
ตรวจสอบเงือ่ นไข
การทาซ้า
คาสั่ ง
จริง
จริง
คาสั่ ง
เท็จ
ตรวจสอบเงือ่ นไข
การทาซ้า
เท็จ
59
241-101 Introduction to Computer Programming
จงเขียนโปรแกรมคานวณ ค่าเฉลีย่ คะแนนของ นร. 10 คน
Enter score of student 1: 98
Enter score of student 2 : 76
Enter score of student 3 : 71
Enter score of student 4: 65
Enter score of student 5 : 89
Enter score of student 6 : 75
Enter score of student 7: 78
Enter score of student 8 : 76
Enter score of student 9 : 71
Enter score of student 10: 80
Class average is
int n; float score,avg,sum;
n=1; sum=0;
do {
printf(”Score of st %d”,n);
scanf(”%f”,&score);
sum = sum + score;
n++;
} while (n<=10);
avg = sum/10.0;
printf(”Class average is %.2f”,
avg);
77.90
60
241-101 Introduction to Computer Programming
จงเขียนโปรแกรมเพือ่ แสดงผลตามด้านล่าง
Enter Result (1=pass, 2=fail): 1
Enter Result (1=pass, 2=fail): 1
Enter Result (1=pass, 2=fail): 1
Enter Result (1=pass, 2=fail): 2
Enter Result (1=pass, 2=fail): 1
Enter Result (1=pass, 2=fail): 1
Enter Result (1=pass, 2=fail): 1
Enter Result (1=pass, 2=fail): 1
Enter Result (1=pass, 2=fail): 1
Enter Result (1=pass, 2=fail): 1
Passed = 9
Failed = 1
int i,res, npass,nfail;
npass=0; nfail=0;
for (i=0;i<10;i++)
{ printf(”Enter result(1=pass,
2=fail)\n”);
scanf(”%d”,&res);
if (res == 1) npass++;
else nfail++;
}
printf(”Passed = %d \n”,npass);
printf(”Failed = %d \n”,nfail);
61
241-101 Introduction to Computer Programming
จงหาผลลัพธ์ของโปรแกรมด้านล่าง
#include<stdio.h>
#define num 5
int main(){
int i,j;
for (i=0;i<num;i++){
for(j=0; j<=i; j++){
printf("*");
}
printf("\n");
}
return 0;
}
*
**
***
****
*****
62
241-101 Introduction to Computer Programming
จงหาผลลัพธ์ของโปรแกรมด้านล่าง
#include<stdio.h>
#define num 5
int main(){
int i,j;
for (i=num;i>0;i--){
for(j=0;j<i;j++){
printf("*");
}
printf("\n");
}
return 0;
}
*****
****
***
**
*
63
241-101 Introduction to Computer Programming
โปรแกรมรอรับข้อมูลจนกว่าจะได้ค่าในขอบเขต
ทีก่ าหนด
#include<stdio.h>
int main(){
char grade;
printf(”Enter a grade:”);
scanf(”%c”,&grade);
while( grade<’A’|| grade>’E’)
{ printf(”Grade must be A-E\n”);
printf(”Enter a grade again:”);
scanf(”%c”,&grade);
}
printf(”%c OK\n”,grade);
return 0;
}
Enter
A OK
---Enter
Grade
Enter
E OK
----
a grade: A
Enter
Grade
Enter
Grade
Enter
Grade
Enter
B OK
----
a grade: Z
must be A-E
a grade again: 4
must be A-E
a grade again: b
must be A-E
a grade again: B
a grade: e
must be A-E
a grade again: E
64
241-101 Introduction to Computer Programming
โปรแกรมรอรับข้อมูลจนกว่าจะได้ค่าในขอบเขต
ใช้ do-while loop
#include<stdio.h>
int main(){
char grade ; int ok;
do{
ok=1;
printf(”Enter a grade:”);
scanf(”%c”,&grade);
if(grade<’A’|| grade>’E’)
ok=0;
if (!ok)
{ printf(”Grade must be A-E\n”);
printf(“Please enter again.”);
}
} while(!ok);
printf(”%c is OK\n”,grade);
return 0;
}
Enter a grade: A
A is OK
---Enter a grade: e
Grade must be A-E
Please enter again.
Enter a grade: E
E is OK
---Enter a grade: Z
Grade must be A-E
Please enter again.
Enter a grade: b
Grade must be A-E
Please enter again.
Enter a grade again: B
B is OK
---65