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

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

การควบคุมทิศทางการทำงาน

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


งานนำเสนอเรื่อง: "การควบคุมทิศทางการทำงาน"— ใบสำเนางานนำเสนอ:

1 การควบคุมทิศทางการทำงาน
Flow of control (Loop)

2 การควบคุมทิศทางการทํางานแบบเลือกทํา การควบคุมทิศทางการทํางานแบบวนรอบ
Flow of control การควบคุมทิศทางการทํางานแบบเลือกทํา if, if – else, if – else if, switch การควบคุมทิศทางการทํางานแบบวนรอบ while, do while, for คําสั่งประกอบการควบคุมทิศทาง break, continue, exit()

3 การควบคุมทิศทางแบบเลือกทํา
โปรแกรมจะทําหรือไมทําตามชุดคําสั่งนั้นขึ้นอยูกับเงื่อนไขที่ให เงื่อนไขในโปรแกรมอาจเปนการเปรียบเทียบหรือการดําเนินการทาง ตรรกศาสตรซึ่งใหผลลัพธเปน true หรือ หรือ false การควบคุมทิศทางแบบเลือกทําแบงออกเปน 4 ประเภทคือ if if – else if – else if switch

4 การควบคุมทิศทางแบบเลือกทํา – if
if(condition) statement; condition : เงื่อนไขที่ใหผลลัพธเปน true หรือ false statement : คําสั่งที่จะใหทําหากผลลัพธของเงื่อนไขเปน true หากมีคําสั่งมากกวาหนึ่งคําสั่งจะตองมีเครื่องหมาย { } ปกกาครอบคําสั่งไว้ if(condition) { statement_1; statement_2; ... statement_n; }

5 if – แผนภาพแสดงการทำงาน
Condition False/No True/Yes Statement

6 if – ตัวอย่างการใช้งาน
#include <stdio.h> #include <conio.h> int x,y; void main() { printf(“Enter total score : ”); scanf(“%d”,&x); printf(“Enter number of students : ”); scanf(“%d”,&y); if(y==0) printf(“Divided by zero !\n”); getch(); } Enter total score : 500 Enter number of students : 0 Divided by zero

7 การควบคุมทิศทางแบบเลือกทำ – if-else
if-else ใช้ในกรณีที่มีทางเลือกอยู่สองทางหาก condition เป็นtrue จะทำ statement ชุด A หาก condition เป็น false จะทำ statement ชุด B if(condition) { statement_A1; ... statement_An; } else statement_B1; statement_Bn; if(condition) statement_A; else statement_B;

8 if-else แผนภาพแสดงการทำางาน
True/Yes Condition False/No Statement A Statement B

9 ตัวอย่างการใช้งาน if-else
#include <stdio.h> int choice; float radius, circum, area; void main() { printf(“1.Circumference of the circle\n”); printf(“2.Area of the circle\n”); printf(“Enter your choice 1 or 2 : ”); scanf(“%d”,&choice); printf(“Enter radius of the circle : ”); scanf(“%f”,&radius); if(choice==1){ circum = 2* *radius; printf(“Circumference of the circle = %f\n”,circum); } else if(choice==2){ area = *radius*radius; printf(“Area of the circle = %f\n”,area);

10 การควบคุมทิศทางแบบเลือกทำ if - else if
if(condition_A) { statement_A1; ... } else if(condition_B) statement_B1; else statement_Z1; if(condition_A) statement_A; else if(condition_B) statement_B; else if(condition_C) statement_C; ... else if(condition_Y) statement_Y; else statement_Z; Tip: else ตัวสุดท้ายไม่จำเป็นต้องมี

11 การควบคุมทิศทางแบบเลือกทำ if - else if
False False False Condition_A Condition_B Condition_C True True True Statement_Z Statement_A Statement_B Statement_C

12 ตัวอย่างการใช้งาน if-else if
#include <stdio.h> int point; void main() { printf(“Enter your point : ”); scanf(“%d”,&point); if((point<=100)&&(point>=80)) printf(“Grade A\n”); else if((point<80)&&(point>=70)) printf(“Grade B\n”); else if((point<70)&&(point>=60)) printf(“Grade C\n”); else if((point<60)&&(point>=50)) printf(“Grade D\n”); else printf(“Grade F\n”); }

13 การควบคุมทิศทางแบบเลือกทำ – switch
switch(variable) { case constant_A : statement_A1; statement_A2; ... break; case constant_B : statement_B1; statement_B2; default : statement; }

14 การควบคุมทิศทางแบบเลือกทำ – switch
variable : ตัวแปรหรือนิพจน์ที่ให้ผลเป็นข้อมูลแบบ int หรือ char constant_A, constant_B, … : ค่าคงที่ชนิดเดียวกับตัวแปร variable หากค่าของ variable เท่ากับค่าคงที่ตัวใด โปรแกรมจะทำคำสั่งใน case นั่น ใน case เดียวกันอาจมีค่าคงที่ได้หลายตัว break : ใช้คันระหว่าง case ให้โปรแกรมออกจากการทำงานของswitch หากไม่ใส่ตัวคัน โปรแกรมจะทำการเปรียบเทียบ variable กับ constant ตัวถัดไปเรื๋อยๆ default : หาก variable ไม่ตรงกับ constant ใดเลย โปรแกรมจะทำตามคำสั่งใน default

15 แผนภาพแสดงการทำงาน switch
False False False Condition_A Condition_B Condition_C True True True Default Statement_Z Statement_A Statement_B Statement_C

16 switch – ตัวอย่างการใช้งาน
#include <stdio.h> char grade; void main() { printf(“Enter your grade : ”); scanf(“%c”,&grade); switch(grade) case ‘a’: printf(“80-100\n”); break; case ‘b’: printf(“70-79\n”); break; case ‘c’: printf(“60-69\n”); break; case ‘d’: printf(“50-59\n”); break; default: rintf(“0-49\n”); }

17 การควบคุมทิศทางแบบวนรอบ
โปรแกรมจะทำซ้ำคำสั่งเดิมหรือวนลูป (loop) จนกว่าเงื่อนไขจะเป็นfalse จึงหลุดออกจากลูปนั้น คำสั่งแบบวนรอบมี 3 แบบคือ while do – while for

18 การควบคุมทิศทางแบบวนรอบ while
โปรแกรมจะตรวจสอบเงื่อนไขหากเป็นจริงจะทำตามคำสั่ง เมื่อเสร็จแล้วก็จะกลับไปตรวจสอบเงื่อนไขใหม่ วนไปเรื่อยๆจนกว่าเงื่อนไขจะเป็นเท็จจึงออกจากลูป while(condition) statement; while(condition) { statement_1; statement_2; ... statement_n; }

19 while – แผนภาพแสดงการทำงาน

20 while - ตัวอย่างการใช้งาน
#include <stdio.h> #include <conio.h> int count=0; void main() { while(count<5){ printf(“Line %d\n”, ++count); count++; } getch(); Line 1 Line 2 Line 3 Line 4 Line 5

21 การควบคุมทิศทางแบบวนรอบ do - while
{ statement_1; statement_2; ... statement_n; }while(condition) ; do statement; while(condition)

22 แผนภาพแสดงการทำงาน do - while

23 do - while - ตัวอย่างการใช้งาน
/* This example prompts users for a password */ /* and continued to prompt them until they */ /* enter one that matches the value stored in */ /* checkword. */ #include <stdio.h> #include <string.h> #include <conio.h> void main () { char checkword[80] = “pentium"; char password[80] = ""; do{ printf ("Enter password: "); scanf("%s", password); }while(strcmp(password, checkword)); printf(“Correct”); getch(); }

24 การควบคุมทิศทางแบบวนรอบ – for
เป็นคำสั่งแบบวนรอบที่รวมเอาทั้งการกำหนดค่าเริ่มต้น,เงื่อนไข,และการเปลี่ยนแปลงค่าเริ่มต้นไว้ด้วยกัน for(initial; condition; change) { statement_1; ... } initial กำาหนดค่าเริ่มต้นให้ตัวแปร ตัวแปรนี้จะถูกนำมาใช้ในเงื่อนไข condition เงื่อนไขที่กำหนดจากตัวแปรเพื่อให้โปรแกรมวนรอบ change ส่วนที่เปลี่ยนค่าตัวแปรเพื่อนำไปใช้ตรวจสอบเงื่อนไขใน รอบถัดไปโดยอาจเป็นการเพิ่มหรือลดค่าของตัวแปรทีละ1หรือมากกว่า

25 for – แผนภาพแสดงการทำางาน

26 for – ตัวอย่างการใช้งาน
#include <stdio.h> int count=0; void main() { for(count=1; count<=12; count++) printf(“Line %d\n”, count*5); }

27 คำสั่งประกอบการควบคุมทิศทาง
break ใช้ร่วมกับคำสั่งควบคุมทิศทางแบบเงื่อนไขหรือวนรอบ switch, for, while, และ do เพื่อให้โปรแกรมออกจากลูปเลือกทำหรือลูปวนรอบในสุดที่กำลังทำอยู่ continue ใช้ร่วมกับคำสั่งควบคุมทิศทางแบบวนรอบ มีผลทำให้โปรแกรมหยุดการทำงานในรอบปัจจุบันแล้วไปเริ่มการทำงานในรอบใหม่ exit() ใช้ออกจากการทำงานของโปรแกรม อยู่ในไลบราลี stdlib.h

28 break - ตัวอย่างการใช้งาน
#include <stdio.h> #include <conio.h> int count=0, num; void main() { clrscr(); while(count<10){ printf(“Enter Number (0 for quit): ”); scanf(“%d”, &num); if(num==0){ printf(“Quit Loop”); break; printf(“This message will never be reached”); } // end If else printf(“You Enter %d\n”, num); } // end While getch(); }

29 continue - ตัวอย่างการใช้งาน
#include <stdio.h> #include <conio.h> int i; double array[] = {5, 0, 9, 2, 10}; void main () { for(i = 0; i < 5; i++) if(array[i] == 0) continue; printf(“1/%.02f = %.02f\n”, array[i], 1/array[i]); } getch(); 1/5.00 = 0.20 1/9.00 = 0.11 1/2.00 = 0.50 1/10.00 = 0.10

30 exit() - ตัวอย่างการใช้งาน
#include <stdio.h> #include <stdlib.h> #include <conio.h> float x, y, z; void main() { clrscr(); printf(“Enter 2 numbers (x/y): ”); scanf(“%f/%f”, &x, &y); if(y==0){ printf(“Error, divided by zero”); exit(0); } else printf(“x/y = %f”, x/y); getch();

31 END


ดาวน์โหลด ppt การควบคุมทิศทางการทำงาน

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


Ads by Google