Switch Case
1. Which of the following statements are true.
1. Every switch statement requires a default
Ans: False. You can write a switch case with no default.
2. switch(4.2 > 3.14) is a valid switch control expression.
Ans: True. Relational operator will evaluate to an int value 0 or 1.
3. break makes the control jump out of current code block.
Ans: True
4. break can be used to terminate a program.
Ans: False. break will cause control to jump out of current code block. It will not terminate the program.
5. If control expression matches a case label then control will jump to that case and then to default case since default matches everything.
Ans: False. Default case is executed only if no other case labels match OR if there are no break statements and control advances linearly to default case.
2. Write a program using switch case that takes day of the week (1-7) as user input and prints the name of the day, Monday for 1, Tuesday for 2, and so on. In case user input is outside the range 1-7, program should print an error message using default case.
#include <stdio.h> int main (int argc, char * argv[]) { int day = 0; printf("Enter day of week [1-7]: "); scanf("%d",&day); switch(day) { case 1: printf("Monday\n"); break; case 2: printf("Tuesday\n"); break; case 3: printf("Wednesday\n"); break; case 4: printf("Thursday\n"); break; case 5: printf("Friday\n"); break; case 6: printf("Saturday\n"); break; case 7: printf("Sunday\n"); break; default: printf("Invalid input\n"); } return 0; }
3. Write a program using switch case that takes year as input and prints the name of cricket world cup winning country for that year,
- 1975: West Indies
- 1979: West Indies
- 1983: India
- 1987: Australia
- 1992: Pakistan
- 1996: Sri Lanka
- 1999: Australia
- 2003: Australia
- 2007: Australia
- 2011: India
- 2015: Australia
If world cup was not played in the year entered by the user, program should print an error message.
#include <stdio.h> int main (int argc, char * argv[]) { int year = 0; printf("Enter year of world cup: "); scanf("%d",&year); switch(year) { case 1975: printf("West Indies won the world cup in 1975\n"); break; case 1979: printf("West Indies won the world cup in 1979\n"); break; case 1983: printf("India won the world cup in 1983\n"); break; case 1987: printf("Australia won the world cup in 1987\n"); break; case 1992: printf("Pakistan won the world cup in 1992\n"); break; case 1996: printf("Sri Lanka won the world cup in 1996\n"); break; case 1999: printf("Australia won the world cup in 1999\n"); break; case 2003: printf("Australia won the world cup in 2003\n"); break; case 2007: printf("Australia won the world cup in 2007\n"); break; case 2011: printf("India won the world cup in 2011\n"); break; case 2015: printf("Australia won the world cup in 2015\n"); break; default: printf("There was no world cup played in %d\n", year); } return 0; }
4. Trace the control flow of following program for user inputs listed below.
- 4
- 5
- 9
- 11
- 3
01: #include <stdio.h> 02: int main (int argc, char * argv[]) { 03: int test = 0; 04: printf("Enter a value[1-9]: "); 05: scanf("%d",&test); 06: printf("You entered "); 07: switch(test) { 08: case 9: 09: printf("9\n"); 10: break; 11: case 3: 12: printf("3\n"); 13: default: 14: printf("Default\n"); 15: case 4: 16: printf("4\n"); 17: case 5: 18: printf("5\n"); 19: break; 20: case 6: 21: printf("6\n"); 22: break; 23: } 24: return 0; 25: }
4
- After user input control reaches line 07. test has value 4. switch control expression evaluates to 4. Control jumps to label 4, line 15.
- Control advances to line 16, message 4 is printed. There is no break statement so control advances to line 17.
- Control enters case 5, line 18. Message 5 is printed. Control advances to line 19. break statement causes control to jump out of switch case, line 24.
- Program terminates.
5
- After user input control reaches line 07. test has value 5. switch control expression evaluates to 5. Control jumps to label 4, line 17.
- Control advances to line 18, message 5 is printed.
- Control advances to line 19. break statement causes control to jump out of switch case, line 24.
- Program terminates.
9
- After user input control reaches line 07. test has value 9. switch control expression evaluates to 9. Control jumps to label 9, line 08.
- Control advances to line 09. Message 9 is printed.
- Control advances to line 10. break statement causes control to jump out of switch case, line 24.
- Program terminates.
11
- After user input control reaches line 07. test has value 11. switch control expression evaluates to 11. Since there is no matching label, control jumps to default case, line 13.
- Control advances to line 14. Message default is printed.
- Control advances to line 15 and enters case 4.
- Control advances to line 16. Message 4 is printed.
- Control advances to line 17. Control enters case 5, line 18.
- Message 5 is printed. Control advances to line 19. break statement causes control to jump out of switch case, line 24.
- Program terminates.
3
- After user input control reaches line 07. test has value 3. switch control expression evaluates to 3. Control jumps to label 3, line 11.
- Control advances to line 12. Message 3 is printed.
- Control advances to line 13 and enter default case.
- Control advances to line 14. Message default is printed.
- Control advances to line 15 and enters case 4.
- Control advances to line 16. Message 4 is printed.
- Control advances to line 17. Control enters case 5, line 18.
- Message 5 is printed. Control advances to line 19. break statement causes control to jump out of switch case, line 24.
- Program terminates.
5. Trace the control flow of the program below for following user inputs
- 99
- 100
- 7
- 49
01: #include <stdio.h> 02: int main (int argc, char * argv[]) { 03: int percentile = 0; 04: printf("Enter your percentile [1-99]: "); 05: scanf("%d",&percentile); 06: switch(percentile/10) { 07: case 9: 08: printf("Wow, you are among top 10% \n"); 09: break; 10: case 8: 11: printf("Good job, among top 20% \n"); 12: break; 13: case 7: 14: printf("Decent show, among top 30% \n"); 15: break; 16: case 6: 17: case 5: 18: printf("Pull up your socks \n"); 19: break; 20: case 4: 21: case 3: 22: case 2: 23: case 1: 24: case 0: 25: printf("You should attempt again \n"); 26: break; 27: default: 28: printf("Value out of range\n"); 29: } 30: return 0; 31: }
99
- After user input, control reaches line 06. percentile has value 99. switch control expression evaluates to 9 – recall integer division yields quotient. Control jumps to matching case label, line 07.
- Control advances to line 08. Message Wow, you are among top 10% is printed.
- Control advances to line 09. break causes control to jump out of switch case, line 30.
- Program terminates.
100
- After user input, control reaches line 06. percentile has value 100. switch control expression evaluates to 10. There is no matching case label, control jumps to default case line 27.
- Control advances to line 28. Message Value out of range is printed.
- Control advances to end of switch case and to line 30.
- Program terminates.
7
- After user input, control reaches line 06. percentile has value 7. switch control expression evaluates to 0. Control jumps to matching case label, line 24.
- Control advances to line 25. Message You should attempt again is printed.
- Control advances to line 26. break causes control to jump out of switch case, line 30.
- Program terminates.
49
- After user input, control reaches line 06. percentile has value 49. switch control expression evaluates to 4. Control jumps to matching case label, line 20.
- There is no break statement so control falls through line 21, 22, 23, 24.
- Control advances to line 25. Message You should attempt again is printed.
- Control advances to line 26. break causes control to jump out of switch case, line 30.
- Program terminates.
6. Rewrite the world cup winning team program such that there is only one print statement for each winning country.
#include <stdio.h> int main (int argc, char * argv[]) { int year = 0; printf("Enter year of world cup: "); scanf("%d",&year); switch(year) { case 1975: case 1979: printf("West Indies won the world cup in %d\n", year); break; case 1983: case 2011: printf("India won the world cup in %d\n", year); break; case 1987: case 1999: case 2003: case 2007: case 2015: printf("Australia won the world cup in %d\n", year); break; case 1992: printf("Pakistan won the world cup in %d\n", year); break; case 1996: printf("Sri Lanka won the world cup in %d\n", year); break; default: printf("There was no world cup played in %d\n", year); } return 0; }
7. Trace control flow of above program for following user inputs
- temperature = 3 and coolant_level = 4
- temperature = 1 and coolant_level = 4
- temperature = 1 and coolant_level = 3
01: #include <stdio.h> 02: int main(int argc, char * argv[]) { 03: int temperature = 0, coolant_level = 0; 04: printf("Enter reactor temperature [1]hot [2]warm [3]cold "); 05: scanf("%d", &temperature); 06: printf("Enter coolant level [1]high [2]medium [3]low "); 07: scanf("%d", &coolant_level); 08: switch (temperature) { 09: case 3: 10: printf("Kick back and have a coffee!"); 11: break; 12: case 2: 13: printf("Keep eyes open for any changes"); 14: break; 15: case 1: 16: switch (coolant_level) { 17: case 1: 18: printf("Good job. Keep it full"); 19: break; 20: case 2: 21: printf("Quick, fill up the coolant"); 22: break; 23: case 3: 24: printf("Run for your life"); 25: break; 26: default: 27: printf("Invalid input"); 28: break; 29: } 30: break; 31: default: 32: printf("Invalid input"); 33: break; 34: } 35: return 0; 36: }
temperature = 3 and coolant_level = 4
- After user input control reaches line 08. Value of temperature is 3. Hence control expression evaluates to 3. Control jumps to matching case, line 09.
- Control advances to line 10. Message Kick back and have a coffee! is printed.
- Control advances to line 11. break causes control to jump out of switch case – line 35.
- Program terminates.
temperature = 1 and coolant_level = 4
- After user input control reaches line 08. Value of temperature is 1. Hence control expression evaluates to 1. Control jumps to matching case, line 15.
- Control advances to line 16. Control expression of nested switch evaluates to value of coolant_level, i.e. 4. There is no matching case, control jumps to default case, line 26.
- Control advances to line 27. Message Invalid input is printed.
- Control advances to line 28. break causes control to jump out of nested switch case, line 30.
- break causes control to jump out of outer switch case, line 35.
- Program terminates.
temperature = 1 and coolant_level = 3
- After user input control reaches line 08. Value of temperature is 1. Hence control expression evaluates to 1. Control jumps to matching case, line 15.
- Control advances to line 16. Control expression of nested switch evaluates to value of coolant_level, i.e. 3. Control jumps to matching case, line 23.
- Control advances to line 24. Message Run for your life is printed.
- Control advances to line 25. break causes control to jump out of nested switch case, line 30.
- break causes control to jump out of outer switch case, line 35.
- Program terminates.