Ce diaporama a bien été signalé.
Le téléchargement de votre SlideShare est en cours. ×

# 4b C switch structure .ppt

Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Prochain SlideShare
Control Structures.pptx
Chargement dans…3
×

## Consultez-les par la suite

1 sur 39 Publicité

# 4b C switch structure .ppt

Switch structure

Switch structure

Publicité
Publicité

## Plus De Contenu Connexe

Publicité

### 4b C switch structure .ppt

1. 1. C – switch structure Technical Training Titus
2. 2. if statement • if...else statement provides support to control program flow. • if statement make decisions based on conditions. • It selects an action, if some condition is met. 2
3. 3. if limitations • compare 2 numbers – Greater than – Less than – Equal to • Choice 1. Football 2. Hockey 3. Cricket 4. Volleyball 5. Basketball 3
4. 4. switch statement • It selects an action, if some condition is met • To make a decision from available choices Eg.,- – select a laptop from available models – select a menu from available menu list etc. 4
5. 5. switch case • switch...case statement gives ability to make decisions from fixed available choices. • not making decision based on conditions. • using switch we can write a more clean and optimal code, that take decisions from available choices 5
6. 6. flow chart 6
7. 7. why switch case? • with the if-else statement, the complexity of the program increases whenever the number of alternative path increases. • using multiple if-else constructs in the program might makes it difficult to read and comprehend. 7
8. 8. syntax switch(expression){ case 1: /* Statement/s */ break; case 2: /* Statement/s */ break; case n: /* Statement/s */ break; default: /* Statement/s */ } 8
9. 9. if for number of choices int num=3; if(num==1) printf("One"); if(num==2) printf("Two"); if(num==3) printf("Three"); if(num==4) printf("Four"); if(num==5) printf("Five"); 9
10. 10. if to switch int num=1; switch(num){ case 1: printf("One"); break; case 2: printf("Two"); break; case 3: printf("Three"); break; case 4: printf("Four"); break; case 5: printf("Five"); break; } 10
11. 11. int num=3; if(num==1) printf("One"); if(num==2) printf("Two"); if(num==3) printf("Three"); if(num==4) printf("Four"); if(num==5) printf("Five"); int num=1; switch(num){ case 1: printf("One"); break; case 2: printf("Two"); break; case 3: printf("Three"); break; case 4: printf("Four"); break; case 5: printf("Five"); break; } 11
12. 12. default in switch int num=7; switch(num){ case 1: printf("One"); break; case 2: printf("Two"); break; case 3: printf("Three"); break; case 4: printf("Four"); break; case 5: printf("Five"); break; default: printf("not a valid choice"); } 12
13. 13. rules: • An expression must always execute to a result • Case labels must be constants and unique • Case labels must end with a colon ( : ) • A break keyword may be present in each case • There can be only one default label • We can nest multiple switch statements • cases can be arranged in any order – it is recommended to put them in ascending order – It increases program readability 13
14. 14. • The break statement is optional – It transfers program flow outside of switch...case • The default case is optional – It works like an else block • If no cases are matched then the control is transferred to default block 14
15. 15. odd or even – using switch int number=8; switch(number%2) { //this will give either 0 or 1 case 0: printf("%d is an EVEN number.n“ ,number); break; case 1: printf("%d is an ODD number.n“ ,number); break; } 15
16. 16. • Given the week day in number, convert that into the day of the week 16
17. 17. int wDay=4; switch(wDay) { case 0: printf("Sunday"); break; case 1: printf("Monday"); break; case 2: printf("Tuesday"); break; case 3: printf("Wednesday"); break; case 4: printf("Thursday"); break; case 5: printf("Friday"); break; case 6: printf("Saturday"); break; default: printf("Invalid weekday number."); } 17
18. 18. • Find out whether the given character is a vowel or not 18
19. 19. char ch='e'; switch(ch){ case 'a': printf("Vowel"); break; case 'e': printf("Vowel"); break; case 'i': printf("Vowel"); break; case 'o': printf("Vowel"); break; case 'u': printf("Vowel"); break; default: printf("Consonant"); } 19
20. 20. fall through effect - used • Side effects used to our advantage 20
21. 21. char ch='e'; switch(ch){ case 'a': printf("Vowel"); break; case 'e': printf("Vowel"); break; case 'i': printf("Vowel"); break; case 'o': printf("Vowel"); break; case 'u': printf("Vowel"); break; default: printf("Consonant"); } char ch='e'; switch(ch){ case 'a': case 'e': case 'i': case 'o': case 'u': printf("Vowel"); break; default: printf("Consonant"); } 21
22. 22. • Write a program which will accept only 5 as a valid input 22
23. 23. fall through effect int i=5; switch(i){ case 1: case 2: case 3: case 4: printf("wrong choice"); break; case 5: printf("right choice"); } 23
24. 24. program to try • Given the number of a month, find out how many days are there in that month 24
25. 25. int month=6; switch(month){ case 4: case 6: case 9: case 11: days=30; break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: days=31; break; case 2: days=28; break; default: days=0; break; } 25
26. 26. guess the output int x = 2; switch (x){ case 1: printf("Choice is 1n"); case 2: printf("Choice is 2n"); case 3: printf("Choice is 3n"); case 4: printf("Choice is 4n"); break; default: printf("Choice other than 1, 2, 3 and 4n"); break; } printf("After Switch"); 26
27. 27. Output: Choice is 2 Choice is 3 Choice is 4 After Switch 27
28. 28. default - anywhere switch (x){ default: printf("Choice other than 1 and 2"); break; case 1: printf("Choice is 1"); break; case 2: printf("Choice is 2"); break; } 28
29. 29. case from an array index switch (x) { case arr[0]: printf("Choice 1n"); case arr[1]: printf("Choice 2n"); case arr[2]: printf("Choice 3n"); } 29
30. 30. nested switch int a = 100; int b = 200; switch(a) { case 100: printf("This is part of outer switchn", a ); switch(b) { case 200: printf("This is part of inner switchn", a ); } } printf("Exact value of a is : %dn", a ); printf("Exact value of b is : %dn", b ); 30
31. 31. limitations of switch...case • switch...case work only with integer, character and enumeration constant. • case label must follow a constant. It does not work with variables and expressions. • programs written using switch...case can be transformed to if...else...if. But not all if...else...if programs can be converted to switch...case. 31
32. 32. points to ponder • switch is a decision making construct in 'C' • switch is used in a program where multiple decisions are involved • switch must contain an executable test-expression. • each case may include a break keyword • case label must be constants and unique • default is optional • multiple switch statements can be nested within one another. • any number of statements are allowed in a case • Statement(s) before the first case statement will not be executed 32
33. 33. conclusion • Use if...else...if statement when - – There are conditions instead of list of choices. – There are few number of conditions. • Use switch...case when - – There is a list of choices, from which you need to take decision. – Choices are in the form of integer, character or enumeration constant. 33
34. 34. • End 1 34
35. 35. Extras 35
36. 36. read weekday number and print weekday name int main(){ int wDay; printf("Enter weekday number (0-6): "); scanf("%d",&wDay); switch(wDay) { case 0: printf("Sunday"); break; case 1: printf("Monday"); break; case 2: printf("Tuesday"); break; case 3: printf("Wednesday"); break; case 4: printf("Thursday"); break; case 5: printf("Friday"); break; case 6: printf("Saturday"); break; default: printf("Invalid weekday number."); } printf("n"); return 0; } 36