C# - Conditions
In C#, conditions are used to control the flow of execution in a program based on whether certain conditions are true or false. Here’s a detailed overview of how to use conditional statements in C#:
1. If Statement
The if
statement evaluates a condition and executes a block of code if the condition is true.
csharpint number = 10;
if (number > 0)
{
Console.WriteLine("The number is positive.");
}
2. If-Else Statement
The if-else
statement allows you to execute one block of code if the condition is true, and another block if the condition is false.
csharpint number = -5;
if (number > 0)
{
Console.WriteLine("The number is positive.");
}
else
{
Console.WriteLine("The number is not positive.");
}
3. If-Else If-Else Statement
The if-else if-else
statement provides multiple conditions to check, executing code for the first true condition.
csharpint number = 0;
if (number > 0)
{
Console.WriteLine("The number is positive.");
}
else if (number < 0)
{
Console.WriteLine("The number is negative.");
}
else
{
Console.WriteLine("The number is zero.");
}
4. Switch Statement
The switch
statement evaluates a variable and executes the code block corresponding to the case that matches the variable's value.
csharpint dayOfWeek = 3; // Assume 1 = Monday, 2 = Tuesday, etc.
switch (dayOfWeek)
{
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 of the week");
break;
}
5. Switch Expression (C# 8.0+)
The switch expression provides a more concise and readable way to handle multiple conditions.
csharpint dayOfWeek = 3; // Assume 1 = Monday, 2 = Tuesday, etc.
string dayName = dayOfWeek switch
{
1 => "Monday",
2 => "Tuesday",
3 => "Wednesday",
4 => "Thursday",
5 => "Friday",
6 => "Saturday",
7 => "Sunday",
_ => "Invalid day of the week"
};
Console.WriteLine(dayName);
6. Ternary Conditional Operator
The ternary conditional operator is a shorthand way of writing simple if-else
statements.
csharpint number = 10;
string result = number > 0 ? "Positive" : "Non-positive";
Console.WriteLine(result);
7. Null-Coalescing Operator
The null-coalescing operator (??
) returns the left-hand operand if it is not null; otherwise, it returns the right-hand operand.
csharpstring name = null;
string displayName = name ?? "Unknown";
Console.WriteLine(displayName); // Outputs "Unknown"
8. Null-Coalescing Assignment Operator (C# 8.0+)
The null-coalescing assignment operator (??=
) assigns the right-hand operand to the left-hand operand only if the left-hand operand is null.
csharpstring name = null;
name ??= "Default Name";
Console.WriteLine(name); // Outputs "Default Name"
Example
Here’s a complete example demonstrating the use of various conditional statements:
csharpusing System;
class Program
{
static void Main()
{
int number = 15;
// If-Else
if (number % 2 == 0)
{
Console.WriteLine("The number is even.");
}
else
{
Console.WriteLine("The number is odd.");
}
// Switch
int dayOfWeek = 5;
string dayName = dayOfWeek switch
{
1 => "Monday",
2 => "Tuesday",
3 => "Wednesday",
4 => "Thursday",
5 => "Friday",
6 => "Saturday",
7 => "Sunday",
_ => "Invalid day of the week"
};
Console.WriteLine($"Day of the week: {dayName}");
// Ternary Operator
string result = number > 10 ? "Greater than 10" : "10 or less";
Console.WriteLine(result);
// Null-Coalescing Operator
string name = null;
string displayName = name ?? "Anonymous";
Console.WriteLine(displayName);
}
}
This example demonstrates various ways to handle conditional logic in C#. Let me know if you need further explanations or examples!