1 / 66

Control Statement: If … Else, Switch

Control Statement: If … Else, Switch. Lt Col Amirul Azim CSE Dept MIST. Control Statement. Statement A statement is a part of program that can be executed. It instructs the computer to take a specific action, such as display to the screen, or collect input. Category of Statements

underwood
Download Presentation

Control Statement: If … Else, Switch

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Control Statement: If … Else, Switch Lt Col AmirulAzim CSE Dept MIST

  2. Control Statement • Statement • A statement is a part of program that can be executed. • It instructs the computer to take a specific action, such as display to the screen, or collect input. • Category of Statements • Selection/Conditional - If and switch • Iteration/loop - while, for, do-while • Jump - break, continue, goto, return • Label - case, default • Expression - statements composed of valid expression • Block/compound - blocks of code

  3. Selection/Conditional • If • Switch If else statements in C language Simple if condition if else condition ladder (multiple) if condition nested (if within if) condition

  4. Selection/Conditionalif

  5. Selection/Conditionalif -Practical example:if it is not raining, we will go to school. -Format: if(expression /test-condition) statement/ body; • In this type of if-statement, the sub-statement will only be executed if the expression is non-zero/true. • If the test expression is evaluated to false (0), statements inside the body of if is skipped from execution. No Semicolon

  6. Selection/Conditionalif

  7. Control Statement (cont.)if -Format: if(expression /test-condition) { statement1; statement2; ………………..; statement n; } The if statement evaluates the test expression inside the parenthesis

  8. Sample Programif #include<stdio.h> void main() { int var; scanf(“ %d”, &var); if(var > 0) printf(“%d is a Positive Number”, var); }

  9. Sample Program (cont.)if #include<stdio.h> void main() { int var; scanf(“ %d”, &var); if(var > 0) { printf(“%d is a Positive Number”, var); } }

  10. Sample Program (cont.) if #include<stdio.h> void main() { int var; scanf(“ %d”, &var); if(var > 0) printf(“%d is a Positive Number”, var); if(var < 0) printf(“%d is a Negative Number”, var); }

  11. Sample Program (cont.) if #include<stdio.h> void main() { int var; scanf(“ %d”, &var); if(var %2 == 0) printf(“%d is an Even Number”, var); if(var %2 != 0) printf(“%d is an Odd Number”, var); }

  12. Control Statementif else • This type of if condition is used, when you have one condition and two body of code (two set of statements) to execute based on a single condition. if( test-condition) { True-block; } else { False-block; } If test-condition is true (non zero value), True-block will execute and if test-condition is false, False-block will execute

  13. Control Statementif else

  14. Control Statementif else -Practical example:ifx is divisible by 2, then x is an even number. otherwisex an odd number -Format: if(expression) statement1; elsestatement2; -Format: if(expression) statement1; else statement2; No Semicolon No Semicolon

  15. Sample Programif else #include<stdio.h> void main() { intvar; scanf(“ %d”, &var); if(var > -1) printf(“%d is a Positive Number”, var); else printf(“%d is a Negative Number”, var); }

  16. Control Statement (Cont.)if else • Format: if(expression) { statement1; statement2; ………………… } else statement 3;

  17. Control Statement (Cont.)if else • Format: if(expression) statement1; else { statement2; ………………… statement n; }

  18. Control Statement (Cont.)if else • Format: if(expression) {statement1; statement2; ………………… } else { statement4; …………………. statement n; }

  19. Control Statement (Cont.)if else • Format: if(expression) { statement 1; } statement m; //Wrong else //mismatched if else { statement4; …………………. statement n; }

  20. Sample Programif else #include<stdio.h> void main() { intvar, mid; scanf(“ %d”, &var); if(var %2 == 0) { mid = var / 2; printf(“Mid = %d”, mid); } else { mid = (var + 1) / 2; printf(“Mid = %d”, mid); } } Input1 Output1 4 2 Input2 Output2 5 3

  21. Nested if else This type of if is used, when you want to check another condition within if condition’s block, suppose there are two conditions, condition1 and condition2 and you want to execute condition2 if condition1 is true

  22. Nested if else

  23. Nested if else • Format: if (expression1) { if(expression2) statement1; else statement2; }

  24. Nested if else (cont.) if (expression1) { if(expression2) statement1; else statement2; } else { if(expression3) statement3; }

  25. Sample Program #include<stdio.h> void main() { int x; scanf(“ %d”, &x) if(x > 5) { if(x < 10) printf(“%d is within 5 and 10”, x); } }

  26. Sample Program(cont.) #include<stdio.h> void main() { int x; scanf(“ %d”, &x) if(x > 5) { if(x < 10) printf(“%d is within 5 and 10”, x); } else printf(“%d is not within 5 and 10”, x); }

  27. Sample Program(cont.) #include<stdio.h> void main() { int x; scanf(“ %d”, &x) if(x > 5) if(x < 10) printf(“%d is within 5 and 10”, x); else printf(“%d is not within 5 and 10”, x); }

  28. Sample Program(cont.) #include<stdio.h> void main() { int x; scanf(“ %d”, &x) if(x > 5 && x < 10) { printf(“%d is within 5 and 10”, x); } else printf(“%d is not within 5 and 10”, x); }

  29. Sample Program(cont.) Test whether given number is even or odd if it is below 100. #include <stdio.h> #include<conio.h> void main(){ int n ; printf("Enter any integer number: ") ; scanf("%d", &n) ; if ( n < 100 ) { printf("Given number is below 100\n") ; if( n%2 == 0) printf("And it is EVEN") ; else printf("And it is ODD") ; } else printf("Given number is not below 100") ; }

  30. Sample Program(cont.) /* Program to check to entered character if vowel or consonant.*/ #include <stdio.h> int main() { char ch; printf("Enter a character: "); scanf("%c",&ch); if( (ch>='A' && ch<='Z') || (ch>='a' && ch<='z')) { if( ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'||ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u') { printf("%c is a VOWEL",ch); } else { printf("%c is a CONSONANT",ch); } } else { printf("%c is not a valid character",ch); } return 0; }

  31. else if ladder This type of if statement is used when you have more than one test conditions and blocks to execute.

  32. else if ladder • Format: if(expression) statement1; else if(expression) statement2; .......................... ……………………….. else if(expression) statement_n; else statement _x; X =1 X =2 else X =3 X =4

  33. Sample else if(a >= 60 && a<65) printf(“B”); else if(a >= 55 && a<60) printf(“B-”); ……………………………… ……………………………… else if(a >= 40 && a<45) printf(“D”); else printf(“F”); } #include<stdio.h> void main() { int a; scanf(“ %d”, &a); if(a >= 80) printf(“A+”); else if(a >= 75 && a<80) printf(“A”); else if(a >= 70 && a<75) printf(“A-”); else if(a >= 65 && a<70) printf(“B+”);

  34. Sample else if(a >= 60) printf(“B”); else if(a >= 55) printf(“B-”); ……………………………… ……………………………… else if(a >= 40) printf(“D”); else printf(“F”); } #include<stdio.h> void main() { int a; scanf(“ %d”, &a); if(a >= 80) printf(“A+”); else if(a >= 75) printf(“A”); else if(a >= 70) printf(“A-”); else if(a >= 65) printf(“B+”);

  35. Sample  Find the largest of three numbers. #include <stdio.h> void main() { int a, b, c ; printf("Enter any three integer numbers: ") ; scanf("%d%d%d", &a, &b, &c) ; if( a>=b && a>=c) printf("%d is the largest number", a) ; else if (b>=a && b>=c) printf("%d is the largest number", b) ; else printf("%d is the largest number", c) ; }

  36. Sample /*program to check entered number if ZERO, POSITIVE or NEGATIVE.*/ #include <stdio.h> int main() { int num; printf("Enter an integer number:"); scanf("%d",&num); if(num==0){ printf("Number is ZERO"); } else if(num>1) { printf("Nunber is POSITIVE"); } else { printf("Number is NEGATIVE"); } return 0; }

  37. Programming Switch Statement

  38. Switch Statement

  39. Switch Statement case 1 Action 1 case 2 Action 2 case 3 Action 3 default Action 4

  40. Switch Statement Start value1 action 1 value2 action 2 value3 action 3 value4 action 4 End

  41. Switch - Flow Chart

  42. Switch - Flow Chart

  43. Switch Statement- Syntax switch (selector expression) { case 1: sequence of statement; break; case 2: sequence of statements; break; case 3: sequence of statements; break; default : sequence of statements; }

  44. BREAK STATEMENT • # include <stdio.h> • int main() • { • int i; double number, sum = 0.0; • for(i=1; i <= 10; ++i) • { • printf("Enter a n%d: ",i); scanf("%lf",&number); • // If user enters negative number, loop is terminated • if(number < 0.0) • { break; • } • sum += number; // sum = sum + number; • } • printf("Sum = %.2lf",sum); • return 0; • }

  45. CONTINUE STATEMENT • # include <stdio.h> • int main() • { • int i; double number, sum = 0.0; • for(i=1; i <= 10; ++i) • { • printf("Enter a n%d: ",i); scanf("%lf",&number); • // Negative numbers are skipped from calculation • if(number < 0.0) • { continue; • } • sum += number; // sum = sum + number; • } • printf("Sum = %.2lf",sum); • return 0; • }

  46. Switch Statement • The switch statement provides another way to decide which statement to execute next • The switch statement evaluates an expression, then attempts to match the result to one of several possible cases • The match must be an exact match. switch ( expression ){ case value1: statement-list1 case value2: statement-list2 case value3 : statement-list3 case... }

  47. Switch Statement • Each case contains a value and a list of statements • The flow of control transfers to statement associated with the first case value that matches switch ( expression ) { case value1: statement-list1 case value2: statement-list2 case value3 : statement-list3 case... }

  48. Switch Statement • The break statement can be used as the last statement in each case's statement list • A break statement causes control to transfer to the end of the switch statement • If a break statement is not used, the flow of control will continue into the next case switch ( expression ){ case value1: statement-list1 break; case value2: statement-list2 break; case value3 : statement-list3 break; case... }

  49. Switch Statement • Examples of the switch statement: switch (option){ case 'A': aCount++; break; case 'B': bCount++; break; case 'C': cCount++; break; } When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement

  50. Switch Statement-No Break • Another Example: switch (option){ case 'A': aCount++; break; case 'B': bCount++; break; case 'C': cCount++; break; } switch (option){ case 'A': aCount++; case 'B': bCount++; case 'C': cCount++; }

More Related