Download presentation
Presentation is loading. Please wait.
Published byAsli Dinçer Modified over 5 years ago
1
Control Structure គោលបំណងនៃមេរៀន អ្វីជា Control structure ?
Control structure គឺជាមែកធាងនៃលក្ខខ័ណ្ឌដែលបានផ្ដល់តំលៃជាលក្ខណៈពិត(True) ឬ មិនពិត(False)។ Control Structure ប្រើប្រាស់ដើម្បីសិក្សា និងដោះស្រាយបញ្ហាដែលមានជាប្រចាំនៅក្នុងសង្គមទៅតាមកាលៈទេសៈនិមួយៗ។
2
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;}
3
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;}
4
Control Structure 2.2. ទម្រង់នៃ Control Structure (if , else if, else…) Syntax: if(expression){ statement1;} else if(expression){ statement(s);} else{ statement2;}
5
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(); }
6
Control Structure Ternary Operator
Ternary Operator គឺជាexpression ដែលសិក្សាល័ក្ខខ័ណ្ឌប្រហាក់ប្រហែល if else statement ដែរ តែវាមានទម្រង់នៃការសរសេរខ្លី។ ប៉ុន្តែ ternary operator មិនត្រូវបានរាប់បញ្ជូលក្នុង មេរៀននេះទេ គ្រាន់តែលើកយកនូវលក្ខណៈមួយចំនួនដែលមានសមត្ថភាពស្រដៀងនឹង if else ប៉ុន្នោះ។ Syntax: Identifier = (test expression)? value1: value2 ;
7
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(); }
8
Control Structure Switch
ចំណាំៈ គ្រប់ Statement case ត្រូវបញ្ជប់ដោយ keyword “break”។ Variable ត្រូវមានប្រភេទទិន្ន័យជាចំនួនគត់ និងមានតួអក្សរ (char)។
9
Control Structure Syntax: switch(expression) { case constant1: statements 1; break; case constant2: statements 2; break; ………………….. default: statements n; break; }
10
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(); }
11
Control Structure Loop
នៅក្នុងការសេរសេរកម្មវិធីដើម្បីដោះស្រាយបញ្ហាអ្វីមួយដែលនៅពេលជួបប្រទះលក្ខខណ្ឌតែ ម្ដង ឬប្រើនដងដដែលៗនោះត្រូវបានគេចាត់ទុកថា loops ។ loop គឺជាការធ្វើសកម្មភាពច្រើនដងដដែលៗរហូតជួបលក្ខខណ្ឌណាមួយទើបវាបញ្ចប់ដំណើរ ការរបស់វា។ ក្នុងនោះដែល loop បានចែកចេញជា 3 ប្រភេទៈ Loop , While Loop , Do While loop។ loop ត្រូវបានគេយកមកប្រើនៅពេលដែល គេដឹងពីចំនួនដងនៃការអនុ វត្តន៍របស់វាច្បាស់លាស់ក្រោមលក្ខខណ្ឌណាមួយ។
12
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
13
Control Structure Initialization: ជាកន្លែងដែលផ្ដល់នូវតំលៃចាប់ផ្ដើម
Condition: ជាកន្លែងដែលសិក្សាលក្ខខណ្ឌ Increment/Decrement: ជាកន្លែងដែលសំរាប់កំណត់តំលៃកើន ឬ ថយ។
14
Control Structure While Loop while loop ត្រូវបានគេយកមកប្រើប្រាស់នៅពេលមានការត្រួតពិនិត្យរូចទើបយកមកអនុវត្ដន៍ការងារជាក្រោយ។ Syntax: while (expression) { Statement 1; Statement 2; …………... Statement n; }
15
Control Structure
16
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();
17
Control Structure Do While Loop
Syntax do { Statement 1; Statement 2; …………... Statement n; } while(expression);
18
Control Structure
19
Control Structure void main() { int i;
printf("please enter the value:"); scanf("%d", &i); do printf("%d\n", i); i++; } while (i <=10); getch();
20
Exercise ចូរគណនាប្រមាណវិធីដូចខាងក្រោមៈ S=1+2+3+…+n S=2+4+6+…+2n
S=1*2*3*…n …+2n S= 1 + 1/2 + 1/ /n S=1! * 2! * 3!...*n! S= nn
21
Exercise 1 -4 7 -10 … -40 x+x^2/2+x^3/3+.....+x^n/n 1+x+x^2+......+x^n
… -40 x+x^2/2+x^3/ x^n/n 1+x+x^ x^n 1/2+4/5+7/ 1+1/2^2+1/3^ /n^n
22
Exercise
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.