Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is difference between using if/else and switch-case in C#?
The switch statement and if-else statements are both selection statements in C# used for decision-making, but they serve different purposes and have distinct performance characteristics. The switch statement chooses a single section to execute from a list of candidates based on pattern matching, while if-else provides conditional branching based on boolean expressions.
Syntax
Following is the syntax for a switch statement −
switch (expression) {
case value1:
// code block
break;
case value2:
// code block
break;
default:
// default code block
break;
}
Following is the syntax for if-else statements −
if (condition1) {
// code block
} else if (condition2) {
// code block
} else {
// default code block
}
Using Switch Statement
The switch statement is ideal when you need to compare a single variable against multiple constant values −
using System;
class Program {
public enum Fruits { Red, Green, Blue }
public static void Main() {
Fruits c = (Fruits)(new Random()).Next(0, 3);
switch (c) {
case Fruits.Red:
Console.WriteLine("The Fruits is red");
break;
case Fruits.Green:
Console.WriteLine("The Fruits is green");
break;
case Fruits.Blue:
Console.WriteLine("The Fruits is blue");
break;
default:
Console.WriteLine("The Fruits is unknown.");
break;
}
}
}
The output of the above code is −
The Fruits is green
Using If-Else Statements
The if-else approach provides the same functionality but uses sequential condition checking −
using System;
class Program {
public enum Fruits { Red, Green, Blue }
public static void Main() {
Fruits c = (Fruits)(new Random()).Next(0, 3);
if (c == Fruits.Red)
Console.WriteLine("The Fruits is red");
else if (c == Fruits.Green)
Console.WriteLine("The Fruits is green");
else if (c == Fruits.Blue)
Console.WriteLine("The Fruits is blue");
else
Console.WriteLine("The Fruits is unknown.");
}
}
The output of the above code is −
The Fruits is blue
Using If-Else for Complex Conditions
If-else statements excel when dealing with complex boolean conditions that switch cannot handle −
using System;
class Program {
public static void Main() {
int score = 85;
int attendance = 92;
if (score >= 90 && attendance >= 95) {
Console.WriteLine("Grade: A+");
} else if (score >= 80 && attendance >= 85) {
Console.WriteLine("Grade: A");
} else if (score >= 70) {
Console.WriteLine("Grade: B");
} else {
Console.WriteLine("Grade: C");
}
}
}
The output of the above code is −
Grade: A
Performance and Use Cases
Comparison
| Aspect | Switch Statement | If-Else Statements |
|---|---|---|
| Performance | O(1) - Jump table optimization | O(n) - Sequential evaluation |
| Use Case | Single variable against constants | Complex boolean conditions |
| Readability | Cleaner for multiple constants | Better for complex logic |
| Flexibility | Limited to equality checks | Supports ranges, logical operators |
Conclusion
Use switch statements when comparing a single variable against multiple constant values for better performance and cleaner code. Choose if-else statements for complex conditions involving ranges, logical operators, or multiple variables. The switch statement offers O(1) lookup time, making it significantly faster for scenarios with many constant comparisons.
