In C#, the switch
statement is a control flow statement that allows you to execute different blocks of code based on the value of a variable or expression. Here’s a basic example to illustrate its usage:
using System;
class Program
{
static void Main()
{
int day = 3;
string dayName;
switch (day)
{
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
case 6:
dayName = "Saturday";
break;
case 7:
dayName = "Sunday";
break;
default:
dayName = "Invalid day";
break;
}
Console.WriteLine(dayName);
}
}
Explanation:
Variable: In this example, we have an integer variable
day
which is being evaluated.Cases: Each
case
corresponds to a specific value thatday
can take.Break Statement: The
break
statement is used to exit the switch block once a matching case is found and executed.Default Case: The
default
case is optional and will execute if none of the specified cases match.
Exercise 1: Grade Calculator
Write a program that takes a numeric score (0-10) and uses a switch statement to determine the corresponding letter grade.
Example input: 5
// Example output: F
Exercise 2: Month Name
Create a program that asks the user for a month number (1-12) and prints the corresponding month name.
Example input: 4
// Example output: April
Exercise 3: Simple Calculator
Implement a simple calculator that takes two numbers and an operator (+, -, *, /) from the user and uses a switch statement to perform the calculation.
Example input: 10, 5, +
// Example output: 15
Exercise 4: Days in a Month
Write a program that asks for a month number (1-12) and uses a switch statement to print how many days are in that month (considering February has 28 days).
Example input: 2
// Example output: 28 days
Exercise 5: Traffic Light
Create a program that takes an input for a traffic light color (Red, Yellow, Green) and uses a switch statement to print the action to take (Stop, Caution, Go).
Example input: Green
// Example output: Go
Solutions to the exercises
Exercise 1: Grade Calculator
using System;
class GradeCalculator
{
static void Main()
{
Console.Write("Enter your score (0-10): ");
int score = int.Parse(Console.ReadLine());
char grade;
switch (score)
{
case 10:
case 9:
grade = 'A';
break;
case 8:
grade = 'B';
break;
case 7:
grade = 'C';
break;
case 6:
grade = 'D';
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
grade = 'F';
break;
default:
Console.WriteLine("Invalid score.");
return;
}
Console.WriteLine("Your grade is: "+grade);
}
}
Exercise 2: Month Name
using System;
class MonthName
{
static void Main()
{
Console.Write("Enter a month number (1-12): ");
int month = int.Parse(Console.ReadLine());
string monthName;
switch (month)
{
case 1: monthName = "January"; break;
case 2: monthName = "February"; break;
case 3: monthName = "March"; break;
case 4: monthName = "April"; break;
case 5: monthName = "May"; break;
case 6: monthName = "June"; break;
case 7: monthName = "July"; break;
case 8: monthName = "August"; break;
case 9: monthName = "September"; break;
case 10: monthName = "October"; break;
case 11: monthName = "November"; break;
case 12: monthName = "December"; break;
default: monthName = "Invalid month"; break;
}
Console.WriteLine(monthName);
}
}
Exercise 3: Simple Calculator
using System;
class SimpleCalculator
{
static void Main()
{
Console.Write("Enter first number: ");
double num1 = double.Parse(Console.ReadLine());
Console.Write("Enter second number: ");
double num2 = double.Parse(Console.ReadLine());
Console.Write("Enter operator (+, -, *, /): ");
char operation = Console.ReadLine();
double result;
switch (operation)
{
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break;
case '/':
if (num2 != 0)
result = num1 / num2;
else
{
Console.WriteLine("Error: Division by zero.");
return;
}
break;
default:
Console.WriteLine("Invalid operator.");
return;
}
Console.WriteLine("Result:" + result);
}
}
Exercise 4: Days in a Month
using System;
class DaysInMonth
{
static void Main()
{
Console.Write("Enter a month number (1-12): ");
int month = int.Parse(Console.ReadLine());
int days;
switch (month)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
days = 31; break;
case 4: case 6: case 9: case 11:
days = 30; break;
case 2:
days = 28; break; // Not accounting for leap years
default:
Console.WriteLine("Invalid month.");
return;
}
Console.WriteLine(days +"days");
}
}
Exercise 5: Traffic Light
using System;
class TrafficLight
{
static void Main()
{
Console.Write("Enter traffic light color (Red, Yellow, Green): ");
string color = Console.ReadLine()?.ToLower();
string action;
switch (color)
{
case "red":
action = "Stop"; break;
case "yellow":
action = "Caution"; break;
case "green":
action = "Go"; break;
default:
action = "Invalid color"; break;
}
Console.WriteLine(action);
}
}