Control Structure គោលបំណងនៃមេរៀន អ្វីជា Control structure ? Control structure គឺជាមែកធាងនៃលក្ខខ័ណ្ឌដែលបានផ្ដល់តំលៃជាលក្ខណៈពិត(True) ឬ មិនពិត(False)។ Control Structure ប្រើប្រាស់ដើម្បីសិក្សា និងដោះស្រាយបញ្ហាដែលមានជាប្រចាំនៅក្នុងសង្គមទៅតាមកាលៈទេសៈនិមួយៗ។
Control Structure ទម្រង់នៃ Control Structure (if ) ឩទាហណ៍ៈ #include<stdio.h> void main() { int number=0; printf(“Enter the number ”); scanf(“%d”, &number); if(number >0) printf(“the number is positive number…=%d”, number); return 0; } Syntax: if(expression){ statement1;}
Control Structure 2.1 ទម្រង់នៃ Control Structure (if else ) ឩទាហណ៍ៈ #include<stdio.h> void main() { int number=0; printf(“Enter the number ”); scanf(“%d”, &number); if(number >0) printf(“the number is positive number…=%d”, number); else printf(“the number is negative number…=%d”, number); return 0; } Syntax: if(expression){ statement1;} else{ statement2;}
Control Structure 2.2. ទម្រង់នៃ Control Structure (if , else if, else…) Syntax: if(expression){ statement1;} else if(expression){ statement(s);} else{ statement2;}
Control Structure ឩទាហណ៍ៈ #include<stdio.h> void main() { int number=0; printf(“Enter the number ”); scanf(“%d”, &number); if(number >0){ printf(“the number is positive number…=%d”, number);} else if (<0){ printf(“the number is negative number…=%d”, number); } else{ printf(“the number is zero number…=%d”, number);} getch(); }
Control Structure Ternary Operator Ternary Operator គឺជាexpression ដែលសិក្សាល័ក្ខខ័ណ្ឌប្រហាក់ប្រហែល if else statement ដែរ តែវាមានទម្រង់នៃការសរសេរខ្លី។ ប៉ុន្តែ ternary operator មិនត្រូវបានរាប់បញ្ជូលក្នុង មេរៀននេះទេ គ្រាន់តែលើកយកនូវលក្ខណៈមួយចំនួនដែលមានសមត្ថភាពស្រដៀងនឹង if else ប៉ុន្នោះ។ Syntax: Identifier = (test expression)? value1: value2 ;
Control Structure ឩទាហណ៍ៈ #include<stdio.h> #include<stdbool.h> void main() { int a; scanf("%d", &a); (a>=0)? printf("%d", true): printf("%d", false); getch(); } ឩទាហណ៍ៈ #include<stdio.h> #include<stdbool.h> void main() { int a , show=0; scanf("%d", &a); show=(a>=0)? 1 : 0 ; printf("%d", show) getch(); }
Control Structure Switch ចំណាំៈ គ្រប់ Statement case ត្រូវបញ្ជប់ដោយ keyword “break”។ Variable ត្រូវមានប្រភេទទិន្ន័យជាចំនួនគត់ និងមានតួអក្សរ (char)។
Control Structure Syntax: switch(expression) { case constant1: statements 1; break; case constant2: statements 2; break; ………………….. default: statements n; break; }
Control Structure ឩទាហណ៍ៈ void main() { int input; scanf(“%d”, &input); switch ( input ) { case 1: printf(Play game”); break; case 2: printf(“Load game); break; case 3: printf(“Play multiplayer”); break; case 4: printf( "Thanks for playing!\n" ); break; default: printf( "Bad input, quitting!\n" );break; } getchar(); }
Control Structure Loop នៅក្នុងការសេរសេរកម្មវិធីដើម្បីដោះស្រាយបញ្ហាអ្វីមួយដែលនៅពេលជួបប្រទះលក្ខខណ្ឌតែ ម្ដង ឬប្រើនដងដដែលៗនោះត្រូវបានគេចាត់ទុកថា loops ។ loop គឺជាការធ្វើសកម្មភាពច្រើនដងដដែលៗរហូតជួបលក្ខខណ្ឌណាមួយទើបវាបញ្ចប់ដំណើរ ការរបស់វា។ ក្នុងនោះដែល loop បានចែកចេញជា 3 ប្រភេទៈ Loop , While Loop , Do While loop។ loop ត្រូវបានគេយកមកប្រើនៅពេលដែល គេដឹងពីចំនួនដងនៃការអនុ វត្តន៍របស់វាច្បាស់លាស់ក្រោមលក្ខខណ្ឌណាមួយ។
Control Structure Syntax: for(initialization, condition, increment/decrement) { Statement 1; Statement 2; …………... Statement n; } ឩទាហណ៍ៈ For(int i=0; i<=10; i++) { printf(“the value of loop are =%d”, i); } 1 2 4 3
Control Structure Initialization: ជាកន្លែងដែលផ្ដល់នូវតំលៃចាប់ផ្ដើម Condition: ជាកន្លែងដែលសិក្សាលក្ខខណ្ឌ Increment/Decrement: ជាកន្លែងដែលសំរាប់កំណត់តំលៃកើន ឬ ថយ។
Control Structure While Loop while loop ត្រូវបានគេយកមកប្រើប្រាស់នៅពេលមានការត្រួតពិនិត្យរូចទើបយកមកអនុវត្ដន៍ការងារជាក្រោយ។ Syntax: while (expression) { Statement 1; Statement 2; …………... Statement n; }
Control Structure
Control Structure បើសិន expression មានលក្ខខណ្ឌពិតនោះវានឹងអនុវត្ត Statement ដ៏ទៃទៀតដែលនៅក្រោមវា។ បើសិន expression មានលក្ខខណ្ឌមិនពិតនោះវាមិនអនុវត្ត Statement ដ៏ទៃទៀតដែលនៅក្រោមវាទេ។ ឩទាហណ៍ៈ int main() { int i; printf("please enter number "); scanf("%d", &i); while(i <=10) printf("the value of While =%d \n", i); i++; } getch();
Control Structure Do While Loop Syntax do { Statement 1; Statement 2; …………... Statement n; } while(expression);
Control Structure
Control Structure void main() { int i; printf("please enter the value:"); scanf("%d", &i); do printf("%d\n", i); i++; } while (i <=10); getch();
Exercise ចូរគណនាប្រមាណវិធីដូចខាងក្រោមៈ S=1+2+3+…+n S=2+4+6+…+2n S=1*2*3*…n +2+4+6+…+2n S= 1 + 1/2 + 1/3 + ... + 1/n S=1! * 2! * 3!...*n! S=11 + 22 + 33 + ... + nn
Exercise 1 -4 7 -10 … -40 x+x^2/2+x^3/3+.....+x^n/n 1+x+x^2+......+x^n 1 -4 7 -10 … -40 x+x^2/2+x^3/3+.....+x^n/n 1+x+x^2+......+x^n 1/2+4/5+7/8+...... 1+1/2^2+1/3^3+.....+1/n^n
Exercise