242 101 241 101 Introduction to Computer Programming

  • Slides: 79
Download presentation
242 -101, 241 -101 Introduction to Computer Programming บทท 3 โครงสรางควบคม Control Structures คณาจารยภาควชาวศวกรรมคอมพวเต

242 -101, 241 -101 Introduction to Computer Programming บทท 3 โครงสรางควบคม Control Structures คณาจารยภาควชาวศวกรรมคอมพวเต อร introcom@coe. psu. ac. th Department of Computer Engineering, PSU

242 -101, 241 -101 Introduction to Computer Programming && และ (AND) , || หรอ

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 true false Department of Computer Engineering, PSU false true false 10

ลำดบความสำคญของตวดำเน หากในนพจนมตวดำเนนการ มากกวาหนง 242 -101, 241 -101 Introduction to Computer Programming (operator) และไมมวงเลบ เปนไปดงน

ลำดบความสำคญของตวดำเน หากในนพจนมตวดำเนนการ มากกวาหนง 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,

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) ) 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 1 1 12

242 -101, 241 -101 Introduction to Computer Programming ตวอยาง ผลลพธของ โปรแกรม รนครงท 1 #include<stdio.

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(”Goodbyen”); 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() {

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 ผลลพธของโปรแกรม Enter your score : 67 Enter your score : : 34 You pass : -) Good bye!! 2 Good bye!! Department of Computer Engineering, PSU 18

242 -101, 241 -101 Introduction to Computer Programming เงอนไขของคำสง if • เงอนไขทตองการตรวจสอบสามารถนำมารวมก นได เชน

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 ������

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 #include<stdio. h> int main () {

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 1: ") ; 2: ") ; greater than %dn", a, b); less than %dn", a, b); ผลลพธของโปรแกรม 2 Enter integer 1: 77 Enter integer 1: 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 �������

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 ()

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 vs if-else • เราสามารถแปลงนพจนทม

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 = %. 2 fn”, x>=0. 0? x: -x); ใช if-else แทนจะได if(x>=0. 0) printf(abs x = %. 2 fn”, x); Department of Computer Engineering, PSU 27

242 -101, 241 -101 Introduction to Computer Programming แบบฝกหด �� ������������������ ����� 50 �������

242 -101, 241 -101 Introduction to Computer Programming แบบฝกหด �� ������������������ ����� 50 ������� ‘P’ (pass) ������� 50 ������ ‘F’ (fail) Department of Computer Engineering, PSU 28

ตวอยางโปรแกรม conditonal operator #include<stdio. h> 242 -101, 241 -101 Introduction to Computer Programming int

ตวอยางโปรแกรม conditonal operator #include<stdio. h> 242 -101, 241 -101 Introduction to Computer Programming 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 #include<stdio. h> int main () {

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 againn"); } printf("Bye bye. . See you again next semestern"); } ����� �� Fail. . . Attempt again Bye bye. . See you again next semester Department of Computer Engineering, PSU 31

242 -101, 241 -101 Introduction to Computer Programming #include<stdio. h> int main () {

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 } ผลลพธของโปรแกรม Enter integer 1: ") ; 2: ") ; greater than %dn", a, b); equal to %dn", a, b); less than %dn", a, b); รนครงทผลลพธของโปรแกรม 1 รนครงท 2 22 Enter integer 2: 22 Enter integer 1: 22 22 is equal to 22 Enter integer 2: 77 Department of Computer Engineering, PSU 34

242 -101, 241 -101 Introduction to Computer Programming #include<stdio. h> int main() { float

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 An"); } else if (points>=70. 0) printf("You get grade Bn"); else if (points>=60. 0) printf("You get grade Cn"); else if (points>=50. 0) printf("You get grade Dn"); else ผลลพธ รนครงท 12 ผลลพธ printf("Fail. . . n"); รนครงท Please Enter Your Score 89 printf("Bye Please Enter bye. . See Your Score you 55 again next semestern"); Congratulations! } You get grade D You get grade A Bye bye. . . . See you again next semester Department of Computer Engineering, PSU 36

242 -101, 241 -101 Introduction to Computer Programming #include<stdio. h> ผลลพธ int main() Enter

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("ONEn"); THREE case 2: printf("TWOn"); ผลลพธ case 3: } } ������ Enter integer 1 or 2 or 3: 1 printf("THREEn"); ONE TWO THREE ������ break; ������ Department of Computer Engineering, PSU 39

242 -101, 241 -101 Introduction to Computer Programming #include<stdio. h> int main() { int

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("ONEn"); break; ผลลพธ case 2: printf("TWOn"); Enter integer 1 or 2 or 3: 2 break; TWO case 3: printf("THREEn"); break; ผลลพธ } Enter integer 1 or 2 or 3: 1 } ONE Department of Computer Engineering, PSU 41

242 -101, 241 -101 Introduction to Computer Programming #include<stdio. h> int main() { int

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("ONEn"); Enter integer 1 or 2 or 3 4 Out of range break; case 2: printf("TWOn"); break; case 3: printf("THREEn"); 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() ผลลพธ {

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 Goodn"); break; ----case ’b’: Enter your grade: case ’B’: printf(“Goodn"); Fair break; ----case ’c’: Enter your grade: case ’C’: printf(“Fairn"); 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 เฉลย char ch; printf(”Enter a character(a-z):

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(”Voweln”); else printf(”Consonantn”); printf(”End of the program. n”); switch (ch) { case ’a’: printf(”Voweln”); break; case ’e’: printf(”Voweln”); break; case ’i’: printf(”Voweln”); break; case ’o’: printf(”Voweln”); break; case ’u’: printf(”Voweln”); break; default : printf(”Consonantn”); } printf(”End of the program. n”); Department of Computer Engineering, PSU 47

Flow chart การทำงาน คำสงการทำซำ for 242 -101, 241 -101 Introduction to Computer Programming ������������

Flow chart การทำงาน คำสงการทำซำ for 242 -101, 241 -101 Introduction to Computer Programming ������������ ��������� Department of Computer Engineering, PSU 54

Flowchart คำสง 242 -101, 241 -101 Introduction to Computer Programming while ������� ���� ������

Flowchart คำสง 242 -101, 241 -101 Introduction to Computer Programming while ������� ���� ������ Department of Computer Engineering, PSU 57

242 -101, 241 -101 Introduction to Computer Programming คำสงการทำซำ for loop while loop #include<stdio.

242 -101, 241 -101 Introduction to Computer Programming คำสงการทำซำ for loop while loop #include<stdio. h> int main() { { int i=0; while (i<5( { printf("* n"); i++; } } int i; for (i=0; i<5; i++( printf("* n"); } Department of Computer Engineering, PSU 58

242 -101, 241 -101 Introduction to Computer Programming โครงสราง do while loop do {

242 -101, 241 -101 Introduction to Computer Programming โครงสราง do while loop do { กลมคำสง ; } while ( ���� ) ; Department of Computer Engineering, PSU �� ; ���� 61 ����

242 -101, 241 -101 Introduction to Computer Programming ตวอยาง �������������� ? �������� 6 ���������

242 -101, 241 -101 Introduction to Computer Programming ตวอยาง �������������� ? �������� 6 ��������� #include<stdio. h> int main() { int i=6; do { printf("* n"); i++; } while (i<5); } Department of Computer Engineering, PSU do while loop int main() { int i=6; do { printf("* n"); i++; } while (i<12); } 62

เปรยบเทยบโครงสราง do while กบ while loop 242 -101, 241 -101 Introduction to Computer Programming

เปรยบเทยบโครงสราง do while กบ while loop 242 -101, 241 -101 Introduction to Computer Programming while loop �������� ���� �� ������ Department of Computer Engineering, PSU do loop ��������� � ���� 63

242 -101, 241 -101 Introduction to Computer Programming จงเขยนโปรแกรมคำนวณ คาเฉลยคะแนนของ นร. 10 คน Enter

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 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 %. 2 f”, avg); Enter score of student 10: 80 Class average is 77. 90 Department of Computer Engineering, PSU 64

242 -101, 241 -101 Introduction to Computer Programming จงเขยนโปรแกรมเพอแสดงผลตามดาน ลาง Enter Result (1=pass, 2=fail):

242 -101, 241 -101 Introduction to Computer Programming จงเขยนโปรแกรมเพอแสดงผลตามดาน ลาง 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 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

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

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(){

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-En”); printf(”Enter a grade again: ”); scanf(”%c”, &grade); } printf(”%c OKn”, grade); return 0; } Department of Computer Engineering, PSU Enter A OK ---Enter Grade Enter E OK ---- a grade: A 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

โปรแกรมรอรบขอมลจนกวาจะไดคา ในขอบเขต do-while loop #include<stdio. h> ใช 242 -101, 241 -101 Introduction to Computer

โปรแกรมรอรบขอมลจนกวาจะไดคา ในขอบเขต do-while loop #include<stdio. h> ใช 242 -101, 241 -101 Introduction to Computer Programming 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-En”); printf(“Please enter again. ”); } } while(!ok); printf(”%c is OKn”, 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

do { ตวอยางคำสง 242 -101, 241 -101 Introduction to Computer Programming break . .

do { ตวอยางคำสง 242 -101, 241 -101 Introduction to Computer Programming break . . . 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 ��������. . . } Department of Computer Engineering, PSU 71

ตวอยางคำสง 242 -101, 241 -101 Introduction to Computer Programming continue . . . while

ตวอยางคำสง 242 -101, 241 -101 Introduction to Computer Programming continue . . . while (x>0) {. . . if (y>100) continue; // ��� y>100 ����������� printf(”y = %dn”, y); . . . }. . . for(n=0; n<100; n++) {. . . if (n%10==0) continue; // ��� n ������������ (�. . . } Department of Computer Engineering, PSU 72

242 -101, 241 -101 Introduction to Computer Programming ตวอยางโปรแกรม . . . int i;

242 -101, 241 -101 Introduction to Computer Programming ตวอยางโปรแกรม . . . int i; for (i=1; i<=99; i++) { printf(”%02 d”, i); if(i%3==0) { printf(”#n”); continue; } printf(“xxx”); if(i>10) break; } ��������� 01 xxx 02 xxx 03# 04 xxx 05 xxx 06# 07 xxx 08 xxx 09# 10 xxx 11 xxx Department of Computer Engineering, PSU 73

. 1 โปรแกรมรบจำนวนเตมแลวหาผลรวมและผลค ณ #include<stdio. h> 242 -101, 241 -101 Introduction to Computer Programming

. 1 โปรแกรมรบจำนวนเตมแลวหาผลรวมและผลค ณ #include<stdio. h> 242 -101, 241 -101 Introduction to Computer Programming #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 = %dn”, sum); printf(”Product = %dn”, product); return 0; } Department of Computer Engineering, PSU 76

242 -101, 241 -101 Introduction to Computer Programming . 2โปรแกรมหาคา min, max #include<stdio. h>

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 = %dn”, min); printf(”Max = %dn”, max); return 0; } Department of Computer Engineering, PSU 77

. 3ตวอยางผลการรนโปรแกรมหาคา divisors Enter n : 25 Enter n : 18 sum of 242

. 3ตวอยางผลการรนโปรแกรมหาคา divisors Enter n : 25 Enter n : 18 sum of 242 -101, 241 -101 Introduction to Computer Programming 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. 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.

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 numbern", n); break; // ������ do-while �������� n ������ } if(sum>0) printf("n Sum of divisors of n = %dn", sum); } while( n > 2 ); printf("End of program. "); return 0; } //���������� break; ���� continue; �� Department of Computer Engineering, PSU 79