5. IF, ELSE IF, ELSE SAMPLE int main(){ int marks; printf("Enter Marks :"); scanf("%d",&marks); if ( marks<=100 && marks>=80){printf("Your grade is A");} else if ( marks<=79 && marks>=60){printf("Your grade is B");} else if ( marks<=59 && marks>=40){printf("Your grade is C");} else if ( marks<=39 && marks>=20){printf("Your grade is D");} else if ( marks<=19 && marks>=0){printf("Your grade is E");} else{printf("The exam mark must be between 0 and 100 inclusive.");} return 0; }
7. SWITCH CASE Format: Only Integer/ character switch( conditional expression/variable) { case label1: C statement; break; case label2: C statement; break; … default: pernyataanC; } value Exit from option Functioning like else
8. How To Convert this If Else Snippet to Switch? if ( input == 1) printf(“One”); else if (input == 2) printf(“Two”); else if (input == 3) printf(“Three”); else if (input == 4) printf(“Four”); else printf(“Five”);
9. Answer: switch(input) { case 1: printf("One"); break; case 2: printf("Two");break; case 3: printf("Three");break; case 4: printf("Four");break; default: printf("Five");break; }
10. IF-ELSE / SWITCH CASE Side by Side switch(input) { case 1: printf("One"); break; case 2: printf("Two");break; case 3: printf("Three");break; case 4: printf("Four");break; default: printf("Five");break; } if ( input == 1) printf(“One”); else if (input == 2) printf(“Two”); else if (input == 3) printf(“Three”); else if (input == 4) printf(“Four”); else printf(“Five”);
11.
12. Format :IMPORTANT! 1. Counter 2. Counter initialization 3. Conditional expression testing 4. Increment/decrement of counter counter = 0; while (conditional expression) { C statement; counter++; }
21. Example: I ++; ++a; y--; :: increment is used if the initial value of counter is less while decrement is used if the initial value of counter is high.
22. Example 1: void main() { int nom1,nom2,nom3,nom4,nom5; printf(“Enter one number :”); scanf(“%d”,&nom1); printf(“nEnter one number:”); scanf(“%d”,&nom2); printf(“nEnter one number:”); scanf(“%d”,&nom3); printf(“nEnter one number:”); scanf(“%d”,&nom1); printf(“nEnter one number:”); scanf(“%d”,&nom5); } void main() { int nom; inti; i =0; while(i <5) { printf(“nEnter one number:”); scanf(“%d”,&nom); i++; } } counter condition increment WHILE LOOP CONVERT
23. Determine the output of the following whileloop statement : a = 1; while ( a <= 5) { printf(“Number %d”,a); a++; }
24. Use do..while statement Repetition will occurred if condition is TRUE Similar to while, must include 3 important things Format : counter = 0; do { C statement; counter++; } while (x < 5); Repeated statements DO-WHILE LOOP
26. Example : Converting the following do..while loop to while loop i = 2; j = 1; do { i++; printf(“ i x j = %d”, i * j); } while (i < 8); i= 2; j = 1; while (i < 8) { i++; printf(“ i x j = %d”, i * j); } while do..while DO-WHILE to WHILE LOOP CONVERT
34. A ‘while’ loop is used when the number of times the loop gets executed is not known and the loop should not be executed when the condition is initially false
35. A ‘do while’ loop is used when the number of times the loop gets executed is not known and the loop should be executed even when the condition is initially false