Transcript PPT

Week 4
การเขียนโปรแกรมแบบมีเงื่อนไข
If and If-Else Statements
Outline
• Flowchart ผังงานในการเขียนโปรแกรม
•
การเขียนโปรแกรมแบบมีทางเลือก และรู ปแบบเงื่อนไข
•
การเขียนโปรแกรมโดยใช้ If-Else Statements
•
การเขียนโปรแกรมโดยใช้ Switch Statement
2
Flowchart
• Flowchart ถือเป็ นเครื่ องมือที่ช่วยในการอธิ บายความคิดและแสดง
ตรรกะของผูเ้ ขียนโปรแกรมต่อผูอ้ ่านโปรแกรม
• Flowchart มีลก
ั ษณะเป็ นแผนภาพเพื่อแสดงการทางานของโปรแกรม
แบบเป็ นลาดับ (Sequence), แบบแสดงทางเลือก (Selection), หรื อ
แบบทาซ้ า (Iteration) โดยอาศัยสัญลักษณ์มาตรฐาน (Symbols)
–
การเขียน Flowchart เปรี ยบเสมือนการเขียนแบบแปลนของตึกอาคาร ซึ่ ง
โดยทัว่ ไป ผูเ้ ขียนโปรแกรมควรที่จะเขียน Flowchart ก่อนลงมือเขียน
โปรแกรม โดยเฉพาะ ในกรณี ทีมีการเขียนโปรแกรมขนาดใหญ่ หรื อมี
ผูเ้ ขียนโปรแกรมหลายคน เป็ นต้น
3
การเขียน Flowchart
สัญลักษณ์มาตรฐานสาหรับการเขียน Flowchart มีดงั นี้
Terminal symbol (จุดเริ่ มต้น/สิ้ นสุ ดของ
โปรแกรม)
Process symbol (ใช้แทนคาสัง่ ทัว่ ไป
เช่น การ
คานวณ หรื อ การกาหนดค่า เป็ นต้น)
Input/output symbol (ใช้แทนคาสัง่ ที่มีการติตต่อ
input/output)
Decision symbol (แสดงคาสัง่ ที่ตอ
้ งการให้ตดั สิ นใจ เช่น if ที่ปลาย
ด้านซ้ายและขวาจะมีลกู ศรออกมาและเขียนข้างเส้นลูกศรว่า Yes/No
หรื อ True/False ที่ปลายลูกศรจะเชื่อมต่อกับคาสัง่ ที่เป็ นทางเลือก
ของการตัดสิ นใจ)
4
การเขียน Flowchart (ต่ อ)
Connector symbol (แสดงจุดเชื่อมของโปรแกรม)
A
A
Link symbol (แสดงการเชื่อมต่อระหว่างส่ วนของโปรแกรม
เช่น
กรณี ใช้หลายหน้ากระดาษเพื่อแสดง Flowchart)
Function symbol (แสดงส่ วนของ Function ที่เรี ยกใช้ โดย
รายละเอียดของ Function จะถูกแสดงที่อื่น)
Flow line (ทิศทางของลูกศรใช้แสดงลาดับการทางานของ Flowchart
ซึ่งจะเรี ยงลาดับจากบนลงล่าง)
ลูกศรชี้จากล่างขึ้นบนแสดงการทาซ้ าหรื อวนลูป
5
ตัวอย่ าง 1 : Flowchart
•
ตัวอย่าง Flowchart แสดงถึงการ
ตัดสิ นใจการนารถยนต์เข้า
ศูนย์บริ การ โดยกระบวนการเริ่ ม
จาก การตรวจสอบข้อมูลอายุ
รถยนต์(Age) และระยะเดินทางที่
รถวิ่ง (Mileage) จากนั้น พิจารณา
ว่ารถมีอายุมากกว่า 2 ปี หรื อ มี
ระยะเดินทางมากกว่า 200,000
กม. หรื อไม่ หากอย่างใดอย่างหนึ่ ง
เป็ นจริ ง เจ้าของรถจะตัดสิ นใจนา
รถเข้าศูนย์ฯ หากไม่เป็ นจริ งทั้ง 2
อย่าง เจ้าของรถจะไม่นารถเข้า
ศูนย์ฯ และหลังจากนั้น ไม่วา่ เลือก
ทางใด ถือเป็ นอันสิ้ นสุ ด
กระบวนการ
Get car's
Age
START
YES
Perform car service
Get car's
Mileage
IF
Age > 2 OR
Mileage > 200,000
NO
STOP
Not perform car service
6
การเขียนโปรแกรมแบบมีทางเลือก (Selection)
•
•
•
โดยปกติ Statement ในโปรแกรมจะทางานในแบบ Sequential flow
of control นัน
่ คือ ทาคาสัง่ ที่ 1 ตามด้วย 2, 3 และ 4 ไปเรื่ อยๆ
แต่บางครั้งการเขียนโปรแกรมจาเป็ นต้องมีทางเลือก (Selection) เช่น
ในตัวอย่าง 1 โดยอาศัยเงื่อนไข (Condition) ในการพิจารณาการกระทา
เงื่อนไขจะถูกแสดงด้วยรู ปแบบ (expression) ซึ่งแบ่งเป็ น 2 ลักษณะ
ได้แก่
1. Relational expression
2. Logical expression
7
Relational Expression
•
คือรู ปแบบที่ใช้ประเมิน (Evaluate) ความสัมพันธ์วา่ สอดคล้องตามตัว
กระทา หรื อ Relational operator ที่กาหนดหรื อไม่
• Relational operators:
Relational
operator
ความหมาย
<
น้อยกว่า
มากกว่า
น้อยกว่าหรื อเท่ากับ
มากกว่าหรื อเท่ากับ
เท่ากับ
ไม่เท่ากับ
>
<=
>=
==
!=
•
ตัวอย่าง :
 a<3
 a<b
 -1.3 >= (2.0*x + 3)
8
Relational Expression (ต่ อ)
•
ตัวอย่างการใช้ที่ไม่ถกู ต้อง:
a
a
a
a
a
=< b
< = b
>> b
= b
= = b-1
//out of order
//space is not allowed
//refers shift expression
//assignment statement
//space is not allowed
• Note: โดยปกติตวั เลขที่ใช้ได้ในเครื่ อง (machine) ไม่มี infinite
precision ดังนั้นอาจจะได้คาตอบที่ไม่คาดหวังได้เช่น x < x+y ซึ่ ง
เทียบเท่ากับ 0 < y ในทางการคณิ ตศาสตร์ (mathematically) ถ้า y เป็ น
เลขบวก (positive) ความสัมพันธ์น้ ีจะเป็ นจริ ง แต่ในทางการคานวณ
(computationally) ถ้า x เป็ นตัวแปรชนิ ด float ที่มีค่ามากเช่น 7e+33
และ y เป็ นตัวแปรชนิด float ที่มีค่าน้อยเช่น 0.0001 อาจจะทาให้ x <
x+y เป็ นเท็จได้
9
Logical Expression
•
คือรู ปแบบที่ใช้ประเมิน (Evaluate) ความสัมพันธ์ทางตรรกะ (Logic)
• Logical operators:
Logical operator
ความหมาย
!
นิเสธ (NOT, negation)
&&
Logic AND
||
Logic OR
10
Logical Expression (ต่ อ)
•
ตัวอย่างตารางแสดงการประเมินค่าของ Logic AND และ Logic OR
การประเมินของ expression
exp1
exp2
exp1 && exp2
exp1 || exp2
False
False
False
False
False
True
False
True
True
False
False
True
True
True
True
True
11
Logical Expression (ต่ อ)
•
การประเมินค่า expression จะอาศัยวิธีการเรี ยกว่า “short-circuit
evaluation” นัน
่ คือประเมินค่า expression จะหยุดทันทีที่รู้วา่ ผลลัพธ์เป็ น
จริ งหรื อเท็จ เช่น
- ในกรณี expr1 && expr2 ถ้า expr1 มีค่าเป็ น False แล้ว การประเมิน
ค่า expr2 จะไม่เกิดเนื่องจาก compiler รู ้ทนั ทีวา่ && จะให้ผลลัพธ์
เป็ น False
- ในกรณี expr1 || expr2 ถ้า expr1 มีค่าเป็ น True แล้ว การประเมินค่า
expr2 จะไม่เกิดเนื่ องจาก compiler รู ้ทน
ั ทีวา่ || จะให้ผลลัพธ์เป็ น
True
12
การเขียนโปรแกรมโดยใช้ If Statement
Entry
if (condition)
statement;
• If statement อย่างง่ายที่สุดจะประเมิน
Expr
True
statement
False
Expression ที่แสดงเงื่อนไข
(Condition) ซึ่ งถ้าเงื่อนไขเป็ นจริ ง
(True) จะทางานตาม statement แต่ถา้
เป็ นเท็จ statement จะถูกข้ามและ
control จะถูกส่ งไปยัง statement ถัดไป
Exit
If-Single Statement Flowchart
13
•
ตัวอย่ าง 2 : โปรแกรมที่ใช้ If-Statement
โปรแกรมคานวณหาค่าสัมบูรณ์ (Absolute value) ของข้อมูลแบบ
คาของจ
านวนจริงทีไ่ มค
่ งหมาย
่
่ านึงถึงเครือ
จานวนเต็มชนิด integer
#include <stdio.h>
int main()
{
int number;
printf("type in your number: ");
scanf("%d", &number);
if ( number < 0 )
number = -number;
printf("\nThe absolute value is %d\n", number);
return 0;
}
คาสั
-3 หรือ +3 คือ 143,
่ มบูรณของ
์
การเขียนโปรแกรมโดยใช้ If Compound Statement
Entry
if (condition) {
statement1;
statement2;
...
}
•
ถ้ามี statement มากกว่าหนึ่ง statement
(หรื อเรี ยกว่า compound statement)
โปรแกรมจะทางานในทุก ๆ statement
ดังแสดงใน Flowchart
Expr
True
statement1
False
statement2
Exit
If-multiple statement Flowchart
15
•
ตัวอย่ าง 3 : โปรแกรมทีใ่ ช้ If Compound Statement
จากตัวอย่างที่ 2 เราสามารถแก้ไขส่ วนของโปรแกรม (if-statement) เพื่อ
คานวณค่าสัมบูรณ์ (Absolute value) ของข้อมูล โดยหากข้อมูลมีค่าน้อยกว่า
ศูนย์ให้คานวณค่าสัมบูรณ์และเพิ่มค่าดังกล่าวไปเท่ากับ 10 โดยสามารถแก้ไข
ได้ดงั นี้
...
if ( number < 0 ) {
number = -number;
number = number + 10;
}
printf("\nThe absolute value is %d\n", number);
...
16
การเขียนโปรแกรมโดยใช้ If-Else Statement
Entry
if (condition) {
statement1;
statement2;
…
}
else {
statement3;
statement4;
…
}
statement3
False
Expr
True
statement4
statement1
statement2
Exit
If-Else Statement Flowchart
• If-else statement ใช้ในกรณี ที่มี statements อยูใ่ นทั้ง 2 ทางเลือก และจะ
เลือกทางานตาม Flowchart โดยประเมินจาก Condition ว่าจริ งหรื อเท็จ
17
ตัวอย่าง 4 : โปรแกรมทีใ่ ช้ If-Else Compound Statement
•
โปรแกรมคานวณการแปลงอุณหภูมิในแบบเซลเซี ยสและแบบฟาเรนไฮท์
#include <stdio.h>
void main()
{
char type;
float temp, fahren, celcius;
printf("enter temperature: ");
scanf("%f", &temp);
printf("enter f(farenheit) or c(celcius) for temperature unit: ");
scanf("\n%c", &type);
if ( type == 'f') {
celcius = (5.0/9.0) *(temp -32.0);
printf("\nThe equivalent Celcius temp. is %6.2f\n", celcius);
}
else {
fahren = (9.0/5.0)*(temp) + 32.0;
printf("\nThe equivalent Farenheit temp. is %6.2f\n", fahren);
}
}
18
•
การเขียนโปรแกรมโดยใช้ If-Else-If Statement
If-else-if statement มีไว้สาหรับกรณี มี statement มากกว่า 1
statement ถูกผูกเข้าด้วยกันโดย else ซึ่ งเป็ นทางเลือกมากกว่า 2
ทางเลือก
Entry
if (condition1)
statement1;
else if (condition2)
statement2;
else if (condition3)
statement3;
else
default;
False
Expr1
True
statement1
False
Expr2
False
Expr3
True
True
statement2
statement3
Default
Exit
19
ตัวอย่ าง 5 : โปรแกรมที่ใช้ If-Else-If Compound Statement
•
โปรแกรมแสดง
ข้อความตามชั้นปี
ของนักศึกษา เช่น
–
#include <stdio.h>
int main()
{
int year;
ชั้นปี 1 พิมพ์วา่
printf("enter student year:");
scanf("%d" ,&year);
“Freshman”
–
ชั้นปี 2 พิมพ์วา่
if ( year == 1)
printf("Freshman");
else if ( year == 2)
printf("Sophomore");
else if ( year == 3)
printf("Junior");
else if ( year == 4)
printf("Senior");
else
printf("Super");
return 0;
“Sophomore”
–
ชั้นปี 3 พิมพ์วา่
“Junior”
–
ชั้นปี 4 พิมพ์วา่
“Senior”
–
ชั้นปี อื่นๆ พิมพ์
ว่า “Super”
}
20
Nested If Statement
• Nested if statement เป็ นการเขียนโปรแกรมในลักษณะมีการซ้อนกันของ if
statement เพื่อแสดงการกระทาแบบเงื่อนไขซ้อนเป็ นชั้นๆ ดังเช่นในตัวอย่าง
นี้
Entry
if (condition1)
statement1;
if (condition 2)
statement2;
else
statement3;
else
statement4;
True
Expr1
False
statement1
Expr2
False
statement4
True
statement2
statement3
If ที่ซ้อนอยู่ด้านใน
Exit
21
ตัวอย่ าง 6 : โปรแกรมที่ใช้ Nested If Compound Statement
•
ตัวอย่างโปรแกรมเพื่อรับค่าเลขจานวนเต็ม 2 ตัว โดยตรวจสอบว่าเลขตัวที่ 1 เป็ นเลขในช่วง 09 และเลขตัวที่ 2 เป็ นเลขในช่วง 10-99 หรื อไม่และแสดงผลการตรวจสอบดังกล่าวที่หน้อจอ
#include <stdio.h>
void main()
{
int num1, num2;
printf("program to check num1 (0-9) and num2 (10-99)\n");
printf("enter num1 num2:");
scanf("%d %d" ,&num1,&num2);
if (num1 >=0 && num1 <= 9)
if (num2 >= 10 && num2 <= 99)
printf("Both numbers are entered correctly");
else
printf("only num1 is entered correctly");
else
if (num2 >= 10 && num2 <= 99)
printf("only num2 is entered correctly");
else
printf("Both numbers are not entered correctly");
}
22
การเขียนโปรแกรมโดยใช้ Switch Statement
• switch statement มีไว้สาหรับกรณี มีทางเลือกมากกว่า 1 ทางได้
เหมือนกับกรณี ของ if-else-if statement โดยมี syntax ดังแสดง
switch (expression)
{
case const1Expr:
statement11;
statement12;
…
break;
case const2Expr:
statement21;
statement22;
…
break;
…
default:
defaultstatement1;
defaultstatement2;
…
}
•
สังเกตว่าจะมี break statement ใน
ทุกๆ cases (ยกเว้นกรณี สุดท้ายคือ
default) เพื่อทาการหยุดและออก
จาก switch statement
23
•
การเขียนโปรแกรมโดยใช้ Switch Statement (ต่ อ)
ตัวอย่างการใช้ switch statement ในกรณี มีบาง case ที่มี statement
เหมือนกัน
switch (i) {
case 1: case 2: case 3:
flag = 0;
break;
case 4:
flag = 1;
break;
default:
process(h);
}
24
ตัวอย่ าง 7 : โปรแกรมที่ใช้ Switch Statement
•
โปรแกรมแสดง
ข้อความตามชั้นปี
ของนักศึกษา จาก
ตัวอย่างที่ 5 ซึ่ ง
เขียนโปรแกรมโดย
ใช้ Switch
Statement แทน
#include <stdio.h>
void main()
{
int year;
printf("enter student year:");
scanf("%d" ,&year);
switch (year) {
case 1:
printf("Freshman");
break;
case 2:
printf("Sophomore");
break;
case 3:
printf("Junior");
break;
case 4:
printf("Senior");
break;
default:
printf("Super");
}
}
25