Complete the code to execute the correct case in the switch statement.
int number = 2; switch (number) { case 1: Console.WriteLine("One"); break; case [1]: Console.WriteLine("Two"); break; default: Console.WriteLine("Other"); break; }
The switch statement matches the value of number with the case labels. Since number is 2, the case label must be 2 to execute that block.
Complete the code to print the correct message for the given day number.
int day = 5; switch (day) { case 1: Console.WriteLine("Monday"); break; case 5: Console.WriteLine("[1]"); break; default: Console.WriteLine("Invalid day"); break; }
When day is 5, the switch case for 5 runs, which should print "Friday".
Fix the error in the switch statement to correctly handle the input character.
char grade = 'B'; switch (grade) { case 'A': Console.WriteLine("Excellent"); break; case [1]: Console.WriteLine("Good"); break; default: Console.WriteLine("Needs Improvement"); break; }
In C#, character literals must be enclosed in single quotes. Using double quotes or no quotes causes errors.
Fill both blanks to complete the switch statement that prints the season based on the month number.
int month = 3; switch (month) { case [1]: case 4: case 5: Console.WriteLine("Spring"); break; case [2]: case 7: case 8: Console.WriteLine("Summer"); break; default: Console.WriteLine("Other season"); break; }
Months 3, 4, 5 are spring; months 6, 7, 8 are summer. So blanks should be 3 and 6 respectively.
Fill all three blanks to create a switch statement that prints the type of day based on the day number.
int day = 7; switch (day) { case [1]: case 7: Console.WriteLine("Weekend"); break; case [2]: case [3]: case 5: Console.WriteLine("Weekday"); break; default: Console.WriteLine("Invalid day"); break; }
Days 6 and 7 are weekend; days 1, 2, and 5 are weekdays. The blanks correspond to 6, 1, and 2.