Control Structure II Control Structure if Syntax includestdio






















- Slides: 22


Control Structure II. ទមរងន Control Structure (if ) ឩទ ហណ Syntax: #include<stdio. h> void main() if(expression){ statement 1; } { int number=0; printf(“Enter the number ”); scanf(“%d”, &number); if(number >0) printf(“the number is positive number…=%d”, number); return 0; }

Control Structure 2. 1 ទមរងន Control Structure (if else ) Syntax: ឩទ ហណ if(expression){ statement 1; } else{ statement 2; } #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; }

Control Structure 2. 2. ទមរងន Syntax: Control Structure (if , else if, else…) if(expression){ statement 1; } else if(expression){ statement(s); } else{ statement 2; }

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 ឩទ ហណ #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 Syntax: switch(expression) { case constant 1: statements 1; break; case constant 2: statements 2; break; …………………. . default: statements n; break; }

Control Structure q ឩទ ហ ណ 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 Syntax: for(initialization, condition, increment/decrement) { Statement 1; Statement 2; …………. . . Statement n; } ឩទ ហណ 1 2 4 For(int i=0; i<=10; i++) { printf(“the value of loop are =%d”, i); } 3



Control Structure



Control Structure

Control Structure void main() { } int i; printf("please enter the value: "); scanf("%d", &i); do { printf("%dn", i); i++; } while (i <=10); getch();


Exercise 1. 1 -4 7 -10 … -40 2. x+x^2/2+x^3/3+. . . +x^n/n 3. 1+x+x^2+. . . +x^n 4. 1/2+4/5+7/8+. . . 5. 1+1/2^2+1/3^3+. . . +1/n^n

Exercise