Welcome to Lesson 6! In this lesson, we’ll dive into switch statements in C#. Switch statements provide a more efficient way to handle multiple possible values for a single variable. They are a cleaner alternative to a series of if-else
statements when you need to execute different blocks of code based on the value of a single variable.
1. What is a Switch Statement?
A switch statement allows you to evaluate a single expression and execute code blocks based on the value of that expression. It can be more readable and manageable than using multiple if-else
statements, especially when dealing with a large number of possible values.
Syntax:
switch (expression)
{
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
// Additional cases as needed
default:
// Code to execute if no case matches
break;
}
2. Basic Switch Statement
Let’s start with a simple example of a switch statement:
Example:
int day = 3;
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
case 4:
Console.WriteLine("Thursday");
break;
case 5:
Console.WriteLine("Friday");
break;
case 6:
Console.WriteLine("Saturday");
break;
case 7:
Console.WriteLine("Sunday");
break;
default:
Console.WriteLine("Invalid day");
break;
}
Explanation:
- The
switch
statement evaluates theday
variable. - It matches the value of
day
with eachcase
. Forday
equal to 3, it prints “Wednesday”. - The
break
statement exits theswitch
block. Withoutbreak
, execution would continue into the next case.
3. The default
Case
The default
case is optional but useful for handling any values that don’t match the specified cases. It’s similar to the else
part in an if-else
chain.
Example:
int month = 13;
switch (month)
{
case 1:
Console.WriteLine("January");
break;
case 2:
Console.WriteLine("February");
break;
// Other cases...
default:
Console.WriteLine("Invalid month");
break;
}
Explanation:
- If
month
does not match any of the specified cases, it falls through to thedefault
case and prints “Invalid month”.
4. Using Multiple Cases
You can combine multiple values into a single case to execute the same block of code for different values.
Example:
int grade = 85;
switch (grade)
{
case 90:
case 91:
case 92:
case 93:
case 94:
case 95:
case 96:
case 97:
case 98:
case 99:
case 100:
Console.WriteLine("Excellent");
break;
case 80:
case 81:
case 82:
case 83:
case 84:
case 85:
case 86:
case 87:
case 88:
case 89:
Console.WriteLine("Good job");
break;
default:
Console.WriteLine("Needs improvement");
break;
}
Explanation:
- The
case
labels from 90 to 100 all execute the same block of code, printing “Excellent”. - Similarly, grades from 80 to 89 print “Good job”.
5. Switch Expressions (C# 8.0 and Later)
C# 8.0 introduced switch expressions, which provide a more concise and functional way to use switch statements. Switch expressions return a value and use the =>
syntax.
Syntax:
var result = expression switch
{
value1 => result1,
value2 => result2,
// Additional cases
_ => defaultResult
};
Example:
int day = 3;
var dayName = day switch
{
1 => "Monday",
2 => "Tuesday",
3 => "Wednesday",
4 => "Thursday",
5 => "Friday",
6 => "Saturday",
7 => "Sunday",
_ => "Invalid day"
};
Console.WriteLine(dayName);
Explanation:
- The switch expression assigns a value to
dayName
based on the value ofday
. - The
_
is used as a wildcard to handle any cases not explicitly listed, similar to thedefault
case in a traditional switch statement.
6. Example: Combining if
and switch
In some scenarios, you might need to combine if
statements with switch
statements for more complex logic.
Example:
int number = 7;
if (number % 2 == 0)
{
switch (number)
{
case 2:
Console.WriteLine("Two is the smallest prime number.");
break;
case 4:
Console.WriteLine("Four is the first composite number.");
break;
default:
Console.WriteLine("This is an even number.");
break;
}
}
else
{
Console.WriteLine("This is an odd number.");
}
Explanation:
- The
if
statement checks ifnumber
is even. - The
switch
statement handles specific even numbers, with adefault
case for other even numbers.
Summary
In this lesson, you learned how to use switch
statements in C# to handle multiple possible values of a single variable. We explored the basic syntax, the use of default
, combining multiple cases, and switch expressions introduced in C# 8.0. Switch statements and expressions provide a structured and efficient way to control the flow of your program based on variable values.
Happy coding!
0 Comments