241 101 Introduction to Computer Programming 4 introcomcoe

  • Slides: 49
Download presentation
241 -101 Introduction to Computer Programming ����� 4 ����� คณาจารยภาควชาวศวกรรมคอมพวเต อร introcom@coe. psu. ac.

241 -101 Introduction to Computer Programming ����� 4 ����� คณาจารยภาควชาวศวกรรมคอมพวเต อร introcom@coe. psu. ac. th Department of Computer Engineering, PSU

241 -101 Introduction to Computer Programming ����� • ����������� − if –else , switch-case

241 -101 Introduction to Computer Programming ����� • ����������� − if –else , switch-case • ����������� − for , while ��� do while • ��������������������� Department of Computer Engineering, PSU 2

241 -101 Introduction to Computer Programming ������� �� กำหนดให x = 5 และ y

241 -101 Introduction to Computer Programming ������� �� กำหนดให x = 5 และ y =10 ������� x==y 0 ���� x>y 0 ���� x<y 1 ���� x>=y 0 ���� x<=y 1 ���� x!=y 1 ���� Department of Computer Engineering, PSU 5

241 -101 Introduction to Computer Programming ������� (Logical Operators) • ���������� �������� ! &&

241 -101 Introduction to Computer Programming ������� (Logical Operators) • ���������� �������� ! && || ����� (NOT) ��� (AND) ���� (OR) ���� if((x Department of Computer Engineering, PSU > 10) && (x < 20)) 6

241 -101 Introduction to Computer Programming ! ����� (NOT) • ������������������ • ��������� !a

241 -101 Introduction to Computer Programming ! ����� (NOT) • ������������������ • ��������� !a �������������� � a true false true Department of Computer Engineering, PSU 7

&& ��� (AND) , (OR) 241 -101 Introduction to Computer Programming || ���� •

&& ��� (AND) , (OR) 241 -101 Introduction to Computer Programming || ���� • a && b ��������������� • a || b ��������������� a b a &&���� b a || b true false Department of Computer Engineering, PSU true false true false 8

241 -101 Introduction to Computer Programming ������ �� ��������� q ���������� (condition statement) ��������

241 -101 Introduction to Computer Programming ������ �� ��������� q ���������� (condition statement) �������� • if, if-else • switch-case Department of Computer Engineering, PSU 10

241 -101 Introduction to Computer Programming ���� #include<stdio. h> int main() { int age;

241 -101 Introduction to Computer Programming ���� #include<stdio. h> int main() { int age; char gender; gender = 'm'; if(gender == 'm') printf("Male n"); ผลลพธของ โปรแก รม Male return 0; } Department of Computer Engineering, PSU Gender. c 14

241 -101 Introduction to Computer Programming ���� #include<stdio. h> void main() { int age;

241 -101 Introduction to Computer Programming ���� #include<stdio. h> void main() { int age; printf("How old are you? : "); scanf("%d", &age); if(age>=60) printf("You are old : )n"); printf("Good bye!!n"); } ผลลพธของ โปรแกรม 1 ผลลพธของโปรแกรม How old are you? : 67 How old are you? : 34 You are old : ) Good bye!! 2 Good bye!! Department of Computer Engineering, PSU Age. c 15

241 -101 Introduction to Computer Programming ������ if • เงอนไขทตองการตรวจสอบสามารถนำมารวมก นได เชน #include<stdio. h>

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. "); } } #include<stdio. h> int main () { int a =2, b=7; if((a>0)&& (b>0)) printf("OK. "); } Mix. c, Mix 1. c Department of Computer Engineering, PSU 16

241 -101 Introduction to Computer Programming ����� • ������������ #include<stdio. h> ��������� int main

241 -101 Introduction to Computer Programming ����� • ������������ #include<stdio. h> ��������� int main () { ���� 80 ������ ‘A’ int score; printf("Enter your score") ; scanf("%d", &score); if(score >=80) printf("You get grade A n"); } Department of Computer Engineering, PSU Score. c 17

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

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 Test. Else. c 20

241 -101 Introduction to Computer Programming ������ ��������������� ������� 80 ������ ‘A’ ������ 80

241 -101 Introduction to Computer Programming ������ ��������������� ������� 80 ������ ‘A’ ������ 80 ���� ‘B’ Department of Computer Engineering, PSU 21

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

241 -101 Introduction to Computer Programming #include<stdio. h> int main () { int score; printf("Enter your score ") ; scanf("%d", &score); if(score >=80) printf("You get grade A n"); else printf("You get grade B n"); } Score 1. c Department of Computer Engineering, PSU 22

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

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 Test. Block. c 24

241 -101 Introduction to Computer Programming ����� ��� if-else-ifelse if (������ 1) ����� 1;

241 -101 Introduction to Computer Programming ����� ��� if-else-ifelse if (������ 1) ����� 1; ��������� 1 else if (������ 2) ����� 2; else if (������ 3) ����� 3; else ����� 4; Department of Computer Engineering, PSU 26

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

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

241 -101 Introduction to Computer Programming #include<stdio. h> int main() { float points; printf("Please

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 ผลลพธ printf("Fail. . . n"); ผลลพธ Please Enter Your Score 89 printf("Bye bye. . See you again next semestern"); Please Enter Your Score 55 Congratulations! } A You get grade D Bye bye. . See you again next semester Department of Computer Engineering, PSU Test. Grade. c 29

241 -101 Introduction to Computer Programming ������� switch-case switch (���������� ) { case ������

241 -101 Introduction to Computer Programming ������� switch-case switch (���������� ) { case ������ 1: ����� 1; switch ��� case ������ 2: ����� 2; ������� case ������ 3: ����� 3; case … … } Department of Computer Engineering, PSU 31

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

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: puts("ONE"); THREE case 2: puts("TWO"); case 3: } } ผลลพธ ������ Enter integer 1 or 2 or 3 1 puts("THREE"); ONE TWO THREE ������ Test. Switch. c break; ������ Department of Computer Engineering, PSU 32

241 -101 Introduction to Computer Programming #include<stdio. h> int main() { int c; printf("Enter

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: puts("ONE"); break; ผลลพธ case 2: puts("TWO"); Enter integer 1 or 2 or 3 2 break; TWO case 3: puts("THREE"); break; ผลลพธ } Enter integer 1 or 2 or 3 1 } ONE Test. Switch. c Department of Computer Engineering, PSU 34

������ 241 -101 Introduction to Computer Programming • switch-case default ��������� switch-case • ����������

������ 241 -101 Introduction to Computer Programming • switch-case default ��������� switch-case • ���������� case ������������ default Department of Computer Engineering, PSU Test. Switch. c 35

241 -101 Introduction to Computer Programming #include<stdio. h> int main() { int c; printf("Enter

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: puts("ONE"); Enter integer 1 or 2 or 3 4 Out of range break; case 2: puts("TWO"); break; case 3: puts("THREE"); break; default: printf("Out of range") } } Test. Switch. c Department of Computer Engineering, PSU 36

241 -101 Introduction to Computer Programming ������ • ��������������� Hello ����� 20 ������� #include<stdio.

241 -101 Introduction to Computer Programming ������ • ��������������� Hello ����� 20 ������� #include<stdio. h> int main() { printf(“Hello printf(“Hello ………. } Department of Computer Engineering, PSU "); "); "); 20 ���� � 37

241 -101 Introduction to Computer Programming ������ • ������������������ ����������� • ��������� loop •

241 -101 Introduction to Computer Programming ������ • ������������������ ����������� • ��������� loop • ��������� �� for loop, while loop ��� do while loop Department of Computer Engineering, PSU 38

Flow chart ������������ for 241 -101 Introduction to Computer Programming ������������ ��������� Department of

Flow chart ������������ for 241 -101 Introduction to Computer Programming ������������ ��������� Department of Computer Engineering, PSU 41

������ 241 -101 Introduction to Computer Programming • while ����� while loop ��� (����

������ 241 -101 Introduction to Computer Programming • while ����� while loop ��� (���� ) ������ ; while ������������ ������������ Department of Computer Engineering, PSU 42

241 -101 Introduction to Computer Programming ������ while ������� ���� ������ Department of Computer

241 -101 Introduction to Computer Programming ������ while ������� ���� ������ Department of Computer Engineering, PSU 43

������ 241 -101 Introduction to Computer Programming for loop while loop #include<stdio. h> int

������ 241 -101 Introduction to Computer Programming for loop while loop #include<stdio. h> int main() { { int i; for (i=0; i<5; i++) printf("* n"); int i=0; while (i<5) { printf("* n"); i++; } } Department of Computer Engineering, PSU } 44

241 -101 Introduction to Computer Programming ����� do while loop do { กลมคำสง ;

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

241 -101 Introduction to Computer Programming ���� do while loop #include<stdio. h> int main()

241 -101 Introduction to Computer Programming ���� do while loop #include<stdio. h> int main() { int i=0; do { printf("* n"); i++; } while (i<5); } Department of Computer Engineering, PSU ผลลพธ * * * 48

������� do while ��� while loop 241 -101 Introduction to Computer Programming while loop

������� do while ��� while loop 241 -101 Introduction to Computer Programming while loop do loop �������� ���� ������ Department of Computer Engineering, PSU ������ ������� � ���� 49