242-101, 241-101 Introduction to Computer Programming

Download Report

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

242-101, 241-101 Introduction to Computer Programming
บทที่ 3
โครงสร้างควบคุม
Control Structures
คณาจารย์ภาควิชาวิศวกรรมคอมพิวเตอร์
[email protected]
Department of Computer Engineering, PSU
242-101, 241-101 Introduction to Computer Programming
วัตถุประสงค์
• เข้าใจการทางานของคาสังแบบทางเลื
่
อก ได้แก่
− if, if–else ,switch-case
• เข้าใจการทางานของคาสังแบบท
่
าซ้ า ได้แก่
− for , while และ do-while
• สามารถนาคาสังต่
่ างๆไปประยุกต์ใช้เพือ่ ควบคุมการทางานของ
โปรแกรมตามทีต่ อ้ งการได้
Department of Computer Engineering, PSU
2
242-101, 241-101 Introduction to Computer Programming
เนื้ อหาในบทเรียน
• เครือ่ งหมายทีใ่ ช้ในการเปรียบเทียบ
• โครงสร้างการทางานของคาสัง่ if else และ switch case
• ทาไมต้องทาซ้ า??
• โครงสร้างการทางานของคาสัง่ for, while และ do while
• การเปลีย่ นคาสังจาก
่
for เป็ น do และ จากคาสัง่ do เป็ น for
Department of Computer Engineering, PSU
3
242-101, 241-101 Introduction to Computer Programming
โครงสร้างควบคุม

โดยปกติโปรแกรมจะทางานต่อเนือ่ งตามลาดับคาสัง่
เรียกว่า Sequence หรือการทางานแบบ Sequential
 โครงสร้างควบคุม เป็ นการกาหนดการทางานของโปรแกรม
ให้เป็ นไปในทิศทางทีต่ อ้ งการ หรือเรียกว่า flow of control
 คาสังที
่ ใ่ ช้ในการควบคุมแบ่งได้เป็ น 2 ประเภทคือ
 โครงสร้างแบบทางเลือก (Selection)
 โครงสร้างแบบทาซ้ า (Repetition)
Department of Computer Engineering, PSU
4
242-101, 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 ต้องไม่ใช่สิบ
Department of Computer Engineering, PSU
5
242-101, 241-101 Introduction to Computer Programming
เครื่องหมายเปรียบเทียบ
•เครื่องหมายที่ใช้ ในการเปรียบเทียบ (relational operators)และให้ ผลลัพธ์เป็ น
จริง หรือ เท็จ ได้ แก่
ตัวดาเนินการ
ความหมาย
ตัวอย่าง
==
เท่ากันหรือไม่
x == 3
>
มากกว่าหรือไม่
x > y
<
น้อยกว่าหรือไม่
x < y
>=
มากกว่าหรือเท่ากันหรือไม่
x >= y
<=
น้อยกว่าหรือเท่ากันหรือไม่
x <= y
!=
ไม่เท่ากันหรือไม่
x != y
หมายเหตุ เครื่องหมาย = นั้นเป็ นการกาหนดค่ า (assignment statement)
แต่ == นั้น เป็ นการเปรียบเทียบการเท่ ากัน (equality test)
Department of Computer Engineering, PSU
6
242-101, 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
จริง
Department of Computer Engineering, PSU
7
242-101, 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)
Department of Computer Engineering, PSU
8
242-101, 241-101 Introduction to Computer Programming
! นิเสธ (NOT)
• ตัวดาเนินการนี้ จะต้ องการตัวถูกดาเนินการเพียงตัวเดียว
(unary operator)
• ผลลัพธ์ท่ไี ด้ มาจะเป็ นค่าตรงกันข้ ามกับ ค่าทางตรรกะเดิม
นิพจน์ a
!a
true
false
false
Department of Computer Engineering, PSU
true
9
242-101, 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
Department of Computer Engineering, PSU
10
242-101, 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
Department of Computer Engineering, PSU
Lowest
11
242-101, 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
Department of Computer Engineering, PSU
12
242-101, 241-101 Introduction to Computer Programming
โครงสร้างแบบทางเลือก
 โปรแกรมจะเลือกการทางาน โดยพิจารณาจากเงื่อนไขที่กาหนด
คาสังเงื
่ อ่ นไขที่ใช้ในควบคุมการเลือกได้แก่
• if, if-else
• switch-case
Department of Computer Engineering, PSU
ถ้า, ถ้า-ไม่เช่นนั้นแล้ว
เลือกไปตามกรณี
13
242-101, 241-101 Introduction to Computer Programming
คาสัง่ เงื่อนไข if
• จะเลือกทาคาสัง่ (หรือกลุ่มคาสัง)่ ก็ต่อเมือ่ ตรวจสอบเงือ่ นไข
แล้วเป็ นจริง
•ถ้าเงือ่ นไขเป็ นเท็จ ก็ไม่ทาอะไร
ตัวอย่าง Flowchart ของโครงสร้างแบบทางเลือก if
เงือ่ นไข
เท็จ
จริง
คาสัง่
Department of Computer Engineering, PSU
14
242-101, 241-101 Introduction to Computer Programming
โครงสร้างแบบทางเลือก if
เงือ่ นไขในการตรวจสอบจะเป็ นชนิด
boolean คือ จริง (true) หรือ เท็จ (false)
if
if เป็ นคาสงวน
(เงือ่ นไขที่ตอ้ งการตรวจสอบ)
คาสัง่ ;
• ถ้ าเงือ่ นไขเป็ นจริง คาสั่ งนีจ้ ะถูกทางาน
• ถ้ าเงือ่ นไขเป็ นเท็จ โปรแกรมจะไม่ ทาคาสั่ งนี้
ข้อผิดพลาดที่เกิดขึ้นบ่อย: หลังวงเล็บเงื่อนไข ห้ามใส่ เครื่ องหมาย ;
Department of Computer Engineering, PSU
15
242-101, 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 ก็จะถูก เรี ยกใช้งานเป็ นลาดับต่อมา
(ไม่วา่ เงื่อนไขจะจริ งหรื อเท็จ)
Department of Computer Engineering, PSU
16
242-101, 241-101 Introduction to Computer Programming
ตัวอย่าง
ผลลัพธ์ของโปรแกรม
รันครั้งที่ 1
#include<stdio.h>
int main()
Enter your age: 21
{ int age;
Goodbye
printf(”Enter your age: ”);
scanf(“%d”,&age);
รันครั้งที่ 2
if(age < 0 || age > 150) Enter your age: 210
printf(“Your age is not valid!\n");
Your age is not valid
printf(”Goodbye\n”);
Goodbye
return 0;
รันครั้งที่ 3
}
Enter your age: -10
Your age is not valid
Goodbye
Department of Computer Engineering, PSU
17
242-101, 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!!
Department of Computer Engineering, PSU
18
242-101, 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.");
}
}
Department of Computer Engineering, PSU
#include<stdio.h>
int main ()
{
int a =2,b=7;
if((a>0)&& (b>0))
printf("OK.");
}
19
242-101, 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");
}
Department of Computer Engineering, PSU
20
242-101, 241-101 Introduction to Computer Programming
คาสัง่ เงื่อนไข if-else
• else สามารถถูกเพิม
่ ในคาสังเงื
่ อ่ นไขของ if ได้ โปรแกรมจะ
ทางานทีค่ าสัง่ else เมือ่ เงือ่ นไขถูกตรวจสอบว่าเป็ นเท็จ
ตัวอย่ าง Flowchart ของโครงสร้ างแบบทางเลือก if-else
เงือ่ นไข
จริง
คาสั่ งที่ 1
เท็จ
คาสั่ งที่ 2
คาสั่ งที่ 3
Department of Computer Engineering, PSU
21
242-101, 241-101 Introduction to Computer Programming
โครงสร้างแบบทางเลือก if-else
if (เงือ่ นไขทีต
่ อ้ งการตรวจสอบ)
คาสังที
่ ่ 1;
else
คาสังที
่ ่ 2;
• หากเงือ่ นไขเป็ น “จริง” โปรแกรมจะทางานใน คาสังที
่ ่1
• หากเงือ่ นไขเป็ น “เท็จ” โปรแกรมจะทางานใน คาสังที
่ ่2
Department of Computer Engineering, PSU
22
242-101, 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
Department of Computer Engineering, PSU
23
242-101, 241-101 Introduction to Computer Programming
แบบฝึ กหัด
จงเขียนโปรแกรมประเมินผลการเรี ยนในรายวิชาหนึ่ง
โดยให้รับคะแนนจากผูใ้ ช้ หากคะแนนมีค่ามากกว่า
หรื อเท่ากับ 50 ให้แสดงเกรด ‘P’ (pass) แต่หากได้
คะแนนน้อยกว่า 50 ให้แสดงเกรด ‘F’ (fail)
Department of Computer Engineering, PSU
24
242-101, 241-101 Introduction to Computer Programming
ตัวอย่างโปรแกรม
#include<stdio.h>
int main ()
{
int score;
printf("Enter your score ") ;
scanf("%d",&score);
if (score >=50)
printf("You get grade P \n");
else
printf("You get grade F \n");
return 0;
}
Department of Computer Engineering, PSU
25
242-101, 241-101 Introduction to Computer Programming
Conditional Operator ? :
• ภาษาซีมีตัวดาเนินการเงื่อนไข (Conditonal operator) ที่ใช้ เลือกค่า
ข้ อมูลค่าใดค่าหนึ่งจากตัวเลือกสองค่า โดยพิจารณาจากเงื่อนไข
• รูปแบบ: X ? Y : Z
ถ้ า X เป็ นจริง นิพจน์น้ จี ะมีค่าเท่ากับ Y
แต่ถ้า X เป็ นเท็จ นิพจน์น้ ีจะมีค่าเท่ากับ Z
• ตัวอย่างการใช้ งาน
a จะมีค่าเป็ น 5 ถ้ า b มากกว่า 0 แต่ a
จะมีค่าเป็ น 10 ถ้ า b น้ อยกว่าหรือเท่ากับ 0
a = b>0?5:10;
printf(“abs x = %.2f\n”, x>=0.0?x:-x);
ประโยคนี้จะพิมพ์ค่าสัมบูรณ์ของ x
Department of Computer Engineering, PSU
26
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 แทนจะได้
if(b>0) a = 5; else a=10;
printf(“abs x = %.2f\n”,x>=0.0?x:-x);
ใช้ if-else แทนจะได้
if(x>=0.0) printf(abs x = %.2f\n”,x);
else printf(abs x = %.2f\n”,-x);;
Department of Computer Engineering, PSU
27
242-101, 241-101 Introduction to Computer Programming
แบบฝึ กหัด
จงเขียนโปรแกรมประเมินผลการเรี ยนในรายวิชาหนึ่ง
โดยให้รับคะแนนจากผูใ้ ช้ หากคะแนนมีค่ามากกว่า
หรื อเท่ากับ 50 ให้แสดงเกรด ‘P’ (pass) แต่หากได้
คะแนนน้อยกว่า 50 ให้แสดงเกรด ‘F’ (fail)
กาหนดให้ใช้ Conditional operator
Department of Computer Engineering, PSU
28
242-101, 241-101 Introduction to Computer Programming
ตัวอย่างโปรแกรม conditonal operator
#include<stdio.h>
int main ()
{
int score;
printf("Enter your score ") ;
scanf("%d",&score);
printf("You get grade %c \n“,
score>=50 ? ’P’ : ’F’);
return 0;
}
Department of Computer Engineering, PSU
29
242-101, 241-101 Introduction to Computer Programming
กลุ่มคาสัง่ (Block Statements)
• คาสั่งที่อยู่ภายใต้ คาสั่ง if-else สามารถมีได้ มากกว่า 1 คาสั่ง
เรียกว่า กลุ่มคาสั่ง หรือ Block Statements
• กลุ่มคาสั่งจะอยู่ภายในเครื่องหมาย {……}
Department of Computer Engineering, PSU
30
242-101, 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
Department of Computer Engineering, PSU
31
242-101, 241-101 Introduction to Computer Programming
คาสัง่ เงื่อนไข if-else-if-else (Nested if)
ตัวอย่าง Flowchart ของโครงสร้างแบบทางเลือก nested if
(if ทีซ่ อ้ นกันเป็ นชั้นๆ)
เงือ่ นไขที่ 1
เท็จ
จริง
คาสั่งที่ 1
เงือ่ นไขที่ 2
จริง
คาสั่งที่ 2
เท็จ
เงือ่ นไขที่ 3
เท็จ
จริง
คาสั่งที่ 3
คาสั่งที่ 4
คาสั่ งที่ 5
Department of Computer Engineering, PSU
32
242-101, 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.หากเงือ่ นไขเป็ นจริงทั้งหมด?
Department of Computer Engineering, PSU
33
242-101, 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
Department of Computer Engineering, PSU
34
242-101, 241-101 Introduction to Computer Programming
แบบฝึ กหัด
ให้ นศ.เขียนโปรแกรมคานวณการคิดเกรด โดยให้ รับคะแนนจากผู้ใช้ หากคะแนนมีค่า
-มากกว่ าหรือเท่ ากับ 80 ให้ แสดงเกรด ‘A’
-มากกว่ าหรือเท่ ากับ 70 ให้ แสดงเกรด ‘B’
-มากกว่ าหรือเท่ ากับ 60 ให้ แสดงเกรด ‘C’
-มากกว่ าหรือเท่ ากับ 50 ให้ แสดงเกรด ‘D’
-น้ อยกว่ า 50 ให้ แสดงข้ อความว่ า fail……………
-และไม่ ว่าจะได้ เกรดอะไรก็ตาม ให้ แสดงข้ อความ Bye bye....See you again next
semester ทุกกรณี
Department of Computer Engineering, PSU
35
242-101, 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
ผลลั
พธ์พธ์รันรัครั
้ งที้ ง่ ที1่ 2
ผลลั
นprintf("Fail......\n");
ครั
Please
Enter bye....See
Your Score
printf("Bye
you 89
again
next semester\n");
Please
Enter Your Score
55
Congratulations!
}
You get grade D
You get grade A
Bye bye....See you again next semester
Bye bye....See you again next semester
Department of Computer Engineering, PSU
36
242-101, 241-101 Introduction to Computer Programming
คาสังทางเลื
่
อก switch-case
•
•
•
ใช้ ควบคุมให้ โปรแกรมเลือกดาเนินการไปในเส้ นทางใดเส้ นทางหนึ่ง(case) จาก
ทางเลือกหลายๆทาง โดยใช้ ค่าที่ต้องการตรวจสอบว่าตรงกับค่าใด
นิพจน์ท่อี ยู่ในคาสั่ง switch จะถูกตรวจสอบว่าตรงกับ case ใด แล้ วโปรแกรมก็จะ
ทางานตามคาสั่งที่อยู่ใน case นั้น และคาสัง่ อื่นๆทีต่ ามมาจนจบโครงสร้ าง
switch-case หรือเจอคาสั่ง break ก็จะออกจากโครงสร้ าง switch-case
โดยค่าที่ใช้ ตรวจสอบ จะต้ องเป็ นจานวนเต็มหรือตัวอักษรเท่านั้น (int, char)
ค่าสาหรับ
ตรวจสอบ
ค่าที่ 1
คาสั่ งที่ 1
Department of Computer Engineering, PSU
ค่าที่ 2
คาสั่ งที่ 2
ค่าที่ 3
คาสั่ งที่ 3
37
242-101, 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 ได้
Department of Computer Engineering, PSU
38
242-101, 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)
Enter integer 1 or 2 or 3: 2
{
TWO
case 1: printf("ONE\n");
THREE
ไม่ ต้องการ
case 2: printf("TWO\n");
ผลลัพธ์
case 3:
}
}
Enter integer 1 or 2 or 3: 1
printf("THREE\n");
ONE
TWO
THREE ไม่ ต้องการ
จะใช้ คาสั่ ง break; มาช่ วย
Department of Computer Engineering, PSU
39
242-101, 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;
}
Department of Computer Engineering, PSU
40
242-101, 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 integer 1 or 2 or 3:3
{
THREE
case 1: printf("ONE\n");
break;
ผลลัพธ์
case 2: printf("TWO\n");
Enter integer 1 or 2 or 3:2
break;
TWO
case 3: printf("THREE\n");
break;
ผลลัพธ์
}
Enter integer 1 or 2 or 3:1
}
ONE
Department of Computer Engineering, PSU
41
242-101, 241-101 Introduction to Computer Programming
คาสังเงื
่ อ่ นไข
•
•
•
switch-case
default
เป็ นคาสงวน
default เป็ นอีกกรณีหนึง่ ในคาสัง่ switch-case
มักวางไว้เป็ นกรณีสุดท้าย
ในกรณีทีต่ รวจสอบแล้วพบว่า นิพจน์ มีค่าไม่ตรงกับ case
ใดๆเลยข้างต้น โปรแกรมจะเข้าไปทางานในส่วนของ
default
•
ไม่จาเป็ นต้องใส่ break หลังชุดคาสังของ
่
default
Department of Computer Engineering, PSU
42
242-101, 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)
ผลลัพธ์
{
case 1: printf("ONE\n");
Enter integer 1 or 2 or 3 4
Out of range
break;
case 2: printf("TWO\n");
break;
case 3: printf("THREE\n");
break;
default: printf("Out of range");
}
}
Department of Computer Engineering, PSU
43
242-101, 241-101 Introduction to Computer Programming
#include<stdio.h>
int main()
ผลลัพธ์
{
char grade;
printf("Enter your grade:Enter
"); your grade:
Very good
scanf("%c",&grade);
switch(grade)
----{ case ’a’:
Enter your grade:
No good!
case ’A’: printf(“Very Good\n");
break;
----case ’b’:
Enter your grade:
case ’B’: printf(“Good\n");
Fair
break;
----case ’c’:
Enter your grade:
case ’C’: printf(“Fair\n");
No good!
break;
default: printf(“No good!\n");
}
}
Department of Computer Engineering, PSU
a
F
C
3
44
242-101, 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;
}
Department of Computer Engineering, PSU
if(a==b)
คาสั่ ง_1;
else if (a==c)
คาสั่ ง_2;
else if (a==d)
คาสั่ ง_3;
else คาสั่ ง_4;
45
242-101, 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”);
Department of Computer Engineering, PSU
46
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)
{
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”);
Department of Computer Engineering, PSU
47
242-101, 241-101 Introduction to Computer Programming
โครงสร้างแบบทาซ้ า
•
ทาไมต้องทาซ้ า?
•
งานบางอย่าง อาศัยการทางานซ้ าๆกัน อาจเหมือนกันทุกครั้ง
หรือในการทาแต่ละครั้ง มีค่าบางอย่างเปลีย่ นไป อย่างมีรูปแบบ
•
ตัวอย่าง ต้องการแสดงข้อความว่า Hello จานวน 200 ข้อความ
printf(“Hello
printf(“Hello
printf(“Hello
printf(“Hello
printf(“Hello
……….
Department of Computer Engineering, PSU
");
");
");
");
");
200 ครั้ง
48
242-101, 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
Department of Computer Engineering, PSU
49
242-101, 241-101 Introduction to Computer Programming
โครงสร้างแบบทาซ้ า
•
•
•
เป็ นการสังให้
่ โปรแกรมทางานอย่างใดอย่างหนึง่ ซ้ าๆกัน
ตามจานวนรอบทีต่ อ้ งการ หรือ ตามเงือ่ นไขทีก่ าหนด
การวนรอบบางครั้งจะเรียกว่า loop หรือการวนลูป
กลุ่มคาสังการท
่
าซ้ า มีสามประเภท ได้แก่ for loop,
while loop และ do-while loop
Department of Computer Engineering, PSU
50
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;
for (i=1; i<=12; i++ )
{
printf(”2 x %d = %d \n”,i, 2*i);
Department of Computer Engineering, PSU
}
51
242-101, 241-101 Introduction to Computer Programming
การทาซ้ า (ขาขัน)
เด็ก: เสร็ จแล้วค่ะครู
ครู : ... พยายามดีเหลือเกิน (กะจะลองดีกับฉั นใช่ ไหม!?!)
เด็กฉลาด (แกมโกง)
ครูทาโทษให ้เด็กคัดบนกระดาน
“ฉั นจะไม่เล่นปาเครือ
่ งบินกระดาษในห ้องเรียนอีกแล ้ว” 500 จบ
ี จ
แต่เด็ก(หัวใส) เขียนเป็ นโค ้ดโปรแกรมภาษาซท
ี่ ะทาให ้ได ้ผลลัพธ์
เหมือนกัน
Department of Computer Engineering, PSU
52
242-101, 241-101 Introduction to Computer Programming
คาสังการท
่
าซ้ า
for
 หากเงือ่ นไขเป็ นจริง loop ก็จะยังคงทางานต่อไป จนกระทัง่
เงือ่ นไขเป็ นเท็จ ตัวอย่างเช่น
 การปรับค่าตัวแปร (ลด/เพิม่ ) อาจจะส่งผลให้มีเกิดทางานไม่รู ้
จบของ loop ได้ (infinite loop)
#include<stdio.h>
int main()
{
int i;
(i=0;i<5;i++)
for (i=0;i<5;i--)
printf("* \n");
}
Department of Computer Engineering, PSU
ผลลัพธ์
*
*
*
*
*
เกิดการวนรอบ
แบบไม่รูจ้ บ
53
242-101, 241-101 Introduction to Computer Programming
Flow chart การทางาน คาสังการท
่
าซ้ า for
การกาหนดค่ าเริ่มต้ นตัวแปร
ตรวจสอบเงือ่ นไข
จริง
เท็จ
คาสั่ ง
ปรับค่ าตัวแปร
Department of Computer Engineering, PSU
54
242-101, 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); }
Department of Computer Engineering, PSU
int i,n;
for (i=1; i<=n; i++)
{ printf(”%d \n”,i); }
55
242-101, 241-101 Introduction to Computer Programming
คาสังการท
่
าซ้ า
•
while
รูปแบบของ while loop คือ
(เงือ่ นไข)
คาสัง;่
while
while เป็ นคาสงวน
หากเงื่อนไขเป็ นจริ ง โปรแกรมจะ
ทางานตามคาสั่งซ้ า จนกระทัง่
เงื่อนไขเป็ นเท็จ
Department of Computer Engineering, PSU
ในขณะที่ เงือ่ นไข เป็ นจริง
ให้ ทาคาสั่ ง
หรือ ทาคาสั่ งนั้นจนกว่ า
เงือ่ นไขจะกลายเป็ นเท็จ
56
242-101, 241-101 Introduction to Computer Programming
Flowchart คาสัง่ while
ตรวจสอบเงือ่ นไข
การทาซ้า
จริง
เท็จ
คาสั่ ง
Department of Computer Engineering, PSU
57
242-101, 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++;
}
}
Department of Computer Engineering, PSU
58
242-101, 241-101 Introduction to Computer Programming
เปรียบเทียบโครงสร้ างของ for loop กับ while loop
for loop
for
(การกาหนดค่าเริม่ ต้นตัวแปร ; เงือ่ นไข ; ปรับค่าตัวแปร)
คาสัง;่
while loop
การกาหนดค่ าเริ่มต้ นตัวแปร;
while (เงื่อนไข){
คาสั่ ง;
ปรับค่ าตัวแปร;
}
Department of Computer Engineering, PSU
59
242-101, 241-101 Introduction to Computer Programming
คาสังการท
่
าซ้ า do-while
•
โครงสร้างแบบการทาซ้ า while และ for จะต้องมีการ
ตรวจสอบค่าของเงือ่ นไขก่อนว่าเป็ นจริงหรือเท็จ ก่อนทีจ่ ะ
ทาคาสัง่ (กลุ่มคาสัง)่ ภายในรอบ
•
ถ้ าต้ องทาคาสั่งภายในรอบก่อนอย่างน้ อย 1 ครั้ง แล้ วจึง
ตรวจสอบเงื่อนไข เมื่อจบรอบ ให้ ใช้ โครงสร้ าง do-while
Department of Computer Engineering, PSU
60
242-101, 241-101 Introduction to Computer Programming
โครงสร้าง do while loop
do
{
กลุ่มคาสัง;
่
}
while ( เงื่อนไข ) ;
มี ; เพื่อจบคาสัง่
do-while
Department of Computer Engineering, PSU
61
242-101, 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);
}
Department of Computer Engineering, PSU
#include<stdio.h>
int main()
{
int i=6;
do
{
printf("* \n");
i++;
}
while (i<12);
}
62
242-101, 241-101 Introduction to Computer Programming
เปรียบเทียบโครงสร้ าง do while กับ while loop
while loop
do loop
ตรวจสอบเงือ่ นไข
การทาซ้า
คาสั่ ง
จริง
จริง
คาสั่ ง
เท็จ
ตรวจสอบเงือ่ นไข
การทาซ้า
เท็จ
Department of Computer Engineering, PSU
63
242-101, 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
Department of Computer Engineering, PSU
64
242-101, 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
Department of Computer Engineering, PSU
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);
65
242-101, 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;
}
Department of Computer Engineering, PSU
*
**
***
****
*****
66
242-101, 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;
}
Department of Computer Engineering, PSU
*****
****
***
**
*
67
242-101, 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;
}
Department of Computer Engineering, PSU
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
68
242-101, 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;
} Department of Computer Engineering, PSU
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
---69
break และ continue
242-101, 241-101 Introduction to Computer Programming
• continue และ break เป็ นคาสังที
่ ใ่ ช้ในโครงสร้างลูป สามารถใช้ได้ท้ งั ใน
ลูป for, while และ do-while
• คาสัง่ break (นอกจากใช้ในโครงสร้าง switch-case แล้ว) ใช้เพือ่ เป็ น
ทางลัดในออกจากลูปได้ทนั ทีเมือ่ ต้องการ โดยไม่ตอ้ งรอจนเงือ่ นไขเป็ น
เท็จ
• คาสัง่ continue ใช้เมือ่ ต้องการจบการทางานรอบปั จจุบนั แล้วไปเริม่ ต้น
ทางานในรอบใหม่(ไปยังส่วนการตรวจสอบเงือ่ นไข) โดยจะไม่ทาทุก
คาสังที
่ เ่ หลือจนครบรอบตามปกติ
• continue ในลูป for จะไปยังส่วนของการปรับค่าตัวแปรก่อน แล้วจึ ง
ตรวจสอบเงือ่ นไขต่อไป
Department of Computer Engineering, PSU
70
242-101, 241-101 Introduction to Computer Programming
ตัวอย่างคาสัง่ break
do {
...
if (n>10) break;
// ให้ ออกจำกลูปได้ ทันทีที่ n>10
...
} while ( ); //infinite loop เพรำะไม่ มีเงื่อนไข
...
while (x>0)
{ ...
if (x>100) break; // ให้ ออกจำกลูปได้ ทันทีที่ x>100
...
}
...
for(n=0; n<100; n++)
{ ...
if (n > x) break; // หำก n>x ให้ ออกจำกลูปได้ ทันที ไม่ ต้องทำจนครบ 100 รอบ
...
}
Department of Computer Engineering, PSU
71
242-101, 241-101 Introduction to Computer Programming
ตัวอย่างคาสัง่ continue
...
while (x>0)
{ ...
if (y>100) continue;
// ถ้ า y>100 ให้ ไปเริ่ มต้ นลูปใหม่ ทันที ไม่ ต้องทาคาสั่ งที่ เหลือจนครบรอบ
printf(”y = %d\n”, y);
...
}
...
for(n=0; n<100; n++)
{ ...
if (n%10==0) continue;
// ถ้ า n หารสิ บลงตัวให้ ไปเริ่ มต้ นลูปรอบใหม่ ทันที (รอบถัดไปที่ n++)
...
}
Department of Computer Engineering, PSU
72
242-101, 241-101 Introduction to Computer Programming
ตัวอย่างโปรแกรม
...
int i;
for (i=1; i<=99; i++)
{
printf(”%02d”, i);
if(i%3==0) { printf(”#\n”); continue; }
printf(“xxx”);
if(i>10) break;
}
จากส่ วนของโปรแกรม ได้ผลลัพธ์ดงั นี้
01xxx02xxx03#
04xxx05xxx06#
07xxx08xxx09#
10xxx11xxx
Department of Computer Engineering, PSU
73
242-101, 241-101 Introduction to Computer Programming
ลูปซ้อนกัน Nested loop
- หากคาสั่ง break และ continue ปรากฏอยู่ในโครงสร้ างของลูป
ที่ซ้อนกันอยู่หลายชั้น คาสั่งนั้นมีผลกับลูปในสุดที่คร่อมมันอยู่เท่านั้น
(ไม่ส่งผลต่อลูปภายนอก)
...
do {
... break;
while (x>0)
{ ... break;
if (x>100) continue;
...
}
for(j=0; j<x; j++)
{ ...
if(x==50) break;
}
...
} while (n>0);
Department of Computer Engineering, PSU
หมายเหตุ คาสั่ง break ที่ใช้ใน
โครงสร้าง switch-case มี
ขอบเขตในโครงสร้างนั้นเท่านั้น
ไม่ส่งผลต่อลูปภายนอกใดๆ
74
242-101, 241-101 Introduction to Computer Programming
โจทย์แบบฝึ กหัด
1. จงเขียนโปรแกรมรับเลขจานวนเต็ม 5 จานวน แล้วหาค่าผลรวม และผลคูณ
ของทุกจานวน
(ควรใช้ loop ชนิดใด? for, while, do-while? )
2. จงเขียนโปรแกรมรับจานวนเต็มบวก เพื่อหาค่าที่นอ้ ยที่สุด และค่าที่มากสุ ด ที่
ผูใ้ ช้ใส่ เข้ามา โปรแกรมจะจบการรับข้อมูลเมื่อผูใ้ ช้ใส่ จานวนลบ จากนั้นจึง
แสดงค่าน้อยที่สุด และค่ามากที่สุด
3.
จงเขียนโปรแกรมรับจานวนเต็มบวก n ( n มีค่ามากกว่าหรื อเท่ากับ 2) แล้วหาผลรวมของ
ทุกจานวน(ตั้งแต่ 1 ถึง n-1) ที่หาร n ได้ลงตัวและแสดงผลรวมนั้น จากนั้นจึงวนรับค่า n
ค่าใหม่จนกว่าผูใ้ ช้จะใส่ ค่า n ที่นอ้ ยกว่า 2 จึงหยุด แต่หากว่า n เป็ นจานวนเฉพาะ (Prime
number) ให้แสดงว่า n เป็ น Prime number โดยไม่ตอ้ งแสดงผลรวมและให้จบการทางาน
ของโปรแกรมในทันที
(หมายเหตุ หากผลรวมของตัวหารของ n มีค่าเป็ น 1 แสดงว่ามีเฉพาะ 1 ที่หาร n ได้ลงตัว ดังนั้น n จึงเป็ นจานวน
เฉพาะ)
Department of Computer Engineering, PSU
75
242-101, 241-101 Introduction to Computer Programming
1. โปรแกรมรับจานวนเต็มแล้วหาผลรวมและผลคูณ
#include<stdio.h>
#define NUM 5
int main(){
int i,n, sum, product;
sum=0; product=1;
for(i=0;i<NUM;i++)
{ printf(”Enter a number: ”);
scanf(”%d”, &n);
sum += n; product *= n;
}
printf(”Sum = %d\n”, sum);
printf(”Product = %d\n”,product);
return 0;
}
Department of Computer Engineering, PSU
76
242-101, 241-101 Introduction to Computer Programming
2. โปรแกรมหาค่า min, max
#include<stdio.h>
int main()
{ int n,min,max;
printf(”Enter a number: ”);
scanf(”%d”, &n);
min=n; max=n;
while(n>=0)
{ if(n<min) min=n;
if(n>max) max=n;
printf(”Enter a number: ”);
scanf(”%d”, &n);
}
printf(”Min = %d\n”, min);
printf(”Max = %d\n”, max);
return 0;
}
Department of Computer Engineering, PSU
77
242-101, 241-101 Introduction to Computer Programming
3. ตัวอย่างผลการรันโปรแกรมหาค่า sum of divisors
Enter n : 25
1 5
Sum of divisors of n = 6
Enter n : 28
1 2 4 7 14
Sum of divisors of n = 28
Enter n = 0
End of program.
Enter n : 18
1 2 3 6 9
Sum of divisors of n = 21
Enter n : 33
1 3 11
Sum of divisors of n = 15
Enter n = -1
End of program.
Enter n : 40
1 2 4 5 8 10 20
Sum of divisors of n = 50
Enter n : 17
1
17 is prime number
End of program.
Enter n : 97
1
97 is prime number
End of program.
Department of Computer Engineering, PSU
78
242-101, 241-101 Introduction to Computer Programming
3.
โปรแกรมหาค่
า
sum
of
divisors
#include<stdio.h>
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