Statement • Syntax: switch(x) where x must be a variable of integer (int) or character (char) type. Switch statement can sometimes replace "if-else" on some problems.
Exercise: 1. Write a program using switch statement that, given a number representing a TV channel, uses a switch statement to display the call letters of the station that corresponds to that number (or displays a message indicating that the channel is not used). Use the following channel numbers and call letters: (TITLE: channel) Channel Station Name 2 WCBS 4 WNBC 5 WNEW 7 WABC 9 WOR
2. Write a program using switch statement that takes a letter grade as an input. The program will output a comment based on the letter grade as shown below. Make sure that your program will read the letter grade input whether it is upper or lower case. The program must also warn the user to enter the proper letter grade if he/she enters a letter grade not shown in the table. Equivalent Comments Letter A Excellent Very Good Passing Needs Improvement ABCF
3. Write a program using switch statement that takes a quiz score between 0 to 100 (it is assumed that the user will only enter score within the range). The program will output a comment based on the quiz score as follows. Average Comment High Achiever [80, 100] [60,80) Achiever [50,60) Satisfactory Below 50 Must see the advisor
4. Write a program using switch statement that takes the number of line segments of a figure as an input. It will output the name of the figure. The program must output a warning message if the user enters a number of sides not shown in the table below. Number of sides Name of Figure 1 Line 3 Triangle 4 Quadrilateral 5 Pentagon 6 Hexagon 7 Heptagon 8 Octagon 9 Nonagon 10 Decagon 11 Undecagon 12 Dodecagon
5. Write a program using switch statement that takes 3 quiz scores (it is assumed that the user will enter scores within the range 0-100). It will calculate the quiz average then converts the average to letter grade. Average Equivalent Letter [90, 100] A+ [80, 90) A [70, 80) B+ [60, 70) B [50, 60) C+ Below 50 F
Switch Switch Statement • Syntax: switch(x) where x must be a variable of integer (int) or character (char) type. Switch statem
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am