if 1 scanfd score 2 if score 60

  • Slides: 29
Download presentation

ตวอยางการใชงาน if 1 scanf("%d", &score); 2 if (score >= 60) 3 printf("You passn"); 4

ตวอยางการใชงาน if 1 scanf("%d", &score); 2 if (score >= 60) 3 printf("You passn"); 4 printf("bye"); score = 60 ผลลพธทได score = 59 ผลลพธทได ? You pass bye score = 80 ผลลพธทได ? ? You pass bye 5

ตวอยางการใชงาน 1 2 3 4 5 6 7 8 9 10 11 12 if

ตวอยางการใชงาน 1 2 3 4 5 6 7 8 9 10 11 12 if #include <stdio. h> int x, y; int main(void) { printf("Enter total score : "); scanf("%d", &x); printf("Enter number of students : "); scanf("%d", &y); if(y==0) printf("Divided by zero !n"); return(0); } 6

ตวอยางการใชงาน 1 2 3 4 5 6 7 8 9 10 11 if #include

ตวอยางการใชงาน 1 2 3 4 5 6 7 8 9 10 11 if #include <stdio. h> int main(void) { int magic = 123; // กำหนดคา 123 ใหกบตวแปร int guess; printf("Enter your guess: "); scanf("%d", &guess); if (guess == magic) printf("**Right**"); return(0); } ผลลพธทได ? Enter your guess: 123 Enter your guess: 100 ไมแสดงคาอะไรเลย**Right** 7

การควบคมทศทางแบบเลอกท ำ if-else ใชในกรณทมทางเลอกอยสองทาง หาก condition เปน true จะทำ statement if(condition) ชด A หาก

การควบคมทศทางแบบเลอกท ำ if-else ใชในกรณทมทางเลอกอยสองทาง หาก condition เปน true จะทำ statement if(condition) ชด A หาก condition เปน{ false จะทำ statement_A; statement_A 1; statement ชด B else. . . statement_B; statement_An; } else { statement_B 1; . . . statement_Bn; } 9

ตวอยางการใชงาน 1 2 3 4 5 6 7 8 9 10 11 12 if-else

ตวอยางการใชงาน 1 2 3 4 5 6 7 8 9 10 11 12 if-else #include <stdio. h> int main(void) { int score; scanf("%d", &score); if (score >= 60) printf("You pass "); else printf("You fail "); printf("Have a nice day"); return(0); } ผลลพธทได ? 59 You fail Have a nice day ผลลพธทได ? 79 10 You pass Have a nice day

ตวอยางการใชงาน 1 2 3 4 5 6 7 8 9 10 11 12 13

ตวอยางการใชงาน 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 if-else #include <stdio. h> int choice; float radius, circum, area; 1. Cirumference of the circle int main(void) 2. Area of the circle { printf("1. Cirumference of the circlen"); printf("2. Area of the circlen"); Enter your choice 1 or 2 : 1 printf("Enter your choice 1 or 2 : "); คำนวณอะไร ? scanf("%d", &choice); printf("Enter radius of the circle : "); scanf("%f", &radius); Enter your choice 1 or 2 : 2 if(choice==1) { คำนวณอะไร ? circum = 2*3. 14156*radius; printf("Circumference of the circle =%fn", circum); } Enter your choice 1 or 2 : 3 else { คำนวณอะไร ? area = 3. 14156*radius; printf("Area of the circle = %fn", area); } return(0); } 11 ผลลพธทได

ตวอยางการใชงาน 1 2 3 4 5 6 7 8 9 10 11 12 13

ตวอยางการใชงาน 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Nested if #include <stdio. h> int main(void) { int a, b; printf("Please enter two integer: "); scanf("%d %d", &a, &b); if(a>b) printf("%d > %dn", a, b); else if(a<b) printf("%d < %dn", a, b); else printf("%d = %dn", a, b); return 0; ผลลพธทได } Please enter two integer: -2 9 -2 < 9 16

การควบคมทศทางแบบเลอกท ำ if-else if n if-else ใชในกรณทมทางเลอกอยมากกว if(condition_A) { าสองทางเลอก statement_A; statement_A 1; else

การควบคมทศทางแบบเลอกท ำ if-else if n if-else ใชในกรณทมทางเลอกอยมากกว if(condition_A) { าสองทางเลอก statement_A; statement_A 1; else if(condition_B) โดยแตละทางเลอกมเงอนไขตางกน. . . statement_B; else if(condition_C) statement_C; . . . else if(condition_Y) statement_Y; else statement_Z; Tip : else ตวสดทาย ไม จำเ } else if(condition_B) { statement_B 1; . . . } else { statement_Z; . . . } 17

แผนภาพแสดงการทำงาน if-else if Conditi on A True Statem ent A False Conditi on B

แผนภาพแสดงการทำงาน if-else if Conditi on A True Statem ent A False Conditi on B True Statem ent B False Conditi on B True Statem ent C False Statem ent D 18

ตวอยางการใชงาน 1 2 3 4 5 6 7 8 9 10 11 12 13

ตวอยางการใชงาน 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 if-else if #include <stdio. h> int point; int main(void) { printf("Enter your point : "); scanf("%d", &point); if((point>=80)&&(point<=100)) printf("Grade An"); else if((point>=70)&&(point<80)) printf("Grade Bn"); else if((point>=60)&&(point<70)) printf("Grade Cn"); else if((point>=50)&&(point<60)) printf("Grade Dn"); else printf("Grade Fn"); return(0); } 19

การควบคมทศทางแบบเลอกท ำของ switch(variable) ใชในกรณมทางเลอกใหทำหลายทาง { case constant_A : Statement_A 1; จากเงอนไขรวมกน case constant_B :

การควบคมทศทางแบบเลอกท ำของ switch(variable) ใชในกรณมทางเลอกใหทำหลายทาง { case constant_A : Statement_A 1; จากเงอนไขรวมกน case constant_B : case constant_C : . . . default : } Statement_A 2; . . . break; Statement_B 1; Statement_B 2; . . . break; Statement_C 1; Statement_C 2; . . . break; statement ZZ; 23

แผนภาพแสดงการทำงานของ switch Expres sion Matched Stateme case 1 nt 1 Unmatched Matched Stateme case

แผนภาพแสดงการทำงานของ switch Expres sion Matched Stateme case 1 nt 1 Unmatched Matched Stateme case 2 nt 2 Unmatched Matched Stateme case n nt n Unmatched Matched Stateme default nt s break 26

ตวอยางการใชงาน 1 2 3 4 5 6 7 8 9 10 11 12 13

ตวอยางการใชงาน 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include <stdio. h> char grade; int main(void) { printf("Enter your grade : "); scanf("%c", &grade); switch(grade) { case 'A': printf("80 -100n"); break; case 'B': printf("70 -79n"); break; case 'C': printf("60 -69n"); break; case 'D': printf("50 -59n"); break; default: printf("0 -49n"); } return(0); } switch ผลลพธทได Enter your grade : A 80 -100 Enter your grade : D 50 -59 Enter your grade : b 0 -49 ทำไมปอน แลวไดผล 0 -49 b ? 27

ตวอยางการใชงาน 1 2 3 4 5 6 7 8 9 10 11 12 13

ตวอยางการใชงาน 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 switch #include <stdio. h> int choice; float radius, circum, area; int main(void) 1. Cirumference of the circle 2. Area of the circle { printf("1. Cirumference of the circlen"); printf("2. Area of the circlen"); Enter your choice 1 or 2 : 1 printf("Enter your choice 1 or 2 : "); คำนวณอะไร ? scanf("%d", &choice); printf("Enter radius of the circle : "); Enter your choice 1 or 2 : 2 scanf("%f", &radius); คำนวณอะไร ? switch(choice) { case 1 : circum = 2*3. 14156*radius; printf("Circumference of the circle =%fn", circum); Enter your choice 1 or 2 : 3 break; คำนวณอะไร ? case 2 : area = 3. 14156*radius; printf("Area of the circle = %fn", area); break; } return(0); } 28 ผลลพธทได ?