งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Computer Programming การเขียนโปรแกรมคอมพิวเตอร์"— ใบสำเนางานนำเสนอ:

1 Computer Programming การเขียนโปรแกรมคอมพิวเตอร์
สัปดาห์ที่ 5 คำสั่งควบคุมการทำงานแบบเงื่อนไข

2 objectives รู้จักและเข้าใจคำสั่งควบคุมการทำงานแบบมีเงื่อนไขในภาษาซี
สามารถเขียนโปรแกรมด้วยคำสั่งควบคุมการทำงานแบบมีเงื่อนไขได้ สามารถนำความรู้เรื่องคำสั่งควบคุมการทำงานแบบมีเงื่อนไขไปประยุกต์เพื่อแก้ไขโจทย์ปัญหาในชีวิตประจำวันได้ได้อย่างถูกต้อง ให้นิสิตได้ฝึกฝนและเตรียมตัวเพื่อเรียนรู้คำสั่งแบบวนซ้ำในเนื้อหาบทถัดไป

3 Outline 1 Review Assignment 2 Scanf Function 3 Printf Function 4
The Other Function 5 Assignment #3

4 C Programming Structure
พรีโปรเซสเซอร์ไดเร็คทีฟ #include<file.h> type function_name(type); type variable; int main() { statement-1; ... statement-n; return 0; } type function_name(type variable) return(var); ฟังก์ชันโพรโทรไทพ์ ส่วนหัวโปรแกรม ตัวแปรชนิดโกบอล ตัวแปรชนิดโลคอล ฟังก์ชันหลัก คำสั่ง ส่วนตัวโปรแกรม ฟังก์ชันย่อย

5 Examples of Relational Operators
การเปรียบเทียบ ผลที่ได้ 7 == 9 False 7 != 9 True 8 > 8 8 >= 8 (10+9)<7 4 <= 3 การเปรียบเทียบ ผลที่ได้ 22 == 22 True (3+5)!=8 False 9 > 7 7 >= 9 7<(10+9) 3 <= 4 ไม่ควรใช้เครื่องหมายเท่ากับ == หรือไม่เท่ากับ != สำหรับข้อมูลทศนิยม

6 Logical Operators การดำเนินการ ผลที่ได้ การดำเนินการ ผลที่ได้
T && T T T && F F F && T F && F การดำเนินการ ผลที่ได้ T || T T T || F F || T F || F F การดำเนินการ ผลที่ได้ !T F !F T

7 Condition Control Statement (if)
Outline 1 Review Assignment 2 p Condition Control Statement (if) 3 Condition Control Statement (if-else) 4 Condition Control Statement (if-else-if) 5 Assignment #4

8 Condition Control Statement
คำสั่งควบคุมการทำงานแบบเงื่อนไข คือ การเขียนโปรแกรมให้มีการตัดสินใจสามารถเลือกได้ว่าจะทำหรือไม่ทำตามคำสั่ง โดยขึ้นอยู่กับเงื่อนไขที่กำหนด ผลจากการเปรียบเทียบเงื่อนไขจะให้ผลลัพธ์เป็นจริงหรือเท็จ ถ้าผลเป็นจริงให้ทำงานตามคำสั่งด้านที่เงื่อนไขเป็นจริง ถ้าเป็นเท็จให้ทำตาม คำสั่งด้านที่เงื่อนไขเป็นเท็จ ซึ่งคำสั่งควบคุมการทำงานแบบเงื่อนไขออกเป็น 4 ประเภท คือ if condition control statement if-else condition control statement if-else-if condition control statement Nested if condition control statement Switch condition control statement

9 if Condition Control Statement (1)
True False if (Boolean expression) statement; if (expression) statement; if (Condition) statement;

10 if Condition Control Statement (2)
True False Statement-2; if (Boolean expression) { statement-1; statement-2; ... statement-n; }

11 if Condition Control Statement (3)
IF (Condition) THEN BEGIN Statement 1 ; Statement 2 ; END;

12 if Condition Control Statement (4)
ในการเปรียบเทียบเงื่อนไขแบบ IF – THEN นิยมให้ทำงานตามคำสั่งเมื่อเงื่อนไขมีค่าเป็นจริง ไม่นิยมให้ทำงามตามคำสั่งเป็นเท็จ Condition Statement-1; Statement T F Statement-2; Condition Statement-1; 1 Statement-2; Statement;

13 if and Boolean Expression Condition
int a = 5 ; int b = 4 ; if (a > b) { statement 1; statement 2; statement n; } printf(); a > b True False statement 1; statement 2; statement 3;

14 If –THEN Example 1 #include <stdio.h> void main() { int age;
printf("How old are you? : "); scanf("%d",&age); if(age < 18) printf("You are young\n"); printf("You are %d years old.\n", age); }

15 If –THEN Example 2 #include <stdio.h> int pass = 1234;
int guess; void main() { printf("Enter your password : "); scanf("%d",&guess); if(guess == pass) printf(“**Right**"); }

16 if Condition Control Example (1)
จงเขียนผังงานและโปรแกรมรับชื่อนามสกุล รหัสนักศึกษา คะแนนสอบรวม และคะแนนเต็ม หากนักศึกษาสอบได้มากกว่า 60% ให้แสดงผลชื่อ นามสกุล รหัสนักศึกษา คะแนน และผลสอบว่าผ่าน Input Analysis ชื่อ / นามสกุล / รหัสนักศึกษา / คะแนนสอบ / คะแนนเต็ม Output Analysis แสดงผลชื่อ-สกุล รหัสนักศึกษา คะแนนสอบ ผลสอบ Process Analysis โปรแกรมรอรับชื่อ / นามสกุล / รหัสนักศึกษา / คะแนนสอบ / คะแนนเต็ม ตรวจสอบว่าคะแนนมากกว่าหรือเท่ากับ 60 % หรือไม่ ถ้าจริง แสดงผลชื่อ-สกุล รหัสนักศึกษา คะแนน และแสดงว่าสอบผ่าน

17 if Condition Control Example (2)
Variable Define first : ตัวแปรชนิดข้อความสำหรับเก็บชื่อขนาด 20 last : ตัวแปรชนิดข้อความสำหรับเก็บนามสกุลขนาด 20 id : ตัวแปรชนิดข้อความสำหรับเก็บรหัสนักศึกษาขนาด 9 point : ตัวแปรชนิดจำนวนทศนิยมสำหรับเก็บคะแนนรวม full : ตัวแปรชนิดจำนวนทศนิยมสำหรับเก็บคะแนนเต็ม

18 Process Description 4. แสดงผล คะแนนสอบและคะแนนเต็ม 6. จบการทำงาน
1. เริ่มต้นการทำงาน 2. รับค่า ชื่อ / นามสกุล / รหัสนักศึกษา / คะแนนสอบ / คะแนนเต็ม 3. แสดงผล ชื่อ / นามสกุล / รหัสนักศึกษา 4. แสดงผล คะแนนสอบและคะแนนเต็ม 5. ตัดสินใจเพื่อทำคำสั่งควบคุมการทำงานแบบมีเงื่อนไข โดยถ้านักศึกษาสอบได้มากกว่า 60% แล้วให้แสดง ชื่อ / นามสกุล / รหัสนักศึกษา / คะแนนสอบ และผลสอบ 6. จบการทำงาน

19 Pseudo Code 4. Show Point / Full Point
1. Begin 2. Read Name / Surname / ID / Point / Full Point 3. Show Name / Surname / ID 4. Show Point / Full Point 5. if (Point > = 60 %) Show Name / Surname / ID / Point / Passed 6. End

20 if Condition Control Example (3)
1 START point/full >= 0.6 first, last, id, point, full False first, last, id True "passed" point, full 1 END

21 if Condition Control Example (4)
#include<stdio.h> int main() { char first[20], last[20], id[9]; float point,full; printf ("Enter your Name : "); scanf ("%s",first); printf ("Enter your Surname : "); scanf ("%s",last); printf ("Enter your ID : "); scanf ("%s",id);

22 printf ("Enter your examination points : ");
scanf ("%f",&point); printf ("Enter your total points : "); scanf ("%f",&full); if ((point/full) >= 0.6) { printf ("Name : %s %s\n",first,last); printf ("ID : %s\n",id); printf ("Examination points : %f / %f\n",point,full); printf ("You passed, Congratulation\n"); } return 0;

23 Condition Control Statement (if)
Outline 1 Review Assignment 2 p Condition Control Statement (if) 3 Condition Control Statement (if-else) 4 Condition Control Statement (if-else-if) 5 Assignment #4

24 if-else Condition Control Statement (1)
if (Boolean expression) statement-1; else statement-2; การทำงานขึ้นอยู่กับเงื่อนไข ถ้าเป็นจริงไปทำงานด้านหนึ่ง ถ้าเป็นเท็จก็จะไปทำงานอีกอย่างหนึ่ง

25 If-else Condition Control Statement (2)
……… T F Statement-2.1; Statement-2.2; if (expression) { statement-1.1; statement-1.2; ... statement-1.n; } else statement-2.1; statement-2.2; statement-2.n;

26 If-else Condition Control Statement (3)
ถ้า sex =1 เงื่อนไขเป็นจริง จะเพิ่มค่าตัวแปร Male ขึ้นอีก 1 ถ้า sex !=1 เงื่อนไขเป็นเท็จ จะเพิ่มค่าตัวแปร Female ขึ้นอีก 1 False Sex = 1 True Female = Female + 1 Male = Male +1

27 If-else Condition Control Statement (4)
เริ่มต้น START STOP read age age > 60 “You are old” True False “You are young” รับค่าใส่ตัวแปร age เปรีบเทียบค่า age มากกว่า 60 ทำตามคำสั่งของทางเลือก ให้ตรงกับเงื่อนไข จบการทำงาน

28 If-else Condition Control Statement (5)
เริ่ม เริ่มการทำงาน พิมพ์ Do you like Pascal แสดงข้อความ Do you like Pascal รับค่า ans รับค่าใส่ตัวแปร answer False True ans = ‘y’ OR ans=‘Y’ เปรียบเทียบค่า ans เท่ากับ y หรือ Y - ถ้าเป็นจริง พิมพ์ Thank you ถ้าเป็นเท็จ พิมพ์ I’m sorry to hear that พิมพ์ I’m sorry to hear that พิมพ์ Thank You พิมพ์ Good Bye พิมพ์คำว่า Goodbye จบการทำงาน จบ

29 if –else Condition Control Example (1)
จงเขียนผังงานและโปรแกรมหารเลข 2 จำนวน โดยโปรแกรมต้องตรวจสอบได้ว่าตัวหารเป็น "0" หรือไม่ จงวิเคราะห์โจทย์ เขียนขั้นตอนการทำงานอย่างละเอียด เขียนรหัสเทียม เขียน Flowchart

30 if –else Condition Control Example (2)
START 1 num1, num2 num2 != 0 False num1 "Error" True num1/num2 num2 1 END

31 if –else Condition Control Example (3)
#include<stdio.h> int main() { float num1,num2; printf ("Enter number 1 : "); scanf ("%f",&num1); printf ("Enter number 2 : "); scanf ("%f",&num2); if (num2 != 0) printf ("%.2f / %.2f = %.2f",num1,num2,num1/num2); else printf ("Error divided by zero\n"); return 0; }

32 Condition Control Statement (if)
Outline 1 Review Assignment 2 p Condition Control Statement (if) 3 Condition Control Statement (if-else) 4 Condition Control Statement (if-else-if) 5 Assignment #4

33 if-else-if condition control statement (1)
if (expression-1) statement-1; else if (expression-2) statement-2; ... else if (expression-m) statement-m; else statement-m+1;

34 if-else-if condition control statement (2)
if (expression-1) { statement-1.1; ... statement-1.n; } else if (expression-2) statement-2.1; statement-2.n; ... else if (expression-m) { statement-m.1; statement-m.n; } else statement-m+1.1; statement-m+1.n;

35 if-else-if condition control statement (3)
เป็นรูปแบบการเลือกการทำงานที่มีทางเลือกมากกว่า 2 ทางขึ้นไป Condition กรณี 1 กรณี 2 กรณี 3 กรณี 4 Statement 1; Statement 2; Statement 3; Statement 4;

36 if-else-if condition control statement (4)
Score = mid + final score 70..79 60..69 50..59 0..49 Grade = ‘A’ Grade = ‘B’ Grade = ‘C’ Grade = ‘D’ Grade = ‘F’

37 if-else-if condition control Example (1)
จงเขียนผังงานและโปรแกรมสำหรับรับ ชื่อ นามสกุล รหัสนักศึกษา และคะแนนวิชา Computers and Programming เพื่อตรวจสอบว่านักศึกษาได้เกรดระดับใด โดยใช้เกณฑ์ดังนี้ คะแนน 90 – 100 ได้เกรด A คะแนน 80 – ได้เกรด B คะแนน 70 – ได้เกรด C คะแนน 60 – ได้เกรด D คะแนน 0 – ได้เกรด F แล้วแสดงผลลัพธ์ ชื่อ สกุล รหัสนักศึกษา คะแนน และเกรด

38 if-else-if condition control Example (2)
Output Analysis แสดงชื่อ นามสกุล รหัสนักศึกษา คะแนน และเกรดที่ได้ Input Analysis ชื่อ / นามสกุล / รหัสนักศึกษา / คะแนน Process Analysis โปรแกรมรอรับชื่อ / นามสกุล / รหัสนักศึกษา / คะแนนสอบ แสดงผลชื่อ-สกุล รหัสนักศึกษา และคะแนน

39 if-else-if condition control Example (3)
Process Analysis (ต่อ) การตรวจสอบคะแนนสอบ ถ้ามากกว่าหรือเท่ากับ 90 แสดงผลว่าได้เกรด A ถ้ามากกว่าหรือเท่ากับ 80 แสดงผลว่าได้เกรด B ถ้ามากกว่าหรือเท่ากับ 70 แสดงผลว่าได้เกรด C ถ้ามากกว่าหรือเท่ากับ 60 แสดงผลว่าได้เกรด D ถ้าไม่ตรงเงื่อนไขที่ผ่านมาทั้งหมด แสดงผลว่าได้เกรด F

40 if-else-if condition control Example (4)
Variable Define first : ตัวแปรชนิดข้อความสำหรับเก็บชื่อขนาด 20 last : ตัวแปรชนิดข้อความสำหรับเก็บนามสกุลขนาด 20 id : ตัวแปรชนิดข้อความสำหรับเก็บรหัสนักศึกษาขนาด 9 point : ตัวแปรชนิดจำนวนเต็มสำหรับเก็บคะแนน

41 if-else-if condition control Example (5)
first, last, id, point first, last, id, point "Grade A" START a b True False Point >= 90

42 if-else-if condition control Example (6)
"Grade C" True "Grade D" False END "Grade F" Point >= 60 Point >= 70 Point >= 80 "Grade B" a b

43 if-else-if condition control Example (6)
จงเขียนโค้ดโปรแกรมสำหรับรับ ชื่อ นามสกุล รหัสนักศึกษา และคะแนนวิชา Computers and Programming เพื่อตรวจสอบว่านักศึกษาได้เกรดระดับใด โดยใช้เกณฑ์ดังนี้ คะแนน 90 – 100 ได้เกรด A คะแนน 80 – ได้เกรด B คะแนน 70 – ได้เกรด C คะแนน 60 – ได้เกรด D คะแนน 0 – ได้เกรด F แล้วแสดงผลลัพธ์ ชื่อ สกุล รหัสนักศึกษา คะแนน และเกรด

44 Nested if Condition Control Statement (1)
if (expression-1) statement-1; else if (expression-2) statement-2; if (expression-3) statement-3; statement-4;

45 statements to be executed if test expression is true;
else if(test expression 1) statements to be executed if test expressions 1 is true; if (test expression 2) . statements to be executed if all test expressions are false;

46 Nested if Condition Control Statement (2)
True False Condition 2 Statement 2; Condition 3 Statement 3; Statement 4; เป็นรูปแบบการเลือกการทำงานที่มีทางเลือกมากกว่า 2 ทางขึ้นไป

47 Nested if Condition Control Statement (3)
Score = mid + final False Score >=80 True False Score >=70 True Grade = ‘A’ False Score >=60 True Grade = ‘B’ False Score >=50 True Grade = ‘C’ Grade = ‘F’ Grade = ‘D’

48 Nested if Condition Control Example (1)
#include <stdio.h> int main() { int numb1, numb2; printf("Enter two integers to check".\n); scanf("%d %d",&numb1,&numb2); if(numb1==numb2) //checking whether two integers are equal. printf("Result: %d=%d",numb1,numb2); else if(numb1>numb2) //checking whether numb1 is greater than numb2. printf("Result: %d>%d",numb1,numb2); printf("Result: %d>%d",numb2,numb1); return 0; }

49 Nested if Condition Control Example (2)

50 Switch Condition Control Statement (1)
switch (expression-1) { case constant-expr-1: statement-1.1; statement-1.2; ... statement-1.n; break; case constant-expr-2: statement-2.1; statement-2.2; statement-2.n; ... case constant-expr-m: statement-m.1; statement-m.2; statement-m.n; break; default: statement-d.1; statement-d.2; statement-d.n; }

51 Switch Condition Control Statement (2)
กรณี 1 กรณี 2 กรณี 3 Default Statement 1-1; Statement 1-2; …. Statement 1-n; Statement 2-1; Statement 2-2; …. Statement 2-n; Statement 3-1; Statement 3-2; …. Statement 3-n; Statement m-1; Statement m-2; …. Statement m-n;

52 Example of Switch Control Statement(1)
#include<stdio.h> int main() { int d; printf("Enter a number from 1 to 9: "); scanf("%d", &d); switch (d) case 1: puts("A stitch in time saves nine."); break; case 2: case 6: case 9: puts("Handsome is as handsome does."); default: puts("Very clever. Try again."); } return 0;

53 Example of Switch Control Statement(2)
#include<stdio.h> int main() { int d; printf("Enter a number from 1 to 9: "); scanf("%d", &d); switch (d) case 1: puts("A stitch in time saves nine."); break; case 2: break; case 6: break; case 9: puts("Handsome is as handsome does."); default: puts("Very clever. Try again."); } return 0;

54 Example of Switch Control Statement(3)
#include <stdio.h> int main() { int color = 1; printf("Please choose a color(1: red,2: green,3: blue):\n"); scanf("%d", &color); switch (color) { case 1: printf("you chose red color\n"); break; case 2: printf("you chose green color\n"); break; case 3: printf("you chose blue color\n"); break; default: printf("you did not choose any color\n"); } return 0; }

55 Condition Control Statement (if)
Outline 1 Review Assignment 2 p Condition Control Statement (if) 3 Condition Control Statement (if-else) 4 Condition Control Statement (if-else-if) 5 Assignment #4

56 Assignment (1) จงเขียนผังงาน และโปรแกรมรับค่า GPA ของภาคการศึกษาที่ผ่านมาแล้วแสดงผลสถานะการเรียน โดยมีเงื่อนไขดังนี้ GPA(เดิม) < 1.00 แสดงสถานะ Retried (พ้นสภาพ) จบโปรแกรม GPA(เดิม) < 2.00 แสดงสถานะ Probation (ภาคทัณฑ์) GPA(เดิม) >= 2.00 แสดงสถานะ Normal (ปกติ) หลังจากแสดงสถานะแล้วให้โปรแกรมรับค่า GPS และ GPA ของภาคการศึกษาปัจจุบัน แล้วแสดงผลสถานะการเรียน

57 Assignment (2) โดยมีเงื่อนไขดังนี้
GPA(ใหม่) < 1.00 แสดงสถานะ Retried GPA(ใหม่) >= 2.00 แสดงสถานะ Normal GPA(เดิม) < 2.00 GPS(ใหม่) >= 2.00 แสดงสถานะ Probation GPS(ใหม่) < 2.00 แสดงสถานะ Retried

58 Assignment (3) ตัวอย่างการรันโปรแกรม Enter GPA (past) : 0.74
Your Status : Retired Sorry, Try again. Enter GPA (past) : 2.50 Your Status : Normal Enter GPS (present) : 1.00 Enter GPA (present) : 1.75 Your Status : Probation Enter GPA (past) : 1.50 Your Status : Probation Enter GPS (present) : 2.00 Enter GPA (present) : 1.75 Enter GPA (past) : 1.50 Your Status : Probation Enter GPS (present) : 1.00 Enter GPA (present) : 1.25 Your Status : Retired Sorry, Try agin. Enter GPA (past) : 1.50 Your Status : Probation Enter GPS (present) : 3.0 Enter GPA (present) : 2.25 Your Status : Normal

59 Assignment (4) จงเขียนผังงานโปรแกรมเครื่องคิดเลขที่มีตัวอย่างผลการรันดังต่อไปนี้ Enter Num1 : 3 Enter Num2 : 6 Calculator Menu : 1. + 2. – 3. * 4. / 5. % Choose menu : 1 Ans: Num1 + Num2 = 9

60 Assignment (5) จงเขียนโปรแกรมเพื่อแยกสาร 5 ชนิดซึ่งแต่ละชนิดมีคุณสมบัติดังต่อไปนี้ ชนิดที่ 1 มีคาร์บอนเป็นองค์ประกอบ, มี 5 อะตอม, เป็นก๊าซ ชนิดที่ 2 มีคาร์บอนเป็นองค์ประกอบ, มี 6 อะตอม, เป็นของเหลว ชนิดที่ 3 มีไนโตรเจนเป็นองค์ประกอบ, มี 6 อะตอม, เป็นก๊าซ ชนิดที่ 4 มีไนโตรเจนเป็นองค์ประกอบ, มี 4 อะตอม, เป็นของแข็ง ชนิดที่ 5 เป็นสารชนิดที่ 1 และมี ไฮโดรเจนเป็นองค์ประกอบ

61 Assignment (6) มีส่วนของคำสั่ง switch อยู่ให้หาค่า x , y และ z หลังจากผ่านส่วนของคำสั่ง switch นี้ โดยกำหนดให้ x = 1, y = 0 และ z = 0 switch(x%2) { case 0 : x = 2; y = 3; case 1 : x = 4; break; default : y = 3; x = z; }

62 Assignment (7) จงเขียนโปรแกรมเพื่อรับค่าตัวเลขของผู้ใช้งานตู้ ATM พร้อมทั้งแสดงผลจากการรับค่าตัวเลขดังกล่าวออกสู่หน้าจอ (ให้วิเคราะห์ขั้นตอนการทำงาน เขียนรายละเอียดการทำงาน รหัสเทียม Flowchart พร้อมทั้งเขียนโปรแกรม) ตัวอย่าง ผู้ใช้ป้อนค่า : 2,500 ->แบงค์ 1,000 จำนวน 2 ใบ ->แบงค์ 500 จำนวน 1 ใบ ข้อควรระลึก - ป้อนค่าเป็นเลขทศนิยมและเลขหลักสิบได้ไหม? - มีวิธีการใดบ่งบอกมิให้ผู้ใช้ป้อนค่าผิดหรือไม่?


ดาวน์โหลด ppt Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

งานนำเสนอที่คล้ายกัน


Ads by Google